rework and starts for new friends system

This commit is contained in:
Artemy
2025-07-08 22:28:04 +07:00
parent 92c1ff6458
commit b1f3aa0266
29 changed files with 1028 additions and 309 deletions
+2 -2
View File
@@ -55,7 +55,7 @@ public class AuthService : IAccountService
await _usersRepository.AddAsync(user);
SetRole(user, invitation);
await SetRole(user, invitation);
return await _jwtService.GenerateJwtTokenAsync(user);
}
@@ -74,7 +74,7 @@ public class AuthService : IAccountService
return await _jwtService.GenerateJwtTokenAsync(user);
}
private async void SetRole(User user, Invitation invitation)
private async Task SetRole(User user, Invitation invitation)
{
if(invitation.IsAdmin)
await _adminsRepository.AddAsync(new Admin() { UserId = user.Id });
@@ -1,59 +1,29 @@
using Govor.Application.Exceptions.FriendsService;
using Govor.Application.Interfaces;
using Govor.Application.Interfaces.Friends;
using Govor.Core.Models;
using Govor.Core.Repositories.Friendships;
using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions;
namespace Govor.Application.Services;
namespace Govor.Application.Services.Friends;
public class FriendsService : IFriendsService
public class FriendRequestService : IFriendRequestService
{
private IUsersRepository _usersRepository;
private IFriendshipsRepository _friendshipsRepository;
public FriendsService(IUsersRepository usersRepository, IFriendshipsRepository relationshipsRepository)
{
_usersRepository = usersRepository;
_friendshipsRepository = relationshipsRepository;
}
public async Task<List<User>> SearchUsersAsync(string query, Guid currentId)
{
List<User> all = new List<User>();
try
{
all = await _usersRepository.SearchPotentialFriendsAsync(currentId, query);
private readonly IFriendshipsRepository _friendshipsRepository;
return all
.Where(u => u.Id != currentId)
.ToList();
}
catch (NotFoundByKeyException<(string, Guid)> ex)
{
throw new SearchUsersException(
$"Users with given query: \"{query}\" for user with id {currentId} was not found", ex);
}
catch (NotFoundByKeyException<Guid> ex)
{
return all.Where(u => u.Id != currentId).ToList();
}
catch (Exception ex)
{
throw new UnauthorizedAccessException($"When we try find friends by pattern {query} something wrong", ex);
}
public FriendRequestService(IFriendshipsRepository friendshipsRepository)
{
_friendshipsRepository = friendshipsRepository;
}
public async Task SendFriendRequestAsync(Guid fromUserId, Guid toUserId)
{
if (fromUserId == toUserId)
throw new InvalidOperationException("Cannot send a request to self user");
if (_friendshipsRepository.Exist(fromUserId, toUserId))
throw new RequestAlreadySentException(fromUserId, toUserId);
await _friendshipsRepository.AddAsync(new Friendship()
await _friendshipsRepository.AddAsync(new Friendship
{
Id = Guid.NewGuid(),
RequesterId = fromUserId,
@@ -77,26 +47,30 @@ public class FriendsService : IFriendsService
friendship.Status = FriendshipStatus.Accepted;
await _friendshipsRepository.UpdateAsync(friendship);
}
catch (NotFoundByKeyException<Guid> e)
catch (NotFoundByKeyException<Guid> ex)
{
throw new InvalidOperationException("Friendship not found! You cant accept request!", e);
throw new InvalidOperationException("Friendship not found! You cant accept request!", ex);
}
}
public async Task<List<User>> GetFriendsAsync(Guid userId)
public async Task RejectFriendRequestAsync(Guid requestId, Guid currentUserId)
{
try
{
var friendships = await _friendshipsRepository.FindByUserIdAsync(userId);
var friendship = await _friendshipsRepository.GetByIdAsync(requestId);
return friendships
.Where(f => f.Status == FriendshipStatus.Accepted)
.Select(f => f.RequesterId == userId ? f.Addressee : f.Requester)
.ToList();
if (friendship.AddresseeId != currentUserId)
throw new UnauthorizedAccessException("You cannot accept this request");
if (friendship.Status != FriendshipStatus.Pending && friendship.Status != FriendshipStatus.Rejected)
throw new InvalidOperationException($"Request is already {friendship.Status}");
friendship.Status = FriendshipStatus.Rejected;
await _friendshipsRepository.UpdateAsync(friendship);
}
catch (NotFoundByKeyException<Guid> ex)
{
throw new InvalidOperationException("User not found", ex);
throw new InvalidOperationException("Friendship not found! You cant reject request!", ex);
}
}
@@ -113,5 +87,18 @@ public class FriendsService : IFriendsService
throw new InvalidOperationException("User not exist", ex);
}
}
}
public async Task<List<Friendship>> GetResponsesAsync(Guid userId)
{
try
{
var friendships = await _friendshipsRepository.FindByUserIdAsync(userId);
return friendships.Where(f => f.RequesterId == userId && f.Status != FriendshipStatus.Accepted).ToList()
?? new List<Friendship>();
}
catch (NotFoundByKeyException<Guid> ex)
{
throw new InvalidOperationException("User not exist", ex);
}
}
}
@@ -0,0 +1,16 @@
using Govor.Application.Interfaces;
namespace Govor.Application.Services.Friends;
public class FriendsBlockService : IFriendsBlockService
{
public Task BlockFriendRequestAsync(Guid userId, Guid currentUserId)
{
throw new NotImplementedException();
}
public Task UnblockFriendRequestAsync(Guid userId, Guid currentUserId)
{
throw new NotImplementedException();
}
}
@@ -0,0 +1,64 @@
using Govor.Application.Exceptions.FriendsService;
using Govor.Application.Interfaces.Friends;
using Govor.Core.Models;
using Govor.Core.Repositories.Friendships;
using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions;
namespace Govor.Application.Services.Friends;
public class FriendshipService : IFriendshipService
{
private readonly IUsersRepository _usersRepository;
private readonly IFriendshipsRepository _friendshipsRepository;
public FriendshipService(IUsersRepository usersRepository, IFriendshipsRepository friendshipsRepository)
{
_usersRepository = usersRepository;
_friendshipsRepository = friendshipsRepository;
}
public async Task<List<User>> SearchUsersAsync(string query, Guid currentId)
{
List<User> all = new List<User>();
try
{
all = await _usersRepository.SearchPotentialFriendsAsync(currentId, query);
return all
.Where(u => u.Id != currentId)
.ToList();
}
catch (NotFoundByKeyException<(string, Guid)> ex)
{
throw new SearchUsersException(
$"Users with given query: \"{query}\" for user with id {currentId} was not found", ex);
}
catch (NotFoundByKeyException<Guid> ex)
{
return all.Where(u => u.Id != currentId).ToList();
}
catch (Exception ex)
{
throw new UnauthorizedAccessException($"When we try find friends by pattern {query} something wrong", ex);
}
}
public async Task<List<User>> GetFriendsAsync(Guid userId)
{
try
{
var friendships = await _friendshipsRepository.FindByUserIdAsync(userId);
return friendships
.Where(f => f.Status == FriendshipStatus.Accepted)
.Select(f => f.RequesterId == userId ? f.Addressee : f.Requester)
.ToList();
}
catch (NotFoundByKeyException<Guid> ex)
{
throw new InvalidOperationException("User not found", ex);
}
}
}
@@ -0,0 +1,43 @@
using Govor.Application.Interfaces;
using Govor.Core.Models;
using Govor.Core.Repositories.Groups;
using Govor.Core.Repositories.Messages;
using Govor.Data.Repositories.Exceptions;
namespace Govor.Application.Services;
public class MessagesLoader : IMessagesLoader
{
private IVerifyFriendship _friendship;
private IGroupsRepository _groupsRepository;
private IMessagesRepository _messagesRepository;
public async Task<List<Message>> LoadLastMessagesInUserChat(Guid userId, Guid currentUser, Guid? startMessageId, int pageSize = 20)
{
await _friendship.VerifyAsync(userId, currentUser);
try
{
throw new NotImplementedException();
//return await _groups.GetMessages(userId, startMessageId, pageSize);
}
catch (NotFoundException ex)
{
return new List<Message>();
}
}
public async Task<List<Message>> LoadLastMessagesInChatGroup(Guid chatId, Guid currentUser, Guid? startMessageId, int pageSize = 20)
{
if(!await _groupsRepository.IsUserMemberOfGroupAsync(currentUser, chatId))
throw new UnauthorizedAccessException("You are not in a group.");
try
{
throw new NotImplementedException();
//return await _groups.GetMessages(chatId, startMessageId, pageSize, RecipientType.Group);
}
catch (NotFoundException ex)
{
return new List<Message>();
}
}
}