You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.2 KiB

3 years ago
using Easy;
using Easy.DDD.Domain;
using Easy.DDD.Domain.Repositories;
using Identity.Api.Clean.Domain.Entites;
using Identity.Api.Clean.Shared.IServices;
using Microsoft.EntityFrameworkCore;
namespace Identity.Api.Clean.Domain.Services;
public class TenantManager : DomainService, ITenantManager
{
protected IRepository<Tenant> TenantRepository { get; }
public TenantManager(IRepository<Tenant> tenantRepository)
{
TenantRepository = tenantRepository;
}
public virtual async Task<Tenant> CreateAsync(string name)
{
Check.NotNull(name, nameof(name));
await ValidateNameAsync(name);
return new Tenant(GuidGenerator.Create(), name);
}
public virtual async Task ChangeNameAsync(Tenant tenant, string name)
{
await ValidateNameAsync(name, tenant.Id);
tenant.SetName(name);
}
protected virtual async Task ValidateNameAsync(string name, Guid? expectedId = null)
{
When.Is(string.IsNullOrEmpty(name), "租户名称不能为空");
var tenant = await TenantRepository.Where(o => o.TenantName == name).FirstOrDefaultAsync();
When.Is(tenant != null && tenant.Id != expectedId, "重复的租户名称: " + name);
}
}