Files
Govor/Govor.API/Controllers/ChatLoadController.cs
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

99 lines
3.0 KiB
C#

using AutoMapper;
using Govor.Application.Infrastructure.Extensions;
using Govor.Application.Messages;
using Govor.Contracts.Requests;
using Govor.Contracts.Responses;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers;
[ApiController]
[Authorize(Roles = "Admin, User")]
[Route("api")]
public class ChatLoadController : Controller
{
private readonly ICurrentUserService _currentUser;
private readonly ILogger<ChatLoadController> _logger;
private readonly IMessagesLoader _messagesLoader;
private readonly IMapper _mapper;
public ChatLoadController(
ILogger<ChatLoadController> logger,
IMessagesLoader messagesLoader,
ICurrentUserService currentUser,
IMapper mapper)
{
_logger = logger;
_messagesLoader = messagesLoader;
_currentUser = currentUser;
_mapper = mapper;
}
[HttpGet("groups/{groupId:guid}/messages")]
public async Task<IActionResult> GetGroupMessages(
Guid groupId,
[FromQuery] MessageQuery query)
{
try
{
if (query.Before < 0 || query.After < 0 || query.After + query.Before > 100)
return BadRequest("Values must be non-negative and total must not exceed 100.");
var result = await _messagesLoader.LoadMessagesInChatGroup(
groupId,
_currentUser.GetCurrentUserId(),
query.StartMessageId,
query.Before,
query.After);
var response = _mapper.Map<List<MessageResponse>>(result);
return Ok(response);
}
catch (ArgumentException ex)
{
_logger.LogWarning(ex, ex.Message);
return BadRequest(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return StatusCode(500, "Unexpected Error! Please try again later.");
}
}
[HttpGet("user/{userId:guid}/messages")]
public async Task<IActionResult> GetUserMessages(
Guid userId,
[FromQuery] MessageQuery query)
{
try
{
if (query.Before < 0 || query.After < 0 || query.After + query.Before > 100)
return BadRequest("Values must be non-negative and total must not exceed 100.");
var result = await _messagesLoader.LoadMessagesInUserChat(
userId,
_currentUser.GetCurrentUserId(),
query.StartMessageId,
query.Before,
query.After);
var response = _mapper.Map<List<MessageResponse>>(result);
return Ok(response);
}
catch (ArgumentException ex)
{
_logger.LogWarning(ex, ex.Message);
return BadRequest(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return StatusCode(500, "Unexpected Error! Please try again later.");
}
}
}