Merge branch 'feature-nxdev' of https://gitea.nxsir.cn/code/IM into feature-nxdev
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using AutoMapper;
|
||||
using IM_API.Domain.Events;
|
||||
using IM_API.Dtos.Group;
|
||||
using IM_API.Exceptions;
|
||||
using IM_API.Interface.Services;
|
||||
using IM_API.Models;
|
||||
using IM_API.Tools;
|
||||
using MassTransit;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
|
||||
namespace IM_API.Services
|
||||
{
|
||||
public class GroupService : IGroupService
|
||||
{
|
||||
private readonly ImContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ILogger<GroupService> _logger;
|
||||
private readonly IPublishEndpoint _endPoint;
|
||||
public GroupService(ImContext context, IMapper mapper, ILogger<GroupService> logger, IPublishEndpoint publishEndpoint)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
_logger = logger;
|
||||
_endPoint = publishEndpoint;
|
||||
}
|
||||
|
||||
private async Task<List<GroupInvite>> GetGroupInvites(int userId, int groupId, List<int> ids)
|
||||
{
|
||||
DateTime dateTime = DateTime.UtcNow;
|
||||
//验证被邀请用户是否为好友
|
||||
var validFriendIds = await _context.Friends
|
||||
.Where(f => f.UserId == userId && ids.Contains(f.FriendId))
|
||||
.Select(f => f.FriendId)
|
||||
.ToListAsync();
|
||||
//创建群成员对象
|
||||
return validFriendIds.Select(fid => new GroupInvite
|
||||
{
|
||||
Created = dateTime,
|
||||
GroupId = groupId,
|
||||
InvitedUser = fid,
|
||||
StateEnum = GroupInviteState.Pending,
|
||||
InviteUser = userId
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public async Task<GroupInfoDto> CreateGroup(int userId, GroupCreateDto groupCreateDto, List<int> userIds)
|
||||
{
|
||||
using var transaction = await _context.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
//先创建群
|
||||
DateTime dateTime = DateTime.UtcNow;
|
||||
Group group = _mapper.Map<Group>(groupCreateDto);
|
||||
group.GroupMaster = userId;
|
||||
_context.Groups.Add(group);
|
||||
await _context.SaveChangesAsync();
|
||||
var groupInvites = new List<GroupInvite>();
|
||||
if (userIds.Count > 0)
|
||||
{
|
||||
groupInvites = await GetGroupInvites(userId,group.Id, userIds);
|
||||
_context.GroupInvites.AddRange(groupInvites);
|
||||
}
|
||||
var groupMember = new GroupMember
|
||||
{
|
||||
UserId = userId,
|
||||
Created = dateTime,
|
||||
RoleEnum = GroupMemberRole.Master,
|
||||
GroupId = group.Id
|
||||
};
|
||||
_context.GroupMembers.Add(groupMember);
|
||||
await _context.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
await _endPoint.Publish(new GroupInviteEvent
|
||||
{
|
||||
AggregateId = userId.ToString(),
|
||||
GroupId = group.Id,
|
||||
EventId = Guid.NewGuid(),
|
||||
OccurredAt = dateTime,
|
||||
Ids = groupInvites.Select(x => x.Id).ToList(),
|
||||
OperatorId = userId,
|
||||
UserId = userId
|
||||
});
|
||||
return _mapper.Map<GroupInfoDto>(group);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await transaction.RollbackAsync();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Task DeleteGroup(int userId, int groupId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task InviteUsers(int userId, int groupId, List<int> userIds)
|
||||
{
|
||||
var group = await _context.Groups.FirstOrDefaultAsync(
|
||||
x => x.Id == groupId) ?? throw new BaseException(CodeDefine.GROUP_NOT_FOUND);
|
||||
|
||||
}
|
||||
|
||||
public Task JoinGroup(int userId, int groupId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user