ChatMessageRepository.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. using Microsoft.EntityFrameworkCore;
  2. using MTWorkHR.Core.Entities;
  3. using MTWorkHR.Core.IDto;
  4. using MTWorkHR.Infrastructure.Entities;
  5. using MTWorkHR.Infrastructure.DBContext;
  6. using MTWorkHR.Core.IRepositories;
  7. namespace MTWorkHR.Infrastructure.Repositories
  8. {
  9. public class ChatMessageRepository : Repository<ChatMessage>, IChatMessageRepository
  10. {
  11. private readonly DbSet<ChatMessage> dbSet;
  12. public ChatMessageRepository(HRDataContext context) : base(context)
  13. {
  14. dbSet = context.Set<ChatMessage>();
  15. }
  16. public async Task<ChatMessage> GetByIdWithAllChildren(long id)
  17. {
  18. return await dbSet
  19. .Include(x => x.ChatAttachments)
  20. .FirstOrDefaultAsync(x => x.Id == id);
  21. }
  22. public async Task<Tuple<IQueryable<ChatMessage>, int>> GetAllWithChildrenAsync(string userId, string contactId)
  23. {
  24. var query = dbSet.Include(x => x.ChatAttachments).Where(m=> (m.ReceiverId == contactId && m.SenderId == userId) || (m.ReceiverId == userId && m.SenderId == contactId)).AsQueryable();
  25. var total = await query.CountAsync();
  26. return new Tuple<IQueryable<ChatMessage>, int>(query, total);
  27. }
  28. }
  29. }