app.MapHub<FriendsHub>("/api/friends");

This commit is contained in:
Artemy
2025-07-11 12:19:32 +07:00
parent d9dfaff079
commit e8e2078514
9 changed files with 249 additions and 325 deletions
@@ -1,5 +1,7 @@
using Govor.Application.Interfaces.Friends;
using Govor.Application.Interfaces.Infrastructure.Extensions;
using Govor.Contracts.DTOs;
using Govor.Core.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -10,11 +12,11 @@ namespace Govor.API.Controllers.Friends;
[Route("api/friends")]
public class FriendsRequestQueryController : Controller
{
private readonly ILogger<FriendsController> _logger;
private readonly ILogger<FriendsRequestQueryController> _logger;
private readonly IFriendRequestQueryService _friendsService;
private readonly ICurrentUserService _currentUserService;
public FriendsRequestQueryController(ILogger<FriendsController> logger,
public FriendsRequestQueryController(ILogger<FriendsRequestQueryController> logger,
IFriendRequestQueryService friendsService,
ICurrentUserService currentUserService)
{
@@ -23,5 +25,51 @@ public class FriendsRequestQueryController : Controller
_currentUserService = currentUserService;
}
[HttpGet("requests")] // api/friends/requests
public async Task<IActionResult> GetIncomingRequests()
{
try
{
var result = await _friendsService.GetIncomingAsync(_currentUserService.GetCurrentUserId());
return Ok(BuildFriendshipDtos(result));
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, ex.Message);
return BadRequest("Failed to get friend requests. User data missing.");
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return StatusCode(500, new { error = "Internal server error." });
}
}
[HttpGet("responses")]// api/friends/responses
public async Task<IActionResult> GetResponses()
{
try
{
var result = await _friendsService.GetResponsesAsync(_currentUserService.GetCurrentUserId());
return Ok(BuildFriendshipDtos(result));
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, ex.Message);
return Ok(Array.Empty<FriendshipDto>());
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return StatusCode(500, new { error = "Internal server error." });
}
}
private List<FriendshipDto> BuildFriendshipDtos(IEnumerable<Friendship> friendships) => friendships.Select(f => new FriendshipDto
{
Id = f.Id,
Status = f.Status,
AddresseeId = f.AddresseeId,
RequesterId = f.RequesterId,
}).ToList();
}