102 lines
3.7 KiB
C#
102 lines
3.7 KiB
C#
using AutoMapper;
|
|
using GroupService.Domain.IReposities;
|
|
using GroupService.Domain.ValueObjects;
|
|
using GroupService.WebApi.Application.Dtos;
|
|
using GroupService.WebApi.Application.IntegrationServices;
|
|
using IM.Commons;
|
|
|
|
namespace GroupService.WebApi.Application.GroupInvitation
|
|
{
|
|
public class GroupInvitationService
|
|
{
|
|
private readonly IGroupInvitationReposity reposity;
|
|
private readonly IGroupMemberReposity memberReposity;
|
|
private readonly IIdentityIntegrationService idService;
|
|
private readonly IGroupReposity groupReposity;
|
|
private readonly IMapper mapper;
|
|
|
|
public GroupInvitationService(IGroupInvitationReposity reposity, IGroupMemberReposity memberReposity, IIdentityIntegrationService idService, IGroupReposity groupReposity, IMapper mapper)
|
|
{
|
|
this.reposity = reposity;
|
|
this.memberReposity = memberReposity;
|
|
this.idService = idService;
|
|
this.groupReposity = groupReposity;
|
|
this.mapper = mapper;
|
|
}
|
|
|
|
public async Task<Result<GroupInvitationResponse>> CreateAsync(Guid operatorId, Guid userId, Guid groupId)
|
|
{
|
|
var userRes = await idService.FindUserByIdAsync(userId);
|
|
if (!userRes.Succeeded)
|
|
{
|
|
return Result<GroupInvitationResponse>.Fail(userRes);
|
|
}
|
|
var operatorInfo = await idService.FindUserByIdAsync(operatorId);
|
|
|
|
var member = await memberReposity.FindOneByGroupIdAndUserIdAsync(groupId, operatorId);
|
|
if (member == null)
|
|
{
|
|
return Result.Fail<GroupInvitationResponse>(ResultCode.PERMISSION_DENIED);
|
|
}
|
|
|
|
var group = await groupReposity.FindByIdAsync(groupId);
|
|
|
|
var userProfile = new UserProfile()
|
|
{
|
|
Avatar = userRes.Data.Avatar,
|
|
NickName = userRes.Data.NickName
|
|
};
|
|
|
|
var operatorProfile = new UserProfile()
|
|
{
|
|
Avatar = operatorInfo.Data.Avatar,
|
|
NickName = operatorInfo.Data.NickName
|
|
};
|
|
|
|
var groupProfile = new GroupProfile()
|
|
{
|
|
Avatar = group.Avatar,
|
|
GroupName = group.Name
|
|
};
|
|
|
|
var invitation = new Domain.Entities.GroupInvitation(groupId, groupProfile, userId, userProfile
|
|
, operatorId, operatorProfile);
|
|
reposity.Create(invitation);
|
|
|
|
return Result.Success(mapper.Map<GroupInvitationResponse>(invitation));
|
|
}
|
|
|
|
public async Task<Result<object>> HandleAsync(GroupInvitationHandleCommand command)
|
|
{
|
|
var invitation = await reposity.FindByIdAsync(command.InvitationId);
|
|
if (invitation is null || invitation.UserId != command.UserId)
|
|
{
|
|
return Result.Fail(ResultCode.GROUP_INVITE_EXPIRED);
|
|
}
|
|
|
|
if (command.Action == GroupInvitationAction.Accept)
|
|
{
|
|
invitation.Accept();
|
|
}
|
|
else if (command.Action == GroupInvitationAction.Reject)
|
|
{
|
|
invitation.Reject();
|
|
}
|
|
|
|
return Result.Success();
|
|
}
|
|
|
|
public async Task<Result<GroupInvitationResponse>> GetByIdAsync(Guid id, Guid userId)
|
|
{
|
|
var invitation = await reposity.FindByIdAsync(id);
|
|
|
|
if (invitation is null || (invitation.UserId != userId && invitation.OperatorId != userId))
|
|
{
|
|
return Result.Fail<GroupInvitationResponse>(ResultCode.GROUP_INVITE_EXPIRED);
|
|
}
|
|
|
|
return Result.Success(mapper.Map<GroupInvitationResponse>(invitation));
|
|
}
|
|
}
|
|
}
|