mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
using Govor.Application.Exceptions.FriendsService;
|
|
using Govor.Application.Interfaces.Friends;
|
|
using Govor.Core.Models;
|
|
using Govor.Core.Models.Users;
|
|
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;
|
|
}
|
|
catch (NotFoundByKeyException<(string, Guid)> ex)
|
|
{
|
|
return [];
|
|
}
|
|
catch (NotFoundByKeyException<Guid> ex)
|
|
{
|
|
return [];
|
|
}
|
|
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("Nothing was found for the specified id.", ex);
|
|
}
|
|
}
|
|
} |