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; namespace Govor.API.Controllers.Friends; [ApiController] [Authorize] [Route("api/friends")] public class FriendsRequestQueryController : Controller { private readonly ILogger _logger; private readonly IFriendRequestQueryService _friendsService; private readonly ICurrentUserService _currentUserService; public FriendsRequestQueryController(ILogger logger, IFriendRequestQueryService friendsService, ICurrentUserService currentUserService) { _logger = logger; _friendsService = friendsService; _currentUserService = currentUserService; } [HttpGet("requests")] // api/friends/requests public async Task 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 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()); } catch (Exception ex) { _logger.LogError(ex, ex.Message); return StatusCode(500, new { error = "Internal server error." }); } } private List BuildFriendshipDtos(IEnumerable friendships) => friendships.Select(f => new FriendshipDto { Id = f.Id, Status = f.Status, AddresseeId = f.AddresseeId, RequesterId = f.RequesterId, }).ToList(); }