Files
IM_NEW/GroupService.WebApi/Application/GroupMember/GroupMemberService.cs
T

117 lines
4.7 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; private readonly IM.InitCommon.Management.RuntimePolicy runtime;
public GroupMemberService(IGroupMemberReposity reposity, IGroupReposity groupReposity, GroupMemberDomainService service, IIdentityIntegrationService idService, IMapper mapper, IM.InitCommon.Management.RuntimePolicy runtime)
{
this.reposity = reposity;
this.groupReposity = groupReposity;
this.service = service;
this.idService = idService;
this.mapper = mapper; this.runtime = runtime;
}
public async Task<Result<List<GroupMemberResponse>>> GetByGroupIdAsync(Guid groupId, Guid userId)
{
var group = await groupReposity.FindByIdAsync(groupId);
if (group is null)
{
return Result<List<GroupMemberResponse>>.Fail(ResultCode.GROUP_NOT_FOUND);
}
if (!await reposity.CheckMemberExistAsync(groupId, userId))
{
return Result<List<GroupMemberResponse>>.Fail(ResultCode.PERMISSION_DENIED);
}
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);
}
if (group.Status != Domain.Enums.GroupState.Normal) throw new IM.DomainCommons.DomainException("群组已被封禁,不能加入");
var limit = runtime.Current.GroupMemberLimit;
if (limit > 0 && (await reposity.FindByGroupIdAsync(groupId)).Count() >= limit) throw new IM.DomainCommons.DomainException("群成员数量已达到平台上限");
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 group = await groupReposity.FindByIdAsync(groupId); var exist = group?.Status == Domain.Enums.GroupState.Normal && 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.Id == member.Id ||
member.Role == Domain.Enums.GroupMemberRole.Master ||
operatorMember.Role <= member.Role)
{
return Result.Fail(ResultCode.PERMISSION_DENIED);
}
member.Leave();
return Result.Success();
}
public async Task<Result<object>> LeaveAsync(Guid groupId, Guid userId)
{
var member = await reposity.FindOneByGroupIdAndUserIdAsync(groupId, userId);
if (member is null)
{
return Result.Fail(ResultCode.GROUP_MEMBER_NOT_FOUNT);
}
if (member.Role == Domain.Enums.GroupMemberRole.Master)
{
return Result.Fail<object>(ResultCode.PERMISSION_DENIED, "群主不能直接退群,请使用解散群接口");
}
member.Leave();
return Result.Success();
}
}
}