diff --git a/Govor.API.Tests/IntegrationTests/EF/Repositories/MessagesRepositoryTests.cs b/Govor.API.Tests/IntegrationTests/EF/Repositories/MessagesRepositoryTests.cs index 3a561e6..3c860b2 100644 --- a/Govor.API.Tests/IntegrationTests/EF/Repositories/MessagesRepositoryTests.cs +++ b/Govor.API.Tests/IntegrationTests/EF/Repositories/MessagesRepositoryTests.cs @@ -255,4 +255,143 @@ public class MessagesRepositoryTests // Act & Assert Assert.ThrowsAsync(async () => await messagesRepository.FindBySenderAndReceiverIdAsync(senderId, receiverId, recipientType)); } + + [Test] + public async Task Given_ValidDateTime_When_FindByValidDateTimeAsync_Then_ReturnMessages() + { + // Assert + var messages = _fixture.CreateMany(10).ToList(); + var dateTime = _fixture.Create(); + + foreach (var message in messages) + { + message.SentAt = dateTime; + } + + await using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + context.Messages.AddRange(messages); + await context.SaveChangesAsync(); + + // Act + var results = await messagesRepository.FindBySentAtAsync(dateTime); + + // Assert + Assert.That(results, Is.Not.Null); + Assert.That(results, Is.EquivalentTo(messages)); + } + + [Test] + public async Task Given_ValidMessage_When_AddAsync_Then_MessageAdded() + { + // Arrange + var message = _fixture.Create(); + + await using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + // Act + messagesRepository.AddAsync(message); + + // Assert + Assert.That(context.Messages.Count, Is.EqualTo(1)); + Assert.That(context.Messages.First(), Is.EqualTo(message)); + } + + [Test] + public async Task Given_InvalidMessage_When_AddAsync_Should_Throw_AdditionException() + { + // Arrange + await using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + // Act & Assert + Assert.ThrowsAsync(async () => await messagesRepository.AddAsync(default)); + } + + [Test] + public async Task Given_ExistMessage_When_Exist_Then_ReturnTrue() + { + // Arrange + var message = _fixture.Create(); + + await using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + context.Messages.Add(message); + await context.SaveChangesAsync(); + + // Act + var result = messagesRepository.Exist(message); + var result2 = messagesRepository.Exist(message.Id); + + // Assert + Assert.That(result, Is.True); + Assert.That(result2, Is.True); + } + + [Test] + public async Task Given_NotExistMessage_When_Exist_Then_ReturnFalse() + { + // Arrange + var message = _fixture.Create(); + + await using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + // Act + var result = messagesRepository.Exist(message); + var result2 = messagesRepository.Exist(message.Id); + + // Assert + Assert.That(result, Is.False); + Assert.That(result2, Is.False); + } + + [Test] + public async Task Given_NotEqualMessage_When_Exist_Then_ReturnFalse() + { + // Arrange + var message = _fixture.Create(); + var message2 = _fixture.Create(); + message.Id = message2.Id; + + await using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + context.Messages.Add(message); + await context.SaveChangesAsync(); + + // Act + var result = messagesRepository.Exist(message2); + + // Assert + Assert.That(result, Is.False); + } + + [Test] + public async Task Given_InvalidMessage_When_Exist_Should_Throw_InvalidObjectException() + { + // Arrange + var message = _fixture.Create(); + message.SentAt = DateTime.MinValue; + message.RecipientId = Guid.Empty; + + await using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + // Act & Assert + Assert.Throws>(() => messagesRepository.Exist(message)); + } + + [Test] + public void Given_NullMessage_When_Exist_Should_Throw_InvalidObjectException() + { + using var context = new GovorDbContext(_options); + var messagesRepository = new MessagesRepository(context, _messageValidator); + + // Act & Assert + Assert.Throws>(() => messagesRepository.Exist(default(Message))); + } } \ No newline at end of file diff --git a/Govor.API.Tests/IntegrationTests/EF/Repositories/UsersRepositoryTests.cs b/Govor.API.Tests/IntegrationTests/EF/Repositories/UsersRepositoryTests.cs index ef8b480..d503c74 100644 --- a/Govor.API.Tests/IntegrationTests/EF/Repositories/UsersRepositoryTests.cs +++ b/Govor.API.Tests/IntegrationTests/EF/Repositories/UsersRepositoryTests.cs @@ -272,7 +272,7 @@ public class UsersRepositoryTests var userRepository = new UsersRepository(context, _userValidator); // Act & Assert - Assert.ThrowsAsync(async () => await userRepository.AddAsync(user)); + Assert.ThrowsAsync(async () => await userRepository.AddAsync(user)); } [Test] diff --git a/Govor.Core/Repositories/Messages/IMessagesExist.cs b/Govor.Core/Repositories/Messages/IMessagesExist.cs index 8fbcc96..9bb0833 100644 --- a/Govor.Core/Repositories/Messages/IMessagesExist.cs +++ b/Govor.Core/Repositories/Messages/IMessagesExist.cs @@ -4,5 +4,6 @@ namespace Govor.Core.Repositories.Messages; public interface IMessagesExist { + bool Exist(Guid messageId); bool Exist(Message message); } \ No newline at end of file diff --git a/Govor.Core/Repositories/Messages/IMessagesWriter.cs b/Govor.Core/Repositories/Messages/IMessagesWriter.cs index bf4476e..d316bbf 100644 --- a/Govor.Core/Repositories/Messages/IMessagesWriter.cs +++ b/Govor.Core/Repositories/Messages/IMessagesWriter.cs @@ -4,7 +4,7 @@ namespace Govor.Core.Repositories.Messages; public interface IMessagesWriter { - void Add(Message message); - void Update(Message message); - void Delete(Guid messageId); + Task AddAsync(Message message); + Task UpdateAsync(Message message); + Task RemoveAsync(Guid messageId); } \ No newline at end of file diff --git a/Govor.Data/Repositories/Exceptions/AdditionException.cs b/Govor.Data/Repositories/Exceptions/AdditionException.cs new file mode 100644 index 0000000..d5501f6 --- /dev/null +++ b/Govor.Data/Repositories/Exceptions/AdditionException.cs @@ -0,0 +1,4 @@ +namespace Govor.Data.Repositories.Exceptions; + +public class AdditionException(string s, Exception ex) + : Exception(s, ex); \ No newline at end of file diff --git a/Govor.Data/Repositories/Exceptions/RemoveException.cs b/Govor.Data/Repositories/Exceptions/RemoveException.cs new file mode 100644 index 0000000..7fbb765 --- /dev/null +++ b/Govor.Data/Repositories/Exceptions/RemoveException.cs @@ -0,0 +1,4 @@ +namespace Govor.Data.Repositories.Exceptions; + +public class RemoveException(string s, Exception exception) + : Exception(s, exception); \ No newline at end of file diff --git a/Govor.Data/Repositories/Exceptions/UpdateException.cs b/Govor.Data/Repositories/Exceptions/UpdateException.cs new file mode 100644 index 0000000..25c8a70 --- /dev/null +++ b/Govor.Data/Repositories/Exceptions/UpdateException.cs @@ -0,0 +1,4 @@ +namespace Govor.Data.Repositories.Exceptions; + +public class UpdateException(string s, Exception ex) + : Exception(s, ex); \ No newline at end of file diff --git a/Govor.Data/Repositories/MessagesRepository.cs b/Govor.Data/Repositories/MessagesRepository.cs index 819fb9e..3da8cd6 100644 --- a/Govor.Data/Repositories/MessagesRepository.cs +++ b/Govor.Data/Repositories/MessagesRepository.cs @@ -63,28 +63,108 @@ public class MessagesRepository : IMessagesRepository .ToListOrThrowIfEmpty(new NotFoundException("No messages found in the database")); } - public Task> FindBySentAtAsync(DateTime date) + public async Task> FindBySentAtAsync(DateTime date) { - throw new NotImplementedException(); + return await _context.Messages + .AsNoTracking() + .Include(m => m.ReplyToMessage) + .Where(m => m.SentAt == date) + .ToListOrThrowIfEmpty(new NotFoundByKeyException(date, "Messages sent at date do not exist")); } - public void Add(Message message) + public async Task AddAsync(Message message) { - throw new NotImplementedException(); + try + { + _validator.Validate(message); + + _context.Messages.Add(message); + await _context.SaveChangesAsync(); + } + catch (InvalidObjectException ex) + { + throw new AdditionException("Message with given data invalid", ex); + } + catch (Exception ex) + { + throw new AdditionException("Cannot add message", ex); + } + } + + // TODO: Test this block + public async Task UpdateAsync(Message message) + { + try + { + _validator.Validate(message); + + var rowsAffected = await _context.Messages + .Where(m => m.Id == message.Id) + .ExecuteUpdateAsync(u => u + .SetProperty(m => m.SenderId, message.SenderId) + .SetProperty(m => m.RecipientId, message.RecipientId) + .SetProperty(m => m.SentAt, message.SentAt) + .SetProperty(m => m.RecipientType, message.RecipientType) + .SetProperty(m => m.EncryptedContent, message.EncryptedContent) + .SetProperty(m => m.EditedAt, message.EditedAt) + .SetProperty(m => m.IsEdited, message.IsEdited) + .SetProperty(m => m.Reactions, message.Reactions) + .SetProperty(m => m.ReplyToMessageId, message.ReplyToMessageId) + .SetProperty(m => m.MessageViews, message.MessageViews) + .SetProperty(m => m.MediaAttachments, message.MediaAttachments) + + ); + + if (rowsAffected == 0) + throw new NotFoundByKeyException(message.Id); + } + catch (NotFoundByKeyException ex) + { + throw new UpdateException($"Not found message by given id {message.Id}", ex); + } + catch (Exception ex) + { + throw new UpdateException($"Error when updating the message {message.Id}", ex); + } } - public void Update(Message message) + public async Task RemoveAsync(Guid messageId) { - throw new NotImplementedException(); - } + try + { + var result = await FindByIdAsync(messageId); - public void Delete(Guid messageId) - { - throw new NotImplementedException(); + _context.Messages.Remove(result); + await _context.SaveChangesAsync(); + } + catch (NotFoundByKeyException ex) + { + throw new RemoveException($"Not found message by given id {messageId}", ex); + } + catch (Exception ex) + { + throw new RemoveException("Error when removing the message", ex); + } } public bool Exist(Message message) { - throw new NotImplementedException(); + _validator.Validate(message); + + return _context.Messages.Any( + m => m.Id == message.Id && + m.SenderId == message.SenderId && + m.RecipientId == message.RecipientId && + m.EncryptedContent == message.EncryptedContent && + m.EditedAt == message.EditedAt && + m.IsEdited == message.IsEdited && + m.SentAt == message.SentAt && + m.ReplyToMessageId == message.ReplyToMessageId + ); } -} \ No newline at end of file + + public bool Exist(Guid messageId) + { + return _context.Messages.Any(m => m.Id == messageId); + } +} diff --git a/Govor.Data/Repositories/UsersRepository.cs b/Govor.Data/Repositories/UsersRepository.cs index 870a835..843a1d2 100644 --- a/Govor.Data/Repositories/UsersRepository.cs +++ b/Govor.Data/Repositories/UsersRepository.cs @@ -91,11 +91,11 @@ public class UsersRepository : IUsersRepository } catch (InvalidObjectException ex) { - throw new AdditionUserException("User with given data invalid", ex); + throw new AdditionException("User with given data invalid", ex); } catch (Exception ex) { - throw new AdditionUserException("Cannot add user", ex); + throw new AdditionException("Cannot add user", ex); } } @@ -122,11 +122,11 @@ public class UsersRepository : IUsersRepository } catch (NotFoundByKeyException ex) { - throw new UserUpdateException($"Not found user by given id {user.Id}", ex); + throw new UpdateException($"Not found user by given id {user.Id}", ex); } catch (Exception ex) { - throw new UserUpdateException($"Error when updating the user {user.Id}", ex); + throw new UpdateException($"Error when updating the user {user.Id}", ex); } } @@ -139,7 +139,7 @@ public class UsersRepository : IUsersRepository } catch (InvalidObjectException ex) { - throw new UserRemoveException("User with given data invalid", ex); + throw new RemoveException("User with given data invalid", ex); } } @@ -154,11 +154,11 @@ public class UsersRepository : IUsersRepository } catch (NotFoundByKeyException ex) { - throw new UserRemoveException($"Not found user by given id {userId}", ex); + throw new RemoveException($"Not found user by given id {userId}", ex); } catch (Exception ex) { - throw new UserRemoveException("Error when removing the user", ex); + throw new RemoveException("Error when removing the user", ex); } } @@ -183,14 +183,4 @@ public class UsersRepository : IUsersRepository return _context.Users.AnyAsync(u => u.Username == username); } -} - -public class UserRemoveException(string s, Exception exception) - : Exception(s, exception); - -public class AdditionUserException(string s, Exception ex) - : Exception(s, ex); - -public class UserUpdateException(string s, Exception ex) - : Exception(s, ex); - \ No newline at end of file +} \ No newline at end of file