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>> GetByGroupIdAsync(Guid groupId) { var group = await groupReposity.FindByIdAsync(groupId); if (group is null) { return Result>.Fail(ResultCode.GROUP_NOT_FOUND); } var members = await reposity.FindByGroupIdAsync(groupId); return Result>.Success(mapper.Map>(members.ToList())); } public async Task> CreateAsync(Guid groupId, Guid userId) { var group = await groupReposity.FindByIdAsync(groupId); if (group is null) { return Result.Fail(ResultCode.GROUP_NOT_FOUND); } var userRes = await idService.FindUserByIdAsync(userId); if (!userRes.Succeeded) { return Result.Fail(userRes); } var memberRes = await service.CreateAsync(userId, groupId, userRes.Data.NickName, userRes.Data.Avatar); if (!memberRes.Succeeded) { return Result.Fail(userRes); } return Result.Success(mapper.Map(memberRes.Data)); } public async Task> CheckMemberAsync(Guid groupId, Guid userId) { var exist = await reposity.CheckMemberExistAsync(groupId, userId); return Result.Success(exist); } public async Task> 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(); } } }