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 ClearConversationsAsync(int userId) { await _context.Conversations.Where(x => x.UserId == userId).ExecuteDeleteAsync(); return true; } #endregion #region 获取用户会话列表 public async Task> GetConversationsAsync(int userId) { return await _context.Conversations.Where(x => x.UserId == userId) .ToListAsync(); } #endregion #region 删除单个会话 public async Task 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 } }