Add private chat support and group model enhancements

Introduces repositories, validators, and integration tests for private chats. Refactors group-related models to support invitations and memberships, updates group repository interfaces and implementations, and enhances message service logic to handle private chat creation and retrieval. Also registers new services and validators in DI, and updates related tests.
This commit is contained in:
Artemy
2025-07-07 13:55:29 +07:00
parent 66819a015a
commit 2cb6a93060
21 changed files with 454 additions and 54 deletions
@@ -0,0 +1,38 @@
using Govor.Core.Models;
namespace Govor.Core.Infrastructure.Validators;
public class PrivateChatValidator : IObjectValidator<PrivateChat>
{
public void Validate(PrivateChat chat)
{
try
{
if(chat is null)
throw new ArgumentNullException(nameof(chat));
if(chat.Id == Guid.Empty)
throw new ArgumentException("Id cannot be empty", nameof(chat));
if(chat.UserAId == Guid.Empty)
throw new ArgumentException("UserAId cannot be empty", nameof(chat.UserAId));
if(chat.UserBId == Guid.Empty)
throw new ArgumentException("UserBId cannot be empty", nameof(chat.UserBId));
}
catch (Exception ex)
{
throw new InvalidObjectException<PrivateChat>(ex);
}
}
public bool TryValidate(PrivateChat chat)
{
try
{
Validate(chat);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
+3 -3
View File
@@ -6,9 +6,9 @@ public class ChatGroup
public string Name { get; set; }
public string Description { get; set; }
public Guid ImageId { get; set; }
public List<string> InviteCode { get; set; }
public bool IsChannel { get; set; }
public bool IsPrivate { get; set; }
public List<Guid> Admins { get; set; } = new();
public List<GroupAdmins> Admins { get; set; } = new();
public List<GroupMembership> Members { get; set; } = new();
public List<GroupInvitation> InviteCodes { get; set; } = new();
}
+15
View File
@@ -0,0 +1,15 @@
namespace Govor.Core.Models;
public class GroupInvitation
{
public Guid Id { get; set; }
public Guid GroupId { get; set; }
public Guid UserMakerId { get; set; }
public string InvitationCode { get; set; }
public string Description {get; set;}
public DateTime EndDate { get; set; }
public DateTime CreatedAt { get; set; }
public int MaxParticipants { get; set; }
public List<Guid> GroupMemberships { get; set; } = new();
public User? UserMaker { get; set; }
}
+1
View File
@@ -5,5 +5,6 @@ public class GroupMembership
public Guid Id { get; set; }
public Guid GroupId { get; set; }
public Guid UserId { get; set; }
public Guid InvitationId { get; set; }
public bool IsBanned { get; set; }
}
@@ -0,0 +1,8 @@
using Govor.Core.Models;
namespace Govor.Core.Repositories.Groups;
public interface IGroupMessagesReader
{
public Task<IEnumerable<Message>> GetMessages(Guid chatId, Guid? startMessageId, int pageSize = 20);
}
@@ -1,13 +1,13 @@
using System.Text.RegularExpressions;
using Govor.Core.Models;
namespace Govor.Core.Repositories.Groups;
public interface IGroupsReader
{
public Task<List<Group>> GetAllAsync();
public Task<Group> GetByIdAsync(Guid id);
public Task<List<Group>> FindByNameAsync(string name);
public Task<List<Group>> GetByAdminIdAsync(Guid adminId);
public Task<List<Group>> GetByUserIdAsync(Guid adminId);
public bool IsUserMemberOfGroupAsync(Guid userId, Guid groupId);
public Task<List<ChatGroup>> GetAllAsync();
public Task<ChatGroup> GetByIdAsync(Guid id);
public Task<List<ChatGroup>> SearchByNameAsync(string name);
public Task<List<ChatGroup>> GetByAdminIdAsync(Guid userId);
public Task<List<ChatGroup>> GetByUserIdAsync(Guid userId);
public Task<bool> IsUserMemberOfGroupAsync(Guid userId, Guid groupId);
}
@@ -1,10 +1,10 @@
using System.Text.RegularExpressions;
using Govor.Core.Models;
namespace Govor.Core.Repositories.Groups;
public interface IGroupsWriter
{
Task Add(Group group);
Task Update(Group group);
Task Remove(Guid groupId);
Task AddAsync(ChatGroup group);
Task UpdateAsync(ChatGroup group);
Task RemoveAsync(Guid groupId);
}
@@ -0,0 +1,7 @@
namespace Govor.Core.Repositories.PrivateChats;
public interface IPrivateChatsExist
{
bool Exist(Guid chatId);
bool Exist(Guid userAId, Guid userBId);
}
@@ -0,0 +1,10 @@
using Govor.Core.Models;
namespace Govor.Core.Repositories.PrivateChats;
public interface IPrivateChatsReader
{
Task<List<PrivateChat>> GetAllAsync();
Task<PrivateChat> GetByIdAsync(Guid id);
Task<PrivateChat> GetByMembersAsync(Guid memberAId, Guid memberBId);
}
@@ -0,0 +1,6 @@
namespace Govor.Core.Repositories.PrivateChats;
public interface IPrivateChatsRepository : IPrivateChatsReader, IPrivateChatsWriter, IPrivateChatsExist
{
}
@@ -0,0 +1,10 @@
using Govor.Core.Models;
namespace Govor.Core.Repositories.PrivateChats;
public interface IPrivateChatsWriter
{
Task AddAsync(PrivateChat chat);
Task UpdateAsync(PrivateChat chat);
Task RemoveAsync(Guid chatId);
}