60 lines
2.2 KiB
C#
60 lines
2.2 KiB
C#
using AutoMapper;
|
|
using GroupService.Domain.IReposities;
|
|
using GroupService.WebApi.Application.Dtos;
|
|
using IM.Commons;
|
|
|
|
namespace GroupService.WebApi.Application.Group
|
|
{
|
|
public class GroupService
|
|
{
|
|
private readonly IGroupReposity reposity;
|
|
private readonly IGroupMemberReposity memberReposity;
|
|
private readonly IMapper mapper;
|
|
|
|
public GroupService(IGroupReposity reposity, IGroupMemberReposity memberReposity, IMapper mapper)
|
|
{
|
|
this.reposity = reposity;
|
|
this.memberReposity = memberReposity;
|
|
this.mapper = mapper;
|
|
}
|
|
|
|
public async Task<Result<GroupResponse>> CreateAsync(GroupCreateCommand command)
|
|
{
|
|
var group = new Domain.Entities.Group(command.GroupMasterId, command.Name);
|
|
reposity.Create(group);
|
|
return Result<GroupResponse>.Success(mapper.Map<GroupResponse>(group));
|
|
}
|
|
|
|
public async Task<Result<List<GroupResponse>>> GetAllAsync(Guid userId)
|
|
{
|
|
var groups = await reposity.FindByMasterIdAsync(userId);
|
|
return Result<List<GroupResponse>>.Success(mapper.Map<List<GroupResponse>>(groups));
|
|
}
|
|
|
|
public async Task<Result<GroupResponse>> GetByIdAsync(Guid groupId)
|
|
{
|
|
var group = await reposity.FindByIdAsync(groupId);
|
|
if (group is null)
|
|
{
|
|
return Result<GroupResponse>.Fail(ResultCode.GROUP_NOT_FOUND);
|
|
}
|
|
|
|
return Result<GroupResponse>.Success(mapper.Map<GroupResponse>(group));
|
|
}
|
|
|
|
public async Task<Result<GroupResponse>> UpdateAsync(GroupUpdateCommand command)
|
|
{
|
|
var group = await reposity.FindByIdAsync(command.GroupId);
|
|
if(group is null)
|
|
return Result.Fail<GroupResponse>(ResultCode.GROUP_NOT_FOUND);
|
|
|
|
var isAdmin = await memberReposity.CheckMemberAdminAsync(group.Id ,command.UserId);
|
|
if (!isAdmin)
|
|
return Result.Fail<GroupResponse>(ResultCode.ADMIN_PERMISSION_DENIED);
|
|
|
|
group.Update(command.GroupName, null, command.Description, command.Avatar);
|
|
return Result.Success(mapper.Map<GroupResponse>(group));
|
|
}
|
|
}
|
|
}
|