Skip to content

Commit

Permalink
Add tenant ID
Browse files Browse the repository at this point in the history
Signed-off-by: Mehran Tahir Rashid <[email protected]>
  • Loading branch information
mehranrashid committed Sep 12, 2023
1 parent 664452a commit 6720ead
Show file tree
Hide file tree
Showing 25 changed files with 141 additions and 69 deletions.
5 changes: 5 additions & 0 deletions src/Api/BaseApplicationEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public class BaseApplicationEntity : MongoDBEntityBase
/// </summary>
public DateTime? DateTimeUpdated { get; set; }

/// <summary>
/// Gets or set the ID of the tenant used where multiple clients would share the same data
/// </summary>
public Guid? TenantId { get; set; }

public BaseApplicationEntity()
{
SetDefaultValues();
Expand Down
5 changes: 5 additions & 0 deletions src/Api/MonaiApplicationEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ public class MonaiApplicationEntity : MongoDBEntityBase
/// </summary>
public DateTime? DateTimeUpdated { get; set; }

/// <summary>
/// Gets or set the ID of the tenant used where multiple clients would share the same data
/// </summary>
public Guid? TenantId { get; set; }

public MonaiApplicationEntity()
{
SetDefaultValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Monai.Deploy.InformaticsGateway.Database.Api.Repositories
{
public interface IDestinationApplicationEntityRepository
{
Task<List<DestinationApplicationEntity>> ToListAsync(CancellationToken cancellationToken = default);
Task<List<DestinationApplicationEntity>> ToListAsync(Expression<Func<DestinationApplicationEntity, bool>> filter, CancellationToken cancellationToken = default);

Task<DestinationApplicationEntity?> FindByNameAsync(string name, CancellationToken cancellationToken = default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Monai.Deploy.InformaticsGateway.Database.Api.Repositories
{
public interface IMonaiApplicationEntityRepository
{
Task<List<MonaiApplicationEntity>> ToListAsync(CancellationToken cancellationToken = default);
Task<List<MonaiApplicationEntity>> ToListAsync(Expression<Func<MonaiApplicationEntity, bool>> filter, CancellationToken cancellationToken = default);

Task<MonaiApplicationEntity?> FindByNameAsync(string name, CancellationToken cancellationToken = default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Monai.Deploy.InformaticsGateway.Database.Api.Repositories
{
public interface ISourceApplicationEntityRepository
{
Task<List<SourceApplicationEntity>> ToListAsync(CancellationToken cancellationToken = default);
Task<List<SourceApplicationEntity>> ToListAsync(Expression<Func<SourceApplicationEntity, bool>> filter, CancellationToken cancellationToken = default);

Task<SourceApplicationEntity?> FindByNameAsync(string name, CancellationToken cancellationToken = default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void Configure(EntityTypeBuilder<DestinationApplicationEntity> builder)
builder.Property(j => j.UpdatedBy).IsRequired(false);
builder.Property(j => j.DateTimeCreated).IsRequired();
builder.Property(j => j.DateTimeUpdated).IsRequired(false);
builder.Property(j => j.TenantId).IsRequired(false);

builder.HasIndex(p => p.Name, "idx_destination_name").IsUnique();
builder.HasIndex(p => new { p.Name, p.AeTitle, p.HostIp, p.Port }, "idx_source_all").IsUnique();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void Configure(EntityTypeBuilder<MonaiApplicationEntity> builder)
builder.Property(j => j.Grouping).IsRequired();
builder.Property(j => j.CreatedBy).IsRequired(false);
builder.Property(j => j.DateTimeCreated).IsRequired();
builder.Property(j => j.TenantId).IsRequired(false);
builder.Property(j => j.Workflows)
.HasConversion(
v => JsonSerializer.Serialize(v, jsonSerializerSettings),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void Configure(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityType
builder.Property(j => j.UpdatedBy).IsRequired(false);
builder.Property(j => j.DateTimeCreated).IsRequired();
builder.Property(j => j.DateTimeUpdated).IsRequired(false);
builder.Property(j => j.TenantId).IsRequired(false);

builder.HasIndex(p => p.Name, "idx_source_name").IsUnique();
builder.HasIndex(p => new { p.Name, p.AeTitle, p.HostIp }, "idx_source_all").IsUnique();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,11 @@ public async Task<DestinationApplicationEntity> RemoveAsync(DestinationApplicati
}).ConfigureAwait(false);
}

public async Task<List<DestinationApplicationEntity>> ToListAsync(CancellationToken cancellationToken = default)
public async Task<List<DestinationApplicationEntity>> ToListAsync(
Expression<Func<DestinationApplicationEntity, bool>> filter,
CancellationToken cancellationToken = default)
{
return await _retryPolicy.ExecuteAsync(async () =>
{
return await _dataset.ToListAsync(cancellationToken).ConfigureAwait(false);
}).ConfigureAwait(false);
return await _retryPolicy.ExecuteAsync(async () => await _dataset.Where(filter).ToListAsync(cancellationToken).ConfigureAwait(false)).ConfigureAwait(false);
}

public async Task<DestinationApplicationEntity> UpdateAsync(DestinationApplicationEntity entity, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,11 @@ public async Task<MonaiApplicationEntity> RemoveAsync(MonaiApplicationEntity ent
}).ConfigureAwait(false);
}

public async Task<List<MonaiApplicationEntity>> ToListAsync(CancellationToken cancellationToken = default)
public async Task<List<MonaiApplicationEntity>> ToListAsync(
Expression<Func<MonaiApplicationEntity, bool>> filter,
CancellationToken cancellationToken = default)
{
return await _retryPolicy.ExecuteAsync(async () =>
{
return await _dataset.ToListAsync(cancellationToken).ConfigureAwait(false);
}).ConfigureAwait(false);
return await _retryPolicy.ExecuteAsync(async () => await _dataset.Where(filter).ToListAsync(cancellationToken).ConfigureAwait(false)).ConfigureAwait(false);
}

public async Task<MonaiApplicationEntity> UpdateAsync(MonaiApplicationEntity entity, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,11 @@ public async Task<SourceApplicationEntity> RemoveAsync(SourceApplicationEntity e
}).ConfigureAwait(false);
}

public async Task<List<SourceApplicationEntity>> ToListAsync(CancellationToken cancellationToken = default)
public async Task<List<SourceApplicationEntity>> ToListAsync(
Expression<Func<SourceApplicationEntity, bool>> filter,
CancellationToken cancellationToken = default)
{
return await _retryPolicy.ExecuteAsync(async () =>
{
return await _dataset.ToListAsync(cancellationToken).ConfigureAwait(false);
}).ConfigureAwait(false);
return await _retryPolicy.ExecuteAsync(async () => await _dataset.Where(filter).ToListAsync(cancellationToken).ConfigureAwait(false)).ConfigureAwait(false);
}

public async Task<SourceApplicationEntity> UpdateAsync(SourceApplicationEntity entity, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,23 @@ public async Task GivenDestinationApplicationEntitiesInTheDatabase_WhenToListIsC
var store = new DestinationApplicationEntityRepository(_serviceScopeFactory.Object, _logger.Object, _options);

var expected = await _databaseFixture.DatabaseContext.Set<DestinationApplicationEntity>().ToListAsync().ConfigureAwait(false);
var actual = await store.ToListAsync().ConfigureAwait(false);
var actual = await store.ToListAsync(d => true).ConfigureAwait(false);

Assert.Equal(expected, actual);
}

[Fact]
public async Task GivenDestinationApplicationEntitiesInTheDatabase_WhenToListIsCalledWithFilter_ExpectFilteredEntitiesToBeReturned()
{
var store = new DestinationApplicationEntityRepository(_serviceScopeFactory.Object, _logger.Object, _options);

var expected = await _databaseFixture.DatabaseContext.Set<DestinationApplicationEntity>().ToListAsync().ConfigureAwait(false);
var actual = await store.ToListAsync(d => d.TenantId == new Guid("17d95c58-f100-4dc3-ae58-73d052fc38d1")).ConfigureAwait(false);

Assert.Single(actual);
Assert.Equal(expected.LastOrDefault(), actual.FirstOrDefault());
}

[Fact]
public async Task GivenADestinationApplicationEntity_WhenUpdatedIsCalled_ExpectItToSaved()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,23 @@ public async Task GivenMonaiApplicationEntitiesInTheDatabase_WhenToListIsCalled_
var store = new MonaiApplicationEntityRepository(_serviceScopeFactory.Object, _logger.Object, _options);

var expected = await _databaseFixture.DatabaseContext.Set<MonaiApplicationEntity>().ToListAsync().ConfigureAwait(false);
var actual = await store.ToListAsync().ConfigureAwait(false);
var actual = await store.ToListAsync(d => true).ConfigureAwait(false);

Assert.Equal(expected, actual);
}

[Fact]
public async Task GivenMonaiApplicationEntitiesInTheDatabase_WhenToListIsCalledWithFilter_ExpectFilteredEntitiesToBeReturned()
{
var store = new MonaiApplicationEntityRepository(_serviceScopeFactory.Object, _logger.Object, _options);

var expected = await _databaseFixture.DatabaseContext.Set<MonaiApplicationEntity>().ToListAsync().ConfigureAwait(false);
var actual = await store.ToListAsync(d => d.TenantId == new Guid("17d95c58-f100-4dc3-ae58-73d052fc38d1")).ConfigureAwait(false);

Assert.Single(actual);
Assert.Equal(expected.LastOrDefault(), actual.FirstOrDefault());
}

[Fact]
public async Task GivenAMonaiApplicationEntity_WhenUpdatedIsCalled_ExpectItToSaved()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,28 @@ public async Task GivenASourceApplicationEntity_WhenRemoveIsCalled_ExpectItToDel
}

[Fact]
public async Task GivenDestinationApplicationEntitiesInTheDatabase_WhenToListIsCalled_ExpectAllEntitiesToBeReturned()
public async Task GivenSourceApplicationEntitiesInTheDatabase_WhenToListIsCalled_ExpectAllEntitiesToBeReturned()
{
var store = new SourceApplicationEntityRepository(_serviceScopeFactory.Object, _logger.Object, _options);

var expected = await _databaseFixture.DatabaseContext.Set<SourceApplicationEntity>().ToListAsync().ConfigureAwait(false);
var actual = await store.ToListAsync().ConfigureAwait(false);
var actual = await store.ToListAsync(d => true).ConfigureAwait(false);

Assert.Equal(expected, actual);
}

[Fact]
public async Task GivenSourceApplicationEntitiesInTheDatabase_WhenToListIsCalledWithFilter_ExpectFilteredEntitiesToBeReturned()
{
var store = new SourceApplicationEntityRepository(_serviceScopeFactory.Object, _logger.Object, _options);

var expected = await _databaseFixture.DatabaseContext.Set<SourceApplicationEntity>().ToListAsync().ConfigureAwait(false);
var actual = await store.ToListAsync(d => d.TenantId == new Guid("17d95c58-f100-4dc3-ae58-73d052fc38d1")).ConfigureAwait(false);

Assert.Single(actual);
Assert.Equal(expected.LastOrDefault(), actual.FirstOrDefault());
}

[Fact]
public async Task GivenASourceApplicationEntity_WhenUpdatedIsCalled_ExpectItToSaved()
{
Expand Down
9 changes: 9 additions & 0 deletions src/Database/EntityFramework/Test/SqliteDatabaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void InitDatabaseWithDestinationApplicationEntities()
var aet3 = new DestinationApplicationEntity { AeTitle = "AET3", HostIp = "1.2.3.4", Port = 114, Name = "AET3" };
var aet4 = new DestinationApplicationEntity { AeTitle = "AET4", HostIp = "1.2.3.4", Port = 114, Name = "AET4" };
var aet5 = new DestinationApplicationEntity { AeTitle = "AET5", HostIp = "1.2.3.4", Port = 114, Name = "AET5" };
var aet6 = new DestinationApplicationEntity { AeTitle = "AET6", HostIp = "1.2.3.4", Port = 114, Name = "AET6", TenantId = new Guid("17d95c58-f100-4dc3-ae58-73d052fc38d1") };

var set = DatabaseContext.Set<DestinationApplicationEntity>();
set.RemoveRange(set.ToList());
Expand All @@ -55,6 +56,7 @@ public void InitDatabaseWithDestinationApplicationEntities()
set.Add(aet3);
set.Add(aet4);
set.Add(aet5);
set.Add(aet6);

DatabaseContext.SaveChanges();
}
Expand All @@ -66,6 +68,8 @@ public void InitDatabaseWithMonaiApplicationEntities()
var aet3 = new MonaiApplicationEntity { AeTitle = "AET3", Name = "AET3" };
var aet4 = new MonaiApplicationEntity { AeTitle = "AET4", Name = "AET4" };
var aet5 = new MonaiApplicationEntity { AeTitle = "AET5", Name = "AET5" };
var aet6 = new MonaiApplicationEntity { AeTitle = "AET6", Name = "AET6", TenantId = new Guid("17d95c58-f100-4dc3-ae58-73d052fc38d1") };


var set = DatabaseContext.Set<MonaiApplicationEntity>();
set.RemoveRange(set.ToList());
Expand All @@ -74,6 +78,7 @@ public void InitDatabaseWithMonaiApplicationEntities()
set.Add(aet3);
set.Add(aet4);
set.Add(aet5);
set.Add(aet6);

DatabaseContext.SaveChanges();
}
Expand All @@ -85,6 +90,7 @@ public void InitDatabaseWithVirtualApplicationEntities()
var aet3 = new VirtualApplicationEntity { VirtualAeTitle = "AET3", Name = "AET3" };
var aet4 = new VirtualApplicationEntity { VirtualAeTitle = "AET4", Name = "AET4" };
var aet5 = new VirtualApplicationEntity { VirtualAeTitle = "AET5", Name = "AET5" };
var aet6 = new VirtualApplicationEntity { VirtualAeTitle = "AET6", Name = "AET6" };

var set = DatabaseContext.Set<VirtualApplicationEntity>();
set.RemoveRange(set.ToList());
Expand All @@ -93,6 +99,7 @@ public void InitDatabaseWithVirtualApplicationEntities()
set.Add(aet3);
set.Add(aet4);
set.Add(aet5);
set.Add(aet6);

DatabaseContext.SaveChanges();
}
Expand All @@ -104,6 +111,7 @@ public void InitDatabaseWithSourceApplicationEntities()
var aet3 = new SourceApplicationEntity { AeTitle = "AET3", Name = "AET3", HostIp = "1.2.3.4" };
var aet4 = new SourceApplicationEntity { AeTitle = "AET4", Name = "AET4", HostIp = "1.2.3.4" };
var aet5 = new SourceApplicationEntity { AeTitle = "AET5", Name = "AET5", HostIp = "1.2.3.4" };
var aet6 = new SourceApplicationEntity { AeTitle = "AET6", Name = "AET6", HostIp = "1.2.3.4", TenantId = new Guid("17d95c58-f100-4dc3-ae58-73d052fc38d1") };

var set = DatabaseContext.Set<SourceApplicationEntity>();
set.RemoveRange(set.ToList());
Expand All @@ -112,6 +120,7 @@ public void InitDatabaseWithSourceApplicationEntities()
set.Add(aet3);
set.Add(aet4);
set.Add(aet5);
set.Add(aet6);

DatabaseContext.SaveChanges();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ private void CreateIndexes()
_collection.Indexes.CreateOne(new CreateIndexModel<DestinationApplicationEntity>(indexDefinitionAll, options));
}

public async Task<List<DestinationApplicationEntity>> ToListAsync(CancellationToken cancellationToken = default)
public async Task<List<DestinationApplicationEntity>> ToListAsync(
Expression<Func<DestinationApplicationEntity, bool>> filter,
CancellationToken cancellationToken = default)
{
return await _retryPolicy.ExecuteAsync(async () =>
{
return await _collection.Find(Builders<DestinationApplicationEntity>.Filter.Empty).ToListAsync(cancellationToken).ConfigureAwait(false);
}).ConfigureAwait(false);
return await _retryPolicy.ExecuteAsync(async () => await _collection.Find(filter).ToListAsync(cancellationToken).ConfigureAwait(false)).ConfigureAwait(false);
}

public async Task<DestinationApplicationEntity?> FindByNameAsync(string name, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ private void CreateIndexes()
_collection.Indexes.CreateOne(new CreateIndexModel<MonaiApplicationEntity>(indexDefinition, options));
}

public async Task<List<MonaiApplicationEntity>> ToListAsync(CancellationToken cancellationToken = default)
public async Task<List<MonaiApplicationEntity>> ToListAsync(
Expression<Func<MonaiApplicationEntity, bool>> filter,
CancellationToken cancellationToken = default)
{
return await _retryPolicy.ExecuteAsync(async () =>
{
return await _collection.Find(Builders<MonaiApplicationEntity>.Filter.Empty).ToListAsync(cancellationToken).ConfigureAwait(false);
}).ConfigureAwait(false);
return await _retryPolicy.ExecuteAsync(async () => await _collection.Find(filter).ToListAsync(cancellationToken).ConfigureAwait(false)).ConfigureAwait(false);
}

public async Task<MonaiApplicationEntity?> FindByNameAsync(string name, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ private void CreateIndexes()
_collection.Indexes.CreateOne(new CreateIndexModel<SourceApplicationEntity>(indexDefinitionAll, options));
}

public async Task<List<SourceApplicationEntity>> ToListAsync(CancellationToken cancellationToken = default)
public async Task<List<SourceApplicationEntity>> ToListAsync(
Expression<Func<SourceApplicationEntity, bool>> filter,
CancellationToken cancellationToken = default)
{
return await _retryPolicy.ExecuteAsync(async () =>
{
return await _collection.Find(Builders<SourceApplicationEntity>.Filter.Empty).ToListAsync(cancellationToken).ConfigureAwait(false);
}).ConfigureAwait(false);
return await _retryPolicy.ExecuteAsync(async () => await _collection.Find(filter).ToListAsync(cancellationToken).ConfigureAwait(false)).ConfigureAwait(false);
}

public async Task<SourceApplicationEntity?> FindByNameAsync(string name, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ public DestinationAeTitleController(
[Produces("application/json")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<IEnumerable<DestinationApplicationEntity>>> Get()
public async Task<ActionResult<IEnumerable<DestinationApplicationEntity>>> Get([FromQuery] Guid? tenantId = null)
{
try
{
return Ok(await _repository.ToListAsync(HttpContext.RequestAborted).ConfigureAwait(false));
if (tenantId is not null)
{
return Ok(await _repository.ToListAsync(d => d.TenantId == tenantId, HttpContext.RequestAborted).ConfigureAwait(false));
}

return Ok(await _repository.ToListAsync(d => true, HttpContext.RequestAborted).ConfigureAwait(false));
}
catch (Exception ex)
{
Expand Down
Loading

0 comments on commit 6720ead

Please sign in to comment.