Messages first init

This commit is contained in:
Artemy
2025-06-20 13:32:29 +07:00
parent efcd970e90
commit 3526cde06f
10 changed files with 186 additions and 0 deletions
@@ -0,0 +1,7 @@
namespace Govor.API.Tests.UnitTests.Infrastructure.Validators;
[TestFixture]
public class MessageValidatorTests
{
}
+5
View File
@@ -42,6 +42,11 @@ public class ChatsHub : Hub
} }
await base.OnDisconnectedAsync(exception); await base.OnDisconnectedAsync(exception);
} }
public async Task Edit(string newMessage, Guid messageId)
{
}
public async Task Send(string message, Guid toUserId) public async Task Send(string message, Guid toUserId)
{ {
@@ -0,0 +1,44 @@
using Govor.Core.Models;
namespace Govor.Core.Infrastructure.Validators;
public class MessageValidator : IObjectValidator<Message>
{
public void Validate(Message message)
{
try
{
if (message is null)
throw new ArgumentNullException(nameof(message));
if (message.Id == Guid.Empty)
throw new ArgumentException("Message ID cannot be empty", nameof(message.Id));
if (message.SenderId == Guid.Empty)
throw new ArgumentException("Sender ID cannot be empty", nameof(message.SenderId));
if (message.RecipientId == Guid.Empty)
throw new ArgumentException("Recipient ID cannot be empty", nameof(message.RecipientId));
if(string.IsNullOrWhiteSpace(message.EncryptedContent))
throw new ArgumentException("Encrypted content cannot be empty", nameof(message.EncryptedContent));
if(message.IsEdited && message.EditedAt == DateTime.MinValue)
throw new ArgumentException("Edited at time cannot be empty", nameof(message.EditedAt));
if (message.EditedAt == DateTime.MinValue)
throw new ArgumentException("Edited at time cannot be empty", nameof(message.EditedAt));
}
catch (Exception ex)
{
throw new InvalidObjectException<Message>(ex);
}
}
public bool TryValidate(Message message)
{
try
{
Validate(message);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
+19
View File
@@ -0,0 +1,19 @@
namespace Govor.Core.Models;
public class Message
{
public Guid Id { get; set; }
public Guid SenderId { get; set; }
public Guid RecipientId { get; set; } // or GroupId
public RecipientType RecipientType { get; set; }
public string EncryptedContent { get; set; } = string.Empty;
public DateTime SentAt { get; set; }
public bool IsEdited { get; set; } = false;
public DateTime? EditedAt { get; set; }
}
public enum RecipientType
{
User,
Group
}
@@ -0,0 +1,8 @@
using Govor.Core.Models;
namespace Govor.Core.Repositories.Messages;
public interface IMessagesExist
{
bool Exist(Message message);
}
@@ -0,0 +1,13 @@
using Govor.Core.Models;
namespace Govor.Core.Repositories.Messages;
public interface IMessagesReader
{
Task<List<Message>> GetAllAsync();
Task<Message> FindByIdAsync(Guid messageId);
Task<List<Message>> FindBySenderIdAsync(Guid senderId);
Task<List<Message>> FindByReceiverIdAsync(Guid receiverId);
Task<List<Message>> FindBySenderAndReceiverIdAsync(Guid senderId, Guid receiverId);
Task<List<Message>> FindBySentAtAsync(DateTime date);
}
@@ -0,0 +1,6 @@
namespace Govor.Core.Repositories.Messages;
public interface IMessagesRepository : IMessagesReader, IMessagesWriter, IMessagesExist
{
}
@@ -0,0 +1,10 @@
using Govor.Core.Models;
namespace Govor.Core.Repositories.Messages;
public interface IMessagesWriter
{
void Add(Message message);
void Update(Message message);
void Delete(Guid messageId);
}
+1
View File
@@ -8,6 +8,7 @@ public class GovorDbContext(DbContextOptions<GovorDbContext> options) : DbContex
{ {
public virtual DbSet<User> Users { get; set; } public virtual DbSet<User> Users { get; set; }
public virtual DbSet<ChatGroup> ChatGroups { get; set; } public virtual DbSet<ChatGroup> ChatGroups { get; set; }
public virtual DbSet<Message> Messages { get; set; }
public virtual DbSet<GroupMembership> GroupMemberships { get; set; } public virtual DbSet<GroupMembership> GroupMemberships { get; set; }
public virtual DbSet<GroupAdmins> GroupAdmins { get; set; } public virtual DbSet<GroupAdmins> GroupAdmins { get; set; }
} }
@@ -0,0 +1,73 @@
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()
.Where(x => true)
.ToListOrThrowIfEmpty(new NotFoundException("Messages in Database not exists"));
}
public Task<Message> FindByIdAsync(Guid messageId)
{
throw new NotImplementedException();
}
public Task<List<Message>> FindBySenderIdAsync(Guid senderId)
{
throw new NotImplementedException();
}
public Task<List<Message>> FindByReceiverIdAsync(Guid receiverId)
{
throw new NotImplementedException();
}
public Task<List<Message>> FindBySenderAndReceiverIdAsync(Guid senderId, Guid receiverId)
{
throw new NotImplementedException();
}
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();
}
}