Files
Govor/Govor.API/Controllers/Friends/FriendshipController.cs
T
Artemy 6d1c53beeb Refactor: migrate Core -> Domain and reorganize projects
Large refactor that renames/moves core types into a new Govor.Domain surface and reorganizes the Application layer. Models, configurations, migrations and many files moved from Govor.Core/Govor.Data to Govor.Domain; numerous Application services, interfaces and implementations were relocated or added (authentication, friends, messages, medias, push notifications, user sessions, storage, synching, private chats, etc.). Tests updated to use Govor.Domain namespaces and adjusted project references (removed Govor.Data reference from API tests). Also updated API, Hub and mapping code and project files to reflect the new structure and naming. This is primarily a codebase-wide namespace and module reorganization to establish a Domain project and restructure application services.
2026-07-16 19:27:45 +07:00

82 lines
2.5 KiB
C#

using AutoMapper;
using Govor.Application.Friends;
using Govor.Application.Infrastructure.Extensions;
using Govor.Contracts.DTOs;
using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers.Friends;
[Route("api/friends")]
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 (UnauthorizedAccessException ex)
{
_logger.LogWarning(ex, ex.Message);
return Forbid(ex.Message);
}
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 (InvalidOperationException ex)
{
_logger.LogError(ex, ex.Message);
return Ok(Array.Empty<UserDto>());
}
catch (UnauthorizedAccessException ex)
{
_logger.LogWarning(ex, ex.Message);
return Forbid(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return StatusCode(500, new { error = "Internal server error." });
}
}
}