mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
Finished MessageRepository
+ Tests
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
namespace Govor.Data.Repositories.Exceptions;
|
||||
|
||||
public class AdditionException(string s, Exception ex)
|
||||
: Exception(s, ex);
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace Govor.Data.Repositories.Exceptions;
|
||||
|
||||
public class RemoveException(string s, Exception exception)
|
||||
: Exception(s, exception);
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace Govor.Data.Repositories.Exceptions;
|
||||
|
||||
public class UpdateException(string s, Exception ex)
|
||||
: Exception(s, ex);
|
||||
@@ -63,28 +63,108 @@ public class MessagesRepository : IMessagesRepository
|
||||
.ToListOrThrowIfEmpty(new NotFoundException("No messages found in the database"));
|
||||
}
|
||||
|
||||
public Task<List<Message>> FindBySentAtAsync(DateTime date)
|
||||
public async Task<List<Message>> FindBySentAtAsync(DateTime date)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return await _context.Messages
|
||||
.AsNoTracking()
|
||||
.Include(m => m.ReplyToMessage)
|
||||
.Where(m => m.SentAt == date)
|
||||
.ToListOrThrowIfEmpty(new NotFoundByKeyException<DateTime>(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<Message> 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<Guid>(message.Id);
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> 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<Guid> 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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Exist(Guid messageId)
|
||||
{
|
||||
return _context.Messages.Any(m => m.Id == messageId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,11 +91,11 @@ public class UsersRepository : IUsersRepository
|
||||
}
|
||||
catch (InvalidObjectException<User> 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<Guid> 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<User> 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<Guid> 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);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user