зеркало из https://github.com/Azure/azure-saas.git
Refactored Admin and Catalog
Added Tests Updated to .NET 6
This commit is contained in:
Родитель
688450ff5f
Коммит
072c4e0684
|
@ -19,6 +19,10 @@ Project("{151D2E53-A2C4-4D7D-83FE-D05416EBD58E}") = "Saas.Provider.Web.Deploymen
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Saas.Catalog.Api", "src\Saas.Catalog\Saas.Catalog.Api\Saas.Catalog.Api.csproj", "{08C4A2D8-9EF7-4874-8ACE-07881A71E064}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Saas.Admin.Web.Tests", "Saas.Admin.Web.Tests\Saas.Admin.Web.Tests.csproj", "{CF923D89-FA69-4351-9EE1-7FD75F0A45BF}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Saas.Catalog.Api.Tests", "Saas.Catalog.Api.Tests\Saas.Catalog.Api.Tests.csproj", "{58659E0F-EF2A-4421-9704-F8816BF6417C}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -57,6 +61,14 @@ Global
|
|||
{08C4A2D8-9EF7-4874-8ACE-07881A71E064}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{08C4A2D8-9EF7-4874-8ACE-07881A71E064}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{08C4A2D8-9EF7-4874-8ACE-07881A71E064}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{CF923D89-FA69-4351-9EE1-7FD75F0A45BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CF923D89-FA69-4351-9EE1-7FD75F0A45BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CF923D89-FA69-4351-9EE1-7FD75F0A45BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CF923D89-FA69-4351-9EE1-7FD75F0A45BF}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{58659E0F-EF2A-4421-9704-F8816BF6417C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{58659E0F-EF2A-4421-9704-F8816BF6417C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{58659E0F-EF2A-4421-9704-F8816BF6417C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{58659E0F-EF2A-4421-9704-F8816BF6417C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
|
||||
<PackageReference Include="Moq" Version="4.16.1" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\src\Saas.Admin\Saas.Admin.Web\Saas.Admin.Web.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,263 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Saas.Admin.Web.Models;
|
||||
using Saas.Admin.Web.Services;
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Saas.Admin.Web.Tests.Services
|
||||
{
|
||||
[TestClass]
|
||||
public class TenantServiceTests
|
||||
{
|
||||
private ITenantService _tenantService;
|
||||
|
||||
public TenantServiceTests()
|
||||
{
|
||||
_tenantService = new TenantService(Context);
|
||||
}
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
_tenantService = new TenantService(Context);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_GetItems_EmptyReturnsNone()
|
||||
{
|
||||
//Arrange
|
||||
|
||||
//Act
|
||||
var results = await _tenantService.GetItemsAsync();
|
||||
|
||||
//Assert
|
||||
Assert.IsFalse(results.Any());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_GetItem_EmptyReturnsNone()
|
||||
{
|
||||
//Arrange
|
||||
var guid = Guid.NewGuid();
|
||||
|
||||
//Act
|
||||
var results = await _tenantService.GetItemAsync(guid);
|
||||
|
||||
//Assert
|
||||
Assert.IsNull(results);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(DbUpdateException))]
|
||||
public async Task TenantService_AddItemWithoutRequired_Throws()
|
||||
{
|
||||
//Arrange
|
||||
var tenant = new Tenant();
|
||||
|
||||
//Act
|
||||
await _tenantService.AddItemAsync(tenant);
|
||||
|
||||
//Assert
|
||||
// Expected Exception Microsoft.EntityFrameworkCore.DbUpdateException
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_AddItemWithRequired_Adds()
|
||||
{
|
||||
//Arrange
|
||||
var tenant = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
|
||||
//Act
|
||||
var beforeCount = (await _tenantService.GetItemsAsync()).Count<Tenant>();
|
||||
|
||||
await _tenantService.AddItemAsync(tenant);
|
||||
|
||||
//Assert
|
||||
int afterAddCount = (await _tenantService.GetItemsAsync()).Count<Tenant>();
|
||||
Assert.AreNotEqual(beforeCount, afterAddCount);
|
||||
Assert.IsTrue(afterAddCount == 1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_GetItemInvalid_ReturnsTenant()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenant2 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 2",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant2);
|
||||
|
||||
//Act
|
||||
var result = await _tenantService.GetItemAsync(tenant1.Id);
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue((await _tenantService.GetItemsAsync()).Count<Tenant>() == 2);
|
||||
Assert.AreEqual(result, tenant1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_GetItemInvalid_Null()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenant2 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 2",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant2);
|
||||
|
||||
//Act
|
||||
var result = await _tenantService.GetItemAsync(Guid.NewGuid());
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue((await _tenantService.GetItemsAsync()).Count<Tenant>() == 2);
|
||||
Assert.IsNull(result);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_DeleteItemInvalid_DeletesNothing()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenant2 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 2",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant2);
|
||||
|
||||
//Act
|
||||
await _tenantService.DeleteItemAsync(Guid.NewGuid());
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue((await _tenantService.GetItemsAsync()).Count<Tenant>() == 2);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_DeleteItem_DeletesTenant()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenant2 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 2",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant2);
|
||||
|
||||
//Act
|
||||
await _tenantService.DeleteItemAsync(tenant1.Id);
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue((await _tenantService.GetItemsAsync()).Count<Tenant>() == 1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_UpdateItem_UpdatesTenant()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenantFromDB = await _tenantService.GetItemAsync(tenant1.Id);
|
||||
Assert.AreEqual(tenant1, tenantFromDB);
|
||||
|
||||
//Act
|
||||
var updatedName = "Updated Name";
|
||||
tenant1.Name = updatedName;
|
||||
await _tenantService.UpdateItemAsync(tenant1);
|
||||
var updatedTenantFromDB = await _tenantService.GetItemAsync(tenant1.Id);
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue(updatedTenantFromDB.Name == updatedName);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_UpdateInvalidItem_NoUpdate()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
|
||||
//Act
|
||||
var updatedItem = new Tenant()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
IsActive = tenant1.IsActive,
|
||||
Name = "Updated Name",
|
||||
UserId = tenant1.UserId
|
||||
};
|
||||
await _tenantService.UpdateItemAsync(updatedItem);
|
||||
var updatedTenantFromDB = await _tenantService.GetItemAsync(tenant1.Id);
|
||||
|
||||
//Assert
|
||||
Assert.AreEqual("Test tenant 1", updatedTenantFromDB.Name);
|
||||
}
|
||||
|
||||
|
||||
public CatalogDbContext Context => InMemoryContext();
|
||||
private CatalogDbContext InMemoryContext()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<CatalogDbContext>()
|
||||
.UseInMemoryDatabase(Guid.NewGuid().ToString())
|
||||
.EnableSensitiveDataLogging()
|
||||
.Options;
|
||||
var context = new CatalogDbContext(options);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\src\Saas.Catalog\Saas.Catalog.Api\Saas.Catalog.Api.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,262 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Saas.Catalog.Api.Models;
|
||||
using Saas.Catalog.Api.Services;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Saas.Catalog.Api.Tests.Services
|
||||
{
|
||||
[TestClass]
|
||||
public class TenantServiceTests
|
||||
{
|
||||
private ITenantService _tenantService;
|
||||
|
||||
public TenantServiceTests()
|
||||
{
|
||||
_tenantService = new TenantService(Context);
|
||||
}
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
_tenantService = new TenantService(Context);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_GetItems_EmptyReturnsNone()
|
||||
{
|
||||
//Arrange
|
||||
|
||||
//Act
|
||||
var results = await _tenantService.GetItemsAsync();
|
||||
|
||||
//Assert
|
||||
Assert.IsFalse(results.Any());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_GetItem_EmptyReturnsNone()
|
||||
{
|
||||
//Arrange
|
||||
var guid = Guid.NewGuid();
|
||||
|
||||
//Act
|
||||
var results = await _tenantService.GetItemAsync(guid);
|
||||
|
||||
//Assert
|
||||
Assert.IsNull(results);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(DbUpdateException))]
|
||||
public async Task TenantService_AddItemWithoutRequired_Throws()
|
||||
{
|
||||
//Arrange
|
||||
var tenant = new Tenant();
|
||||
|
||||
//Act
|
||||
await _tenantService.AddItemAsync(tenant);
|
||||
|
||||
//Assert
|
||||
// Expected Exception Microsoft.EntityFrameworkCore.DbUpdateException
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_AddItemWithRequired_Adds()
|
||||
{
|
||||
//Arrange
|
||||
var tenant = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
|
||||
//Act
|
||||
var beforeCount = (await _tenantService.GetItemsAsync()).Count<Tenant>();
|
||||
|
||||
await _tenantService.AddItemAsync(tenant);
|
||||
|
||||
//Assert
|
||||
int afterAddCount = (await _tenantService.GetItemsAsync()).Count<Tenant>();
|
||||
Assert.AreNotEqual(beforeCount, afterAddCount);
|
||||
Assert.IsTrue(afterAddCount == 1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_GetItemInvalid_ReturnsTenant()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenant2 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 2",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant2);
|
||||
|
||||
//Act
|
||||
var result = await _tenantService.GetItemAsync(tenant1.Id);
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue((await _tenantService.GetItemsAsync()).Count<Tenant>() == 2);
|
||||
Assert.AreEqual(result, tenant1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_GetItemInvalid_Null()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenant2 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 2",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant2);
|
||||
|
||||
//Act
|
||||
var result = await _tenantService.GetItemAsync(Guid.NewGuid());
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue((await _tenantService.GetItemsAsync()).Count<Tenant>() == 2);
|
||||
Assert.IsNull(result);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_DeleteItemInvalid_DeletesNothing()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenant2 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 2",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant2);
|
||||
|
||||
//Act
|
||||
await _tenantService.DeleteItemAsync(Guid.NewGuid());
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue((await _tenantService.GetItemsAsync()).Count<Tenant>() == 2);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_DeleteItem_DeletesTenant()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenant2 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 2",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant2);
|
||||
|
||||
//Act
|
||||
await _tenantService.DeleteItemAsync(tenant1.Id);
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue((await _tenantService.GetItemsAsync()).Count<Tenant>() == 1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_UpdateItem_UpdatesTenant()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenantFromDB = await _tenantService.GetItemAsync(tenant1.Id);
|
||||
Assert.AreEqual(tenant1, tenantFromDB);
|
||||
|
||||
//Act
|
||||
var updatedName = "Updated Name";
|
||||
tenant1.Name = updatedName;
|
||||
await _tenantService.UpdateItemAsync(tenant1);
|
||||
var updatedTenantFromDB = await _tenantService.GetItemAsync(tenant1.Id);
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue(updatedTenantFromDB.Name == updatedName);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_UpdateInvalidItem_NoUpdate()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
|
||||
//Act
|
||||
var updatedItem = new Tenant()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
IsActive = tenant1.IsActive,
|
||||
Name = "Updated Name",
|
||||
UserId = tenant1.UserId
|
||||
};
|
||||
await _tenantService.UpdateItemAsync(updatedItem);
|
||||
var updatedTenantFromDB = await _tenantService.GetItemAsync(tenant1.Id);
|
||||
|
||||
//Assert
|
||||
Assert.AreEqual("Test tenant 1", updatedTenantFromDB.Name);
|
||||
}
|
||||
|
||||
|
||||
public CatalogDbContext Context => InMemoryContext();
|
||||
private CatalogDbContext InMemoryContext()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<CatalogDbContext>()
|
||||
.UseInMemoryDatabase(Guid.NewGuid().ToString())
|
||||
.EnableSensitiveDataLogging()
|
||||
.Options;
|
||||
var context = new CatalogDbContext(options);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,259 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Saas.Orders.Api.Tests.Services
|
||||
{
|
||||
[TestClass]
|
||||
public class TenantServiceTests
|
||||
{
|
||||
private ITenantService _tenantService;
|
||||
|
||||
public TenantServiceTests()
|
||||
{
|
||||
_tenantService = new TenantService(Context);
|
||||
}
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
_tenantService = new TenantService(Context);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_GetItems_EmptyReturnsNone()
|
||||
{
|
||||
//Arrange
|
||||
|
||||
//Act
|
||||
var results = await _tenantService.GetItemsAsync();
|
||||
|
||||
//Assert
|
||||
Assert.IsFalse(results.Any());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_GetItem_EmptyReturnsNone()
|
||||
{
|
||||
//Arrange
|
||||
var guid = Guid.NewGuid();
|
||||
|
||||
//Act
|
||||
var results = await _tenantService.GetItemAsync(guid);
|
||||
|
||||
//Assert
|
||||
Assert.IsNull(results);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(DbUpdateException))]
|
||||
public async Task TenantService_AddItemWithoutRequired_Throws()
|
||||
{
|
||||
//Arrange
|
||||
var tenant = new Tenant();
|
||||
|
||||
//Act
|
||||
await _tenantService.AddItemAsync(tenant);
|
||||
|
||||
//Assert
|
||||
// Expected Exception Microsoft.EntityFrameworkCore.DbUpdateException
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_AddItemWithRequired_Adds()
|
||||
{
|
||||
//Arrange
|
||||
var tenant = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
|
||||
//Act
|
||||
var beforeCount = (await _tenantService.GetItemsAsync()).Count<Tenant>();
|
||||
|
||||
await _tenantService.AddItemAsync(tenant);
|
||||
|
||||
//Assert
|
||||
int afterAddCount = (await _tenantService.GetItemsAsync()).Count<Tenant>();
|
||||
Assert.AreNotEqual(beforeCount, afterAddCount);
|
||||
Assert.IsTrue(afterAddCount == 1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_GetItemInvalid_ReturnsTenant()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenant2 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 2",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant2);
|
||||
|
||||
//Act
|
||||
var result = await _tenantService.GetItemAsync(tenant1.Id);
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue((await _tenantService.GetItemsAsync()).Count<Tenant>() == 2);
|
||||
Assert.AreEqual(result, tenant1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_GetItemInvalid_Null()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenant2 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 2",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant2);
|
||||
|
||||
//Act
|
||||
var result = await _tenantService.GetItemAsync(Guid.NewGuid());
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue((await _tenantService.GetItemsAsync()).Count<Tenant>() == 2);
|
||||
Assert.IsNull(result);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_DeleteItemInvalid_DeletesNothing()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenant2 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 2",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant2);
|
||||
|
||||
//Act
|
||||
await _tenantService.DeleteItemAsync(Guid.NewGuid());
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue((await _tenantService.GetItemsAsync()).Count<Tenant>() == 2);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_DeleteItem_DeletesTenant()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenant2 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 2",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant2);
|
||||
|
||||
//Act
|
||||
await _tenantService.DeleteItemAsync(tenant1.Id);
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue((await _tenantService.GetItemsAsync()).Count<Tenant>() == 1);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_UpdateItem_UpdatesTenant()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
var tenantFromDB = await _tenantService.GetItemAsync(tenant1.Id);
|
||||
Assert.AreEqual(tenant1, tenantFromDB);
|
||||
|
||||
//Act
|
||||
var updatedName = "Updated Name";
|
||||
tenant1.Name = updatedName;
|
||||
await _tenantService.UpdateItemAsync(tenant1);
|
||||
var updatedTenantFromDB = await _tenantService.GetItemAsync(tenant1.Id);
|
||||
|
||||
//Assert
|
||||
Assert.IsTrue(updatedTenantFromDB.Name == updatedName);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TenantService_UpdateInvalidItem_NoUpdate()
|
||||
{
|
||||
//Arrange
|
||||
var tenant1 = new Tenant()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
IsActive = true,
|
||||
Name = "Test tenant 1",
|
||||
UserId = Guid.NewGuid().ToString()
|
||||
};
|
||||
await _tenantService.AddItemAsync(tenant1);
|
||||
|
||||
//Act
|
||||
var updatedItem = new Tenant()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
IsActive = tenant1.IsActive,
|
||||
Name = "Updated Name",
|
||||
UserId = tenant1.UserId
|
||||
};
|
||||
await _tenantService.UpdateItemAsync(updatedItem);
|
||||
var updatedTenantFromDB = await _tenantService.GetItemAsync(tenant1.Id);
|
||||
|
||||
//Assert
|
||||
Assert.AreEqual("Test tenant 1", updatedTenantFromDB.Name);
|
||||
}
|
||||
|
||||
|
||||
public CatalogDbContext Context => InMemoryContext();
|
||||
private CatalogDbContext InMemoryContext()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<CatalogDbContext>()
|
||||
.UseInMemoryDatabase(Guid.NewGuid().ToString())
|
||||
.EnableSensitiveDataLogging()
|
||||
.Options;
|
||||
var context = new CatalogDbContext(options);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Saas.Admin.Web.Models;
|
||||
using Saas.Admin.Web.Services;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
@ -12,17 +13,17 @@ namespace Saas.Admin.Web.Controllers
|
|||
public class HomeController : Controller
|
||||
{
|
||||
private readonly ILogger<HomeController> _logger;
|
||||
private readonly CatalogDbContext _context;
|
||||
private readonly ITenantService _tenantService;
|
||||
|
||||
public HomeController(ILogger<HomeController> logger, CatalogDbContext context)
|
||||
public HomeController(ILogger<HomeController> logger, ITenantService tenantService)
|
||||
{
|
||||
_logger = logger;
|
||||
_context = context;
|
||||
_tenantService = tenantService;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> IndexAsync()
|
||||
{
|
||||
return View(await _context.Tenants.ToListAsync());
|
||||
return View(await _tenantService.GetItemsAsync());
|
||||
}
|
||||
|
||||
public IActionResult Privacy()
|
||||
|
|
|
@ -1,25 +1,24 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Saas.Admin.Web.Models;
|
||||
using Saas.Admin.Web.Services;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Saas.Admin.Web.Controllers
|
||||
{
|
||||
public class TenantsController : Controller
|
||||
{
|
||||
private readonly CatalogDbContext _context;
|
||||
private readonly ITenantService _tenantService;
|
||||
|
||||
public TenantsController(CatalogDbContext context)
|
||||
public TenantsController(ITenantService tenantService)
|
||||
{
|
||||
_context = context;
|
||||
_tenantService = tenantService;
|
||||
}
|
||||
|
||||
// GET: Tenants
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
return View(await _context.Tenants.ToListAsync());
|
||||
return View(await _tenantService.GetItemsAsync());
|
||||
}
|
||||
|
||||
// GET: Tenants/Details/5
|
||||
|
@ -30,8 +29,7 @@ namespace Saas.Admin.Web.Controllers
|
|||
return NotFound();
|
||||
}
|
||||
|
||||
var tenant = await _context.Tenants
|
||||
.FirstOrDefaultAsync(m => m.Id == id);
|
||||
var tenant = await _tenantService.GetItemAsync(id.Value);
|
||||
if (tenant == null)
|
||||
{
|
||||
return NotFound();
|
||||
|
@ -55,9 +53,7 @@ namespace Saas.Admin.Web.Controllers
|
|||
{
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
tenant.Id = Guid.NewGuid();
|
||||
_context.Add(tenant);
|
||||
await _context.SaveChangesAsync();
|
||||
await _tenantService.AddItemAsync(tenant);
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
return View(tenant);
|
||||
|
@ -71,7 +67,7 @@ namespace Saas.Admin.Web.Controllers
|
|||
return NotFound();
|
||||
}
|
||||
|
||||
var tenant = await _context.Tenants.FindAsync(id);
|
||||
var tenant = await _tenantService.GetItemAsync(id.Value);
|
||||
if (tenant == null)
|
||||
{
|
||||
return NotFound();
|
||||
|
@ -93,22 +89,12 @@ namespace Saas.Admin.Web.Controllers
|
|||
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
try
|
||||
var lookupTenant = await _tenantService.GetItemAsync(id);
|
||||
if (lookupTenant == null)
|
||||
{
|
||||
_context.Update(tenant);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!TenantExists(tenant.Id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
await _tenantService.UpdateItemAsync(tenant);
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
return View(tenant);
|
||||
|
@ -122,13 +108,11 @@ namespace Saas.Admin.Web.Controllers
|
|||
return NotFound();
|
||||
}
|
||||
|
||||
var tenant = await _context.Tenants
|
||||
.FirstOrDefaultAsync(m => m.Id == id);
|
||||
var tenant = await _tenantService.GetItemAsync(id.Value);
|
||||
if (tenant == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return View(tenant);
|
||||
}
|
||||
|
||||
|
@ -137,15 +121,13 @@ namespace Saas.Admin.Web.Controllers
|
|||
[ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DeleteConfirmed(Guid id)
|
||||
{
|
||||
var tenant = await _context.Tenants.FindAsync(id);
|
||||
_context.Tenants.Remove(tenant);
|
||||
await _context.SaveChangesAsync();
|
||||
var tenant = await _tenantService.GetItemAsync(id);
|
||||
if (tenant == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
await _tenantService.DeleteItemAsync(id);
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
private bool TenantExists(Guid id)
|
||||
{
|
||||
return _context.Tenants.Any(e => e.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Saas.Admin.Web.Models
|
||||
{
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
using Saas.Admin.Web.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Saas.Admin.Web.Services
|
||||
{
|
||||
public interface ITenantService
|
||||
{
|
||||
Task<IEnumerable<Tenant>> GetItemsAsync();
|
||||
Task<Tenant> GetItemAsync(Guid id);
|
||||
Task AddItemAsync(Tenant item);
|
||||
Task UpdateItemAsync(Tenant item);
|
||||
Task DeleteItemAsync(Guid id);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Saas.Admin.Web.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Saas.Admin.Web.Services
|
||||
|
||||
|
||||
{
|
||||
public class TenantService : ITenantService
|
||||
{
|
||||
private CatalogDbContext _context;
|
||||
|
||||
public TenantService(CatalogDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task AddItemAsync(Tenant item)
|
||||
{
|
||||
_context.Tenants.Add(item);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteItemAsync(Guid Id)
|
||||
{
|
||||
var tenant = await GetItemAsync(Id);
|
||||
if (tenant == null)
|
||||
{
|
||||
// TODO: throw not found exception
|
||||
}
|
||||
else
|
||||
{
|
||||
_context.Tenants.Remove(tenant);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Tenant> GetItemAsync(Guid id)
|
||||
{
|
||||
return await _context.Tenants.FindAsync(id);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Tenant>> GetItemsAsync()
|
||||
{
|
||||
return await _context.Tenants.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateItemAsync(Tenant item)
|
||||
{
|
||||
if (!await _context.Tenants.AnyAsync(t => t.Id == item.Id))
|
||||
{
|
||||
// TODO: throw not found exception
|
||||
return;
|
||||
}
|
||||
_context.Tenants.Update(item);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ using Microsoft.Extensions.Hosting;
|
|||
using Microsoft.Identity.Web;
|
||||
using Microsoft.Identity.Web.UI;
|
||||
using Saas.Admin.Web.Models;
|
||||
using Saas.Admin.Web.Services;
|
||||
|
||||
namespace Saas.Admin.Web
|
||||
{
|
||||
|
@ -26,6 +27,7 @@ namespace Saas.Admin.Web
|
|||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddDbContext<CatalogDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("CatalogDbConnection")));
|
||||
services.AddTransient<ITenantService, TenantService>();
|
||||
|
||||
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
|
||||
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
|
||||
|
|
|
@ -2,10 +2,9 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Saas.Catalog.Api.Models;
|
||||
using Saas.Catalog.Api.Services;
|
||||
|
||||
namespace Saas.Catalog.Api.Controllers
|
||||
{
|
||||
|
@ -13,25 +12,25 @@ namespace Saas.Catalog.Api.Controllers
|
|||
[ApiController]
|
||||
public class TenantsController : ControllerBase
|
||||
{
|
||||
private readonly sqldbcatalogdevContext _context;
|
||||
private readonly ITenantService _tenantService;
|
||||
|
||||
public TenantsController(sqldbcatalogdevContext context)
|
||||
public TenantsController(ITenantService tenantService)
|
||||
{
|
||||
_context = context;
|
||||
_tenantService = tenantService;
|
||||
}
|
||||
|
||||
// GET: api/Tenants
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<Tenant>>> GetTenants()
|
||||
public async Task<IEnumerable<Tenant>> GetTenants()
|
||||
{
|
||||
return await _context.Tenants.ToListAsync();
|
||||
return await _tenantService.GetItemsAsync();
|
||||
}
|
||||
|
||||
// GET: api/Tenants/5
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<Tenant>> GetTenant(Guid id)
|
||||
{
|
||||
var tenant = await _context.Tenants.FindAsync(id);
|
||||
var tenant = await _tenantService.GetItemAsync(id);
|
||||
|
||||
if (tenant == null)
|
||||
{
|
||||
|
@ -51,24 +50,14 @@ namespace Saas.Catalog.Api.Controllers
|
|||
return BadRequest();
|
||||
}
|
||||
|
||||
_context.Entry(tenant).State = EntityState.Modified;
|
||||
|
||||
try
|
||||
//TODO replace with exception
|
||||
var dbtenant = await _tenantService.GetItemAsync(id);
|
||||
if (dbtenant == null)
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
if (!TenantExists(id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
await _tenantService.UpdateItemAsync(tenant);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
|
@ -77,21 +66,19 @@ namespace Saas.Catalog.Api.Controllers
|
|||
[HttpPost]
|
||||
public async Task<ActionResult<Tenant>> PostTenant(Tenant tenant)
|
||||
{
|
||||
_context.Tenants.Add(tenant);
|
||||
try
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch (DbUpdateException)
|
||||
{
|
||||
if (TenantExists(tenant.Id))
|
||||
// TODO use exceptions or tweak checking method
|
||||
var dbtenant = await _tenantService.GetItemAsync(tenant.Id);
|
||||
if (dbtenant != null)
|
||||
{
|
||||
return Conflict();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
await _tenantService.AddItemAsync(tenant);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return CreatedAtAction("GetTenant", new { id = tenant.Id }, tenant);
|
||||
|
@ -101,21 +88,14 @@ namespace Saas.Catalog.Api.Controllers
|
|||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> DeleteTenant(Guid id)
|
||||
{
|
||||
var tenant = await _context.Tenants.FindAsync(id);
|
||||
var tenant = await _tenantService.GetItemAsync(id);
|
||||
if (tenant == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
_context.Tenants.Remove(tenant);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
await _tenantService.DeleteItemAsync(id);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
private bool TenantExists(Guid id)
|
||||
{
|
||||
return _context.Tenants.Any(e => e.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,17 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Saas.Catalog.Api.Models
|
||||
{
|
||||
public partial class sqldbcatalogdevContext : DbContext
|
||||
public partial class CatalogDbContext : DbContext
|
||||
{
|
||||
public sqldbcatalogdevContext()
|
||||
public CatalogDbContext()
|
||||
{
|
||||
}
|
||||
|
||||
public sqldbcatalogdevContext(DbContextOptions<sqldbcatalogdevContext> options)
|
||||
public CatalogDbContext(DbContextOptions<CatalogDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
|
@ -33,7 +30,7 @@ namespace Saas.Catalog.Api.Models
|
|||
{
|
||||
entity.ToTable("Tenant");
|
||||
|
||||
entity.Property(e => e.Id).ValueGeneratedNever();
|
||||
entity.Property(e => e.Id).HasDefaultValueSql("(newid())");
|
||||
|
||||
entity.Property(e => e.Created)
|
||||
.HasColumnType("datetime")
|
|
@ -1,13 +1,13 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.10" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
using Saas.Catalog.Api.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Saas.Catalog.Api.Services
|
||||
{
|
||||
public interface ITenantService
|
||||
{
|
||||
Task<IEnumerable<Tenant>> GetItemsAsync();
|
||||
Task<Tenant> GetItemAsync(Guid id);
|
||||
Task AddItemAsync(Tenant item);
|
||||
Task UpdateItemAsync(Tenant item);
|
||||
Task DeleteItemAsync(Guid id);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Saas.Catalog.Api.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Saas.Catalog.Api.Services
|
||||
{
|
||||
public class TenantService : ITenantService
|
||||
{
|
||||
private CatalogDbContext _context;
|
||||
|
||||
public TenantService(CatalogDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task AddItemAsync(Tenant item)
|
||||
{
|
||||
_context.Tenants.Add(item);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteItemAsync(Guid Id)
|
||||
{
|
||||
var tenant = await GetItemAsync(Id);
|
||||
if (tenant == null)
|
||||
{
|
||||
// TODO: throw not found exception
|
||||
}
|
||||
else
|
||||
{
|
||||
_context.Tenants.Remove(tenant);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Tenant> GetItemAsync(Guid id)
|
||||
{
|
||||
return await _context.Tenants.FindAsync(id);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Tenant>> GetItemsAsync()
|
||||
{
|
||||
return await _context.Tenants.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateItemAsync(Tenant item)
|
||||
{
|
||||
if (!await _context.Tenants.AnyAsync(t => t.Id == item.Id))
|
||||
{
|
||||
// TODO: throw not found exception
|
||||
return;
|
||||
}
|
||||
_context.Tenants.Update(item);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ using Microsoft.Extensions.Configuration;
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Saas.Catalog.Api.Models;
|
||||
using Saas.Catalog.Api.Services;
|
||||
|
||||
namespace Saas.Catalog.Api
|
||||
{
|
||||
|
@ -27,8 +28,9 @@ namespace Saas.Catalog.Api
|
|||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Saas.Catalog.Api", Version = "v1" });
|
||||
});
|
||||
|
||||
services.AddDbContext<sqldbcatalogdevContext>(options =>
|
||||
services.AddDbContext<CatalogDbContext>(options =>
|
||||
options.UseSqlServer(Configuration.GetConnectionString("CatalogDbConnection")));
|
||||
services.AddTransient<ITenantService, TenantService>();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
Загрузка…
Ссылка в новой задаче