Created endpoints to manage friends
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using BeReadyBackend.Models;
|
||||
using BeReadyBackend.Repositories;
|
||||
using BeReadyBackend.Services;
|
||||
using BeReadyBackend.Specifications.Friends;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace BeReadyBackend.Endpoints.Friends;
|
||||
|
||||
public class AcceptFriendRequest
|
||||
{
|
||||
public int FriendId { get; set; }
|
||||
}
|
||||
|
||||
public class AcceptFriendRequestEndpoint(UserService userService, UserFriendsRepository userFriendsRepository) : Endpoint<AcceptFriendRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/Friends/{@FriendId}/Request/");
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(AcceptFriendRequest req, CancellationToken ct)
|
||||
{
|
||||
int userId = userService.GetUserIdFromToken();
|
||||
UserFriend? userFriend = await userFriendsRepository.SingleOrDefaultAsync(new GetFriendRequestByIdsSpec(req.FriendId, userId), ct);
|
||||
|
||||
if (userFriend is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
userFriend.IsAccepted = true;
|
||||
|
||||
UserFriend friend = new()
|
||||
{
|
||||
UserId = userId,
|
||||
FriendId = req.FriendId,
|
||||
IsAccepted = true
|
||||
};
|
||||
|
||||
await userFriendsRepository.AddAsync(friend, ct);
|
||||
await userFriendsRepository.SaveChangesAsync(ct);
|
||||
await Send.OkAsync(ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user