Add friendship feature with models, repository, and tests

Introduces Friendship and PrivateChat models, repository interfaces, and implementations for managing friendships. Adds validators and comprehensive unit and integration tests for friendship logic. Updates InviteUserController with new endpoints and restricts access to admin role. Removes unnecessary project references from Govor.API.csproj and registers new DbSets in GovorDbContext.
This commit is contained in:
Artemy
2025-06-27 15:09:28 +07:00
parent 4f3f4ec066
commit 9b9cd73a96
15 changed files with 565 additions and 20 deletions
@@ -0,0 +1,38 @@
using Govor.Core.Models;
namespace Govor.Core.Infrastructure.Validators;
public class FriendshipValidator : IObjectValidator<Friendship>
{
public void Validate(Friendship friendship)
{
try
{
if(friendship is null)
throw new ArgumentNullException(nameof(friendship));
if(friendship.Id == Guid.Empty)
throw new ArgumentException("Id cannot be empty", nameof(friendship.Id));
if(friendship.AddresseeId == Guid.Empty)
throw new ArgumentException("Addresses cannot be empty", nameof(friendship.AddresseeId));
if(friendship.RequesterId == Guid.Empty)
throw new ArgumentException("Requester cannot be empty", nameof(friendship.RequesterId));
}
catch (Exception e)
{
throw new InvalidObjectException<Friendship>(e);
}
}
public bool TryValidate(Friendship friendship)
{
try
{
Validate(friendship);
return true;
}
catch
{
return false;
}
}
}
+20
View File
@@ -0,0 +1,20 @@
namespace Govor.Core.Models;
public class Friendship
{
public Guid Id { get; set; }
public Guid RequesterId { get; set; }
public Guid AddresseeId { get; set; }
public FriendshipStatus Status { get; set; }
public User Requester { get; set; }
public User Addressee { get; set; }
}
public enum FriendshipStatus
{
Pending,
Accepted,
Rejected,
Blocked
}
+11
View File
@@ -0,0 +1,11 @@
namespace Govor.Core.Models;
public class PrivateChat
{
public Guid Id { get; set; }
public Guid UserAId { get; set; }
public Guid UserBId { get; set; }
public User UserA { get; set; }
public User UserB { get; set; }
public List<Message> Messages { get; set; } = new List<Message>();
}
@@ -0,0 +1,6 @@
namespace Govor.Core.Repositories.Friendships;
public interface IFriendshipsExist
{
bool Exist(Guid requesterId, Guid addresseeId);
}
@@ -0,0 +1,10 @@
using Govor.Core.Models;
namespace Govor.Core.Repositories.Friendships;
public interface IFriendshipsReader
{
Task<List<Friendship>> GetAllAsync();
Task<Friendship> GetByIdAsync(Guid id);
Task<List<Friendship>> FindByUserIdAsync(Guid userId);
}
@@ -0,0 +1,6 @@
namespace Govor.Core.Repositories.Friendships;
public interface IFriendshipsRepository : IFriendshipsReader, IFriendshipsWriter, IFriendshipsExist
{
}
@@ -0,0 +1,10 @@
using Govor.Core.Models;
namespace Govor.Core.Repositories.Friendships;
public interface IFriendshipsWriter
{
Task AddAsync(Friendship friendship);
Task UpdateAsync(Friendship friendship);
Task RemoveAsync(Friendship friendship);
}