mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
app.MapHub<FriendsHub>("/api/friends");
This commit is contained in:
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user