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.
 
 
 
 
 

94 lines
2.8 KiB

using AutoMapper;
using Easy;
using Easy.DDD.Application;
using Easy.DDD.Domain.Repositories;
using Easy.Extensions;
using Easy.Result;
using IdentityServer.DDD.Contracts.Inputs;
using IdentityServer.DDD.Contracts.Models;
using IdentityServer.DDD.Domain.Entites;
using IdentityServer.DDD.Shared.IServices;
using Microsoft.EntityFrameworkCore;
namespace IdentityServer.DDD.Application;
public class TenantAppService : ApiService
{
protected IRepository<Tenant> TenantRepository { get; }
protected ITenantManager TenantDomainService { get; }
protected IMapper Mapper { get; }
public TenantAppService(
IRepository<Tenant> tenantRepository,
ITenantManager tenantDomainService,
IMapper mapper)
{
TenantRepository = tenantRepository;
TenantDomainService = tenantDomainService;
Mapper = mapper;
}
public virtual async Task<ApiResultValue<TenantModel>> GetAsync(Guid id)
{
var tenant = await TenantRepository.FirstOrDefaultAsync(o => o.Id == id);
var value = Mapper.Map<TenantModel>(tenant);
return ApiResult.ValueSuccess(value);
}
public virtual async Task<ApiResultPaged<TenantModel>> GetListAsync(GetTenantsInput input)
{
var count = await TenantRepository.LongCountAsync();
var tenants = await TenantRepository.Set
.ApplyPaged(input).ToListAsync();
var pageCount = AppData.PageCount(count, input.PageSize);
var valuePaged = Mapper.Map<List<TenantModel>>(tenants);
return ApiResult.ValuesPagedSuccess(pageCount, valuePaged);
}
public virtual async Task<ApiResultValue<TenantModel>> CreateAsync(TenantCreateInput input)
{
var tenant = await TenantDomainService.CreateAsync(input.TenantName);
Mapper.Map(input, tenant);
await TenantRepository.AddAsync(tenant, true);
var value = Mapper.Map<TenantModel>(tenant);
return ApiResult.ValueSuccess(value);
}
public virtual async Task<ApiResultValue<TenantModel>> UpdateAsync(Guid id, TenantUpdateInput input)
{
var tenant = await TenantRepository.FirstOrDefaultAsync(o => o.Id == id);
When.Is(tenant == null, "此租户不存在");
await TenantDomainService.ChangeNameAsync(tenant, input.TenantName);
Mapper.Map(input, tenant);
await TenantRepository.UpdateAsync(tenant, true);
var value = Mapper.Map<TenantModel>(tenant);
return ApiResult.ValueSuccess(value);
}
public virtual async Task<ApiResult> DeleteAsync(Guid id)
{
var tenant = await TenantRepository.FirstOrDefaultAsync(o => o.Id == id);
if (tenant != null)
{
await TenantRepository.RemoveAsync(tenant, true);
}
return ApiResult.Success();
}
}