92 lines
3.2 KiB
C#
92 lines
3.2 KiB
C#
using AutoMapper;
|
|
using GroupService.Domain;
|
|
using GroupService.Domain.IReposities;
|
|
using GroupService.WebApi.Application.Dtos;
|
|
using GroupService.WebApi.Application.IntegrationServices;
|
|
using IM.Commons;
|
|
|
|
namespace GroupService.WebApi.Application.GroupMember
|
|
{
|
|
public class GroupMemberService
|
|
{
|
|
private readonly IGroupMemberReposity reposity;
|
|
private readonly IGroupReposity groupReposity;
|
|
private readonly GroupMemberDomainService service;
|
|
private readonly IIdentityIntegrationService idService;
|
|
private IMapper mapper;
|
|
|
|
public GroupMemberService(IGroupMemberReposity reposity, IGroupReposity groupReposity, GroupMemberDomainService service, IIdentityIntegrationService idService, IMapper mapper)
|
|
{
|
|
this.reposity = reposity;
|
|
this.groupReposity = groupReposity;
|
|
this.service = service;
|
|
this.idService = idService;
|
|
this.mapper = mapper;
|
|
}
|
|
|
|
public async Task<Result<List<GroupMemberResponse>>> GetByGroupIdAsync(Guid groupId)
|
|
{
|
|
var group = await groupReposity.FindByIdAsync(groupId);
|
|
if (group is null)
|
|
{
|
|
return Result<List<GroupMemberResponse>>.Fail(ResultCode.GROUP_NOT_FOUND);
|
|
}
|
|
var members = await reposity.FindByGroupIdAsync(groupId);
|
|
|
|
return Result<List<GroupMemberResponse>>.Success(mapper.Map<List<GroupMemberResponse>>(members.ToList()));
|
|
}
|
|
|
|
public async Task<Result<GroupMemberResponse>> CreateAsync(Guid groupId, Guid userId)
|
|
{
|
|
var group = await groupReposity.FindByIdAsync(groupId);
|
|
|
|
if (group is null)
|
|
{
|
|
return Result<GroupMemberResponse>.Fail(ResultCode.GROUP_NOT_FOUND);
|
|
}
|
|
|
|
var userRes = await idService.FindUserByIdAsync(userId);
|
|
if (!userRes.Succeeded)
|
|
{
|
|
return Result<GroupMemberResponse>.Fail(userRes);
|
|
}
|
|
|
|
var memberRes = await service.CreateAsync(userId, groupId, userRes.Data.NickName, userRes.Data.Avatar);
|
|
|
|
if (!memberRes.Succeeded)
|
|
{
|
|
return Result<GroupMemberResponse>.Fail(userRes);
|
|
}
|
|
|
|
return Result<GroupMemberResponse>.Success(mapper.Map<GroupMemberResponse>(memberRes.Data));
|
|
|
|
}
|
|
|
|
public async Task<Result<bool>> CheckMemberAsync(Guid groupId, Guid userId)
|
|
{
|
|
var exist = await reposity.CheckMemberExistAsync(groupId, userId);
|
|
|
|
return Result.Success(exist);
|
|
}
|
|
|
|
public async Task<Result<object>> DeleteAsync(Guid memberId, Guid operatorId)
|
|
{
|
|
var member = await reposity.FindByIdAsync(memberId);
|
|
if (member is null)
|
|
{
|
|
return Result.Fail(ResultCode.GROUP_MEMBER_NOT_FOUNT);
|
|
}
|
|
|
|
var operatorMember = await reposity.FindOneByGroupIdAndUserIdAsync(member.GroupId, operatorId);
|
|
if (operatorMember is null || operatorMember.Role == Domain.Enums.GroupMemberRole.Normal)
|
|
{
|
|
return Result.Fail(ResultCode.PERMISSION_DENIED);
|
|
}
|
|
|
|
member.SoftDelete();
|
|
|
|
return Result.Success();
|
|
}
|
|
}
|
|
}
|