mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
rework and starts for new friends system
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
using Govor.Application.Exceptions.FriendsService;
|
||||
using Govor.Application.Interfaces.Friends;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Repositories.Friendships;
|
||||
using Govor.Data.Repositories.Exceptions;
|
||||
|
||||
namespace Govor.Application.Services.Friends;
|
||||
|
||||
public class FriendRequestService : IFriendRequestService
|
||||
{
|
||||
private readonly IFriendshipsRepository _friendshipsRepository;
|
||||
|
||||
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
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RequesterId = fromUserId,
|
||||
AddresseeId = toUserId,
|
||||
Status = FriendshipStatus.Pending
|
||||
});
|
||||
}
|
||||
|
||||
public async Task AcceptFriendRequestAsync(Guid requestId, Guid currentUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var friendship = await _friendshipsRepository.GetByIdAsync(requestId);
|
||||
|
||||
if (friendship.AddresseeId != currentUserId)
|
||||
throw new UnauthorizedAccessException("You cannot accept this request");
|
||||
|
||||
if (friendship.Status != FriendshipStatus.Pending)
|
||||
throw new InvalidOperationException("Request is already accepted");
|
||||
|
||||
friendship.Status = FriendshipStatus.Accepted;
|
||||
await _friendshipsRepository.UpdateAsync(friendship);
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
throw new InvalidOperationException("Friendship not found! You cant accept request!", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RejectFriendRequestAsync(Guid requestId, Guid currentUserId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var friendship = await _friendshipsRepository.GetByIdAsync(requestId);
|
||||
|
||||
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("Friendship not found! You cant reject request!", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<Friendship>> GetIncomingRequestsAsync(Guid userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var friendships = await _friendshipsRepository.FindByUserIdAsync(userId);
|
||||
return friendships.Where(f => f.AddresseeId == userId && f.Status == FriendshipStatus.Pending).ToList()
|
||||
?? new List<Friendship>();
|
||||
}
|
||||
catch (NotFoundByKeyException<Guid> ex)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user