diff --git a/Govor.API.Tests/IntegrationTests/EF/Repositories/MessagesRepositoryTests.cs b/Govor.API.Tests/IntegrationTests/EF/Repositories/MessagesRepositoryTests.cs index d575179..f20a8f0 100644 --- a/Govor.API.Tests/IntegrationTests/EF/Repositories/MessagesRepositoryTests.cs +++ b/Govor.API.Tests/IntegrationTests/EF/Repositories/MessagesRepositoryTests.cs @@ -3,6 +3,7 @@ using Govor.Core.Infrastructure.Validators; using Govor.Core.Models; using Govor.Data; using Govor.Data.Repositories; +using Govor.Data.Repositories.Exceptions; using Microsoft.EntityFrameworkCore; namespace Govor.API.Tests.IntegrationTests.EF.Repositories; @@ -51,16 +52,42 @@ public class MessagesRepositoryTests // Assert Assert.That(result, Is.Not.Null); Assert.That(result.Count, Is.EqualTo(messages.Count)); - Assert.That(result.Select(u => u.Id), Is.EquivalentTo(messages.Select(u => u.Id))); - Assert.That(result.Select(u => u.SentAt), Is.EquivalentTo(messages.Select(u => u.SentAt))); - Assert.That(result.Select(r => r.SenderId), Is.EquivalentTo(messages.Select(u => u.SenderId))); - Assert.That(result.Select(r => r.RecipientId), Is.EquivalentTo(messages.Select(u => u.RecipientId))); - Assert.That(result.Select(r => r.RecipientType), Is.EquivalentTo(messages.Select(u => u.RecipientType))); + Assert.That(result, Is.EquivalentTo(messages)); } [Test] public void Given_EmptySetDb_When_GetAllMessages_Should_Throw_NotFoundException() { + using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + // Act & Assert + Assert.ThrowsAsync(async () => await messagesRepository.GetAllAsync()); + } + + [Test] + public async Task Given_ValidMessageId_When_FindMessageById_Then_ReturnMessage() + { + var messages = _fixture.CreateMany(10); + var id = messages.First().Id; + await using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + context.Messages.AddRange(messages); + await context.SaveChangesAsync(); + + var result = await messagesRepository.FindByIdAsync(id); + + Assert.That(result, Is.Not.Null); + Assert.That(result, Is.EqualTo(messages.First())); + } + + [Test] + public async Task Given_InvalidMessageId_When_FindById_Should_Throw_NotFoundByKeyException() + { + await using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + Assert.ThrowsAsync>(async () => await messagesRepository.FindByIdAsync(_fixture.Create())); } } \ No newline at end of file diff --git a/Govor.Core/Models/Message.cs b/Govor.Core/Models/Message.cs index 44ccb68..b1bac31 100644 --- a/Govor.Core/Models/Message.cs +++ b/Govor.Core/Models/Message.cs @@ -16,6 +16,21 @@ public class Message public Guid? ReplyToMessageId { get; set; } public Message? ReplyToMessage { get; set; } // navigation + + public override bool Equals(object? obj) + { + if (obj is not Message other) return false; + + return Id == other.Id && + SenderId == other.SenderId && + RecipientId == other.RecipientId && + RecipientType == other.RecipientType && + EncryptedContent == other.EncryptedContent && + SentAt == other.SentAt && + IsEdited == other.IsEdited && + EditedAt == other.EditedAt && + ReplyToMessageId == other.ReplyToMessageId; + } } public enum RecipientType diff --git a/Govor.Core/Repositories/Messages/IMessagesReader.cs b/Govor.Core/Repositories/Messages/IMessagesReader.cs index a5f1abc..623256c 100644 --- a/Govor.Core/Repositories/Messages/IMessagesReader.cs +++ b/Govor.Core/Repositories/Messages/IMessagesReader.cs @@ -8,6 +8,6 @@ public interface IMessagesReader Task FindByIdAsync(Guid messageId); Task> FindBySenderIdAsync(Guid senderId); Task> FindByReceiverIdAsync(Guid receiverId); - Task> FindBySenderAndReceiverIdAsync(Guid senderId, Guid receiverId); + Task> FindBySenderAndReceiverIdAsync(Guid senderId, Guid receiverId, RecipientType recipientType = RecipientType.User); Task> FindBySentAtAsync(DateTime date); } \ No newline at end of file diff --git a/Govor.Data/Repositories/MessagesRepository.cs b/Govor.Data/Repositories/MessagesRepository.cs index 2ed5ee0..091fdbd 100644 --- a/Govor.Data/Repositories/MessagesRepository.cs +++ b/Govor.Data/Repositories/MessagesRepository.cs @@ -22,13 +22,17 @@ public class MessagesRepository : IMessagesRepository { return await _context.Messages .AsNoTracking() - .Where(x => true) - .ToListOrThrowIfEmpty(new NotFoundException("Messages in Database not exists")); + .Include(m => m.ReplyToMessage) + .ToListOrThrowIfEmpty(new NotFoundException("No messages found in the database")); } - public Task FindByIdAsync(Guid messageId) + public async Task FindByIdAsync(Guid messageId) { - throw new NotImplementedException(); + return await _context.Messages + .AsNoTracking() + .Include(m => m.ReplyToMessage) + .FirstOrDefaultAsync(m => m.Id == messageId) + ?? throw new NotFoundByKeyException(messageId, "Message with given id does not exist"); } public Task> FindBySenderIdAsync(Guid senderId) @@ -41,7 +45,7 @@ public class MessagesRepository : IMessagesRepository throw new NotImplementedException(); } - public Task> FindBySenderAndReceiverIdAsync(Guid senderId, Guid receiverId) + public Task> FindBySenderAndReceiverIdAsync(Guid senderId, Guid receiverId, RecipientType recipientType = RecipientType.User) { throw new NotImplementedException(); } diff --git a/Govor.Data/Repositories/UsersRepository.cs b/Govor.Data/Repositories/UsersRepository.cs index 97d5b5a..870a835 100644 --- a/Govor.Data/Repositories/UsersRepository.cs +++ b/Govor.Data/Repositories/UsersRepository.cs @@ -22,7 +22,6 @@ public class UsersRepository : IUsersRepository { return await _context.Users .AsNoTracking() - .Where(x => true) .ToListOrThrowIfEmpty(new NotFoundException("Users in Database not exists")); }