Files
Govor/Govor.Data/Repositories/MessagesRepository.cs
T
2025-06-22 12:48:48 +07:00

90 lines
3.0 KiB
C#

using Govor.Core.Infrastructure.Extensions;
using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models;
using Govor.Core.Repositories.Messages;
using Govor.Data.Repositories.Exceptions;
using Microsoft.EntityFrameworkCore;
namespace Govor.Data.Repositories;
public class MessagesRepository : IMessagesRepository
{
private GovorDbContext _context;
private IObjectValidator<Message> _validator;
public MessagesRepository(GovorDbContext context, IObjectValidator<Message> validator)
{
_context = context;
_validator = validator;
}
public async Task<List<Message>> GetAllAsync()
{
return await _context.Messages
.AsNoTracking()
.Include(m => m.ReplyToMessage)
.ToListOrThrowIfEmpty(new NotFoundException("No messages found in the database"));
}
public async Task<Message> FindByIdAsync(Guid messageId)
{
return await _context.Messages
.AsNoTracking()
.Include(m => m.ReplyToMessage)
.FirstOrDefaultAsync(m => m.Id == messageId)
?? throw new NotFoundByKeyException<Guid>(messageId, "Message with given id does not exist");
}
public async Task<List<Message>> FindBySenderIdAsync(Guid senderId)
{
return await _context.Messages
.AsNoTracking()
.Include(m => m.ReplyToMessage)
.Where(m => m.SenderId == senderId)
.ToListOrThrowIfEmpty(new NotFoundByKeyException<Guid>(senderId, "Messages with given sender id do not exist"));
}
public async Task<List<Message>> FindByReceiverIdAsync(Guid receiverId)
{
return await _context.Messages
.AsNoTracking()
.Include(m => m.ReplyToMessage)
.Where(m => m.RecipientId == receiverId)
.ToListOrThrowIfEmpty(new NotFoundByKeyException<Guid>(receiverId, "Messages with given recipient id do not exist"));
}
public async Task<List<Message>> FindBySenderAndReceiverIdAsync(Guid senderId, Guid receiverId, RecipientType recipientType = RecipientType.User)
{
return await _context.Messages
.AsNoTracking()
.Include(m => m.ReplyToMessage)
.Where(m => m.SenderId == senderId
&& m.RecipientId == receiverId
&& m.RecipientType == recipientType)
.ToListOrThrowIfEmpty(new NotFoundException("No messages found in the database"));
}
public Task<List<Message>> FindBySentAtAsync(DateTime date)
{
throw new NotImplementedException();
}
public void Add(Message message)
{
throw new NotImplementedException();
}
public void Update(Message message)
{
throw new NotImplementedException();
}
public void Delete(Guid messageId)
{
throw new NotImplementedException();
}
public bool Exist(Message message)
{
throw new NotImplementedException();
}
}