Files
Govor/Govor.API/Controllers/Friends/FriendshipController.cs
T
Artemy 3ec61fa2c0 Stable server
was added removing messages
many fixes bugs and moving to result pattern from throwing exceptions
2026-08-22 19:28:42 +07:00

70 lines
2.1 KiB
C#

using AutoMapper;
using Govor.Application.Friends;
using Govor.Application.Infrastructure.Extensions;
using Govor.Contracts.DTOs;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers.Friends;
[Authorize]
[Route("api/friends")]
[ApiController]
public class FriendshipController : Controller
{
private readonly ILogger<FriendshipController> _logger;
private readonly IFriendshipService _friendsService;
private readonly ICurrentUserService _currentUserService;
private readonly IMapper _mapper;
public FriendshipController(
ILogger<FriendshipController> logger,
IFriendshipService friendsService,
ICurrentUserService currentUserService,
IMapper mapper)
{
_logger = logger;
_mapper = mapper;
_friendsService = friendsService;
_currentUserService = currentUserService;
}
[HttpGet("search")] // api/friends/search?query=
public async Task<IActionResult> Search(string query)
{
if (string.IsNullOrWhiteSpace(query))
return BadRequest("Query cannot be empty");
try
{
var result = await _friendsService.SearchUsersAsync(query, _currentUserService.GetCurrentUserId());
var response = _mapper.Map<List<UserDto>>(result);
return Ok(response);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return StatusCode(500, new { error = "Internal error during user search." });
}
}
[HttpGet] // api/friends
public async Task<IActionResult> GetFriends()
{
try
{
var result = await _friendsService.GetFriendsAsync(_currentUserService.GetCurrentUserId());
var response = _mapper.Map<List<UserDto>>(result);
return Ok(response);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return StatusCode(500, new { error = "Internal server error." });
}
}
}