add(conversationSerivice):后端新增消息会话服务add(main):完善主面板

This commit is contained in:
2025-12-29 20:39:29 +08:00
parent 06410a75e7
commit cf09984056
21 changed files with 1146 additions and 444 deletions
@@ -0,0 +1,44 @@
using IM_API.Dtos;
using IM_API.Exceptions;
using IM_API.Interface.Services;
using IM_API.Models;
using IM_API.Tools;
using Microsoft.EntityFrameworkCore;
namespace IM_API.Services
{
public class ConversationService : IConversationService
{
private readonly ImContext _context;
public ConversationService(ImContext context)
{
_context = context;
}
#region
public async Task<bool> ClearConversationsAsync(int userId)
{
await _context.Conversations.Where(x => x.UserId == userId).ExecuteDeleteAsync();
return true;
}
#endregion
#region
public async Task<List<Conversation>> GetConversationsAsync(int userId)
{
return await _context.Conversations.Where(x => x.UserId == userId)
.ToListAsync();
}
#endregion
#region
public async Task<bool> DeleteConversationAsync(int conversationId)
{
var conversation = await _context.Conversations.FirstOrDefaultAsync(x => x.Id == conversationId);
if (conversation == null) throw new BaseException(CodeDefine.CONVERSATION_NOT_FOUND);
_context.Conversations.Remove(conversation);
await _context.SaveChangesAsync();
return true;
}
#endregion
}
}
+1 -1
View File
@@ -68,7 +68,7 @@ namespace IM_API.Services
{
query = query.OrderByDescending(x => x.UserId);
}
var friendList = await query.Skip((page - 1 * limit)).Take(limit).ToListAsync();
var friendList = await query.Skip(((page - 1) * limit)).Take(limit).ToListAsync();
return _mapper.Map<List<FriendInfoDto>>(friendList);
}
#endregion
+17 -4
View File
@@ -54,12 +54,24 @@ namespace IM_API.Services
{
throw new NotImplementedException();
}
public Task<bool> SendGroupMessageAsync(int senderId, int groupId, MessageBaseDto dto)
#region
public async Task<bool> SendGroupMessageAsync(int senderId, int groupId, MessageBaseDto dto)
{
throw new NotImplementedException();
}
//判断群存在
var isExist = await _context.Groups.AnyAsync(x => x.Id == groupId);
if (!isExist) throw new BaseException(CodeDefine.GROUP_NOT_FOUND);
//判断是否是群成员
var isMember = await _context.GroupMembers.AnyAsync(x => x.GroupId == groupId && x.UserId == senderId);
if (!isMember) throw new BaseException(CodeDefine.NO_GROUP_PERMISSION);
var message = _mapper.Map<Message>(dto);
message.Sender = senderId;
_context.Messages.Add(message);
await _context.SaveChangesAsync();
return true;
}
#endregion
#region
public async Task<bool> SendPrivateMessageAsync(int senderId, int receiverId, MessageBaseDto dto)
{
bool isExist = await _context.Friends.AnyAsync(x => x.FriendId == receiverId);
@@ -70,5 +82,6 @@ namespace IM_API.Services
await _context.SaveChangesAsync();
return true;
}
#endregion
}
}