51 lines
1.7 KiB
C#
51 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
using Knot.Modules.Relations.Domain;
|
|
using Knot.Shared.Kernel;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Knot.Modules.Relations.Application.Abstractions;
|
|
namespace Knot.Modules.Relations.Application.Friends;
|
|
|
|
public record GetIncomingRequestsQuery(Guid UserId) : IQuery<List<FriendRequestDto>>;
|
|
|
|
internal sealed class GetIncomingRequestsQueryHandler : IQueryHandler<GetIncomingRequestsQuery, List<FriendRequestDto>>
|
|
{
|
|
private readonly IRelationsDbContext _context;
|
|
private readonly IUserRepository _userRepository;
|
|
|
|
public GetIncomingRequestsQueryHandler(IRelationsDbContext context, IUserRepository userRepository)
|
|
{
|
|
_context = context;
|
|
_userRepository = userRepository;
|
|
}
|
|
|
|
public async Task<Result<List<FriendRequestDto>>> Handle(GetIncomingRequestsQuery request, CancellationToken cancellationToken)
|
|
{
|
|
var friendships = await _context.Friendships
|
|
.Where(f => f.FriendId == request.UserId && f.Status == FriendshipStatus.Pending)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
var requestsList = new List<FriendRequestDto>();
|
|
foreach (var fs in friendships)
|
|
{
|
|
var user = await _userRepository.GetByIdAsync(fs.UserId, cancellationToken);
|
|
if (user == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
requestsList.Add(new FriendRequestDto(
|
|
fs.Id,
|
|
new FriendUserDto(user.Id, user.Username, user.DisplayName, user.Avatar),
|
|
fs.CreatedAt
|
|
));
|
|
}
|
|
return Result.Success(requestsList);
|
|
}
|
|
}
|