添加项目文件。
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
namespace GroupService.WebApi.Application.GroupInvitation
|
||||
{
|
||||
public record GroupInvitationHandleCommand(Guid InvitationId, Guid UserId, GroupInvitationAction Action);
|
||||
public enum GroupInvitationAction
|
||||
{
|
||||
Accept = 0,
|
||||
Reject = 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using AutoMapper;
|
||||
using GroupService.WebApi.Application.Dtos;
|
||||
|
||||
namespace GroupService.WebApi.Application.GroupInvitation
|
||||
{
|
||||
public class GroupInvitationMapperConfig : Profile
|
||||
{
|
||||
public GroupInvitationMapperConfig()
|
||||
{
|
||||
CreateMap<Domain.Entities.GroupInvitation, GroupInvitationResponse>()
|
||||
.ForMember(dest => dest.Created, opt => opt.MapFrom(src => src.CreationTime))
|
||||
.ForMember(dest => dest.Updated, opt => opt.MapFrom(src => src.ModificationTime))
|
||||
.ForMember(dest => dest.GroupAvatar, opt => opt.MapFrom(src => src.GroupProfile.Avatar))
|
||||
.ForMember(dest => dest.GroupName, opt => opt.MapFrom(src => src.GroupProfile.GroupName))
|
||||
.ForMember(dest => dest.UserNickName, opt => opt.MapFrom(src => src.UserProfile.NickName))
|
||||
.ForMember(dest => dest.UserAvatar, opt => opt.MapFrom(src => src.UserProfile.Avatar))
|
||||
.ForMember(dest => dest.OperatorAvatar, opt => opt.MapFrom(src => src.OperatorProfile.Avatar))
|
||||
.ForMember(dest => dest.OperatorName, opt => opt.MapFrom(src => src.OperatorProfile.NickName));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user