mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
96587b491d
Introduced AutoMapper to the API project and registered it in the DI container. Added mapping profiles for core models to DTOs and response objects. Refactored controllers to use AutoMapper for mapping entities to response DTOs. Implemented new response models for messages, media attachments, reactions, and views. Updated MessagesLoader to load messages with related data. Added a migration to support ChatGroupId in messages. Updated dependencies and fixed minor issues in related files.
78 lines
2.4 KiB
C#
78 lines
2.4 KiB
C#
using AutoMapper;
|
|
using Govor.Application.Exceptions.FriendsService;
|
|
using Govor.Application.Interfaces.Friends;
|
|
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
|
using Govor.Contracts.DTOs;
|
|
using Govor.Core.Models.Users;
|
|
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 (SearchUsersException ex)
|
|
{
|
|
_logger.LogWarning(ex, ex.Message);
|
|
return NotFound(new { error = 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 (Exception ex)
|
|
{
|
|
_logger.LogError(ex, ex.Message);
|
|
return StatusCode(500, new { error = "Internal server error." });
|
|
}
|
|
}
|
|
} |