mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
6d1c53beeb
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.
98 lines
3.2 KiB
C#
98 lines
3.2 KiB
C#
using Govor.Application.Exceptions.VerifyFriendship;
|
|
using Govor.Application.Friends;
|
|
using Govor.Application.Infrastructure.AdminsStuff;
|
|
using Govor.Application.Infrastructure.Extensions;
|
|
using Govor.Application.PrivateUserChats;
|
|
using Govor.Contracts.DTOs;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Govor.API.Controllers;
|
|
|
|
[ApiController]
|
|
[Authorize(Roles = "Admin, User")]
|
|
[Route("api")]
|
|
public class PrivateChatController : Controller
|
|
{
|
|
private readonly ICurrentUserService _currentUser;
|
|
private readonly IVerifyFriendship _verifyFriendship;
|
|
private readonly IUserPrivateChatsCreator _userPrivateChatsCreator;
|
|
private readonly IUserPrivateChatsGetterService _privateChatsGetter;
|
|
private readonly ILogger<ChatLoadController> _logger;
|
|
|
|
public PrivateChatController(
|
|
ICurrentUserService currentUser,
|
|
IVerifyFriendship verifyFriendship,
|
|
IUserPrivateChatsCreator userPrivateChatsCreator,
|
|
IUserPrivateChatsGetterService userPrivateChatsGetterService,
|
|
ILogger<ChatLoadController> logger)
|
|
{
|
|
_currentUser = currentUser;
|
|
_verifyFriendship = verifyFriendship;
|
|
_userPrivateChatsCreator = userPrivateChatsCreator;
|
|
_privateChatsGetter = userPrivateChatsGetterService;
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpGet("user/{friendId:guid}/private-chat")]
|
|
public async Task<IActionResult> GetChatByFriendId(Guid friendId)
|
|
{
|
|
try
|
|
{
|
|
var currentId = _currentUser.GetCurrentUserId();
|
|
|
|
await _verifyFriendship.VerifyAsync(currentId, friendId);
|
|
|
|
var chat = await _userPrivateChatsCreator.CreateAsync(currentId, friendId);
|
|
return Ok(chat.Id);
|
|
}
|
|
catch (UnauthorizedAccessException ex)
|
|
{
|
|
_logger.LogWarning(ex.Message);
|
|
return Forbid(ex.Message);
|
|
}
|
|
catch (ArgumentException ex)
|
|
{
|
|
_logger.LogWarning(ex, ex.Message);
|
|
return BadRequest(ex.Message);
|
|
}
|
|
catch (FriendshipException ex)
|
|
{
|
|
_logger.LogWarning(ex, ex.Message);
|
|
return Forbid(ex.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, ex.Message);
|
|
return StatusCode(500, "Unexpected Error! Please try again later.");
|
|
}
|
|
}
|
|
|
|
[HttpGet("user/private-chats")]
|
|
public async Task<IActionResult> GetChatsByFriends()
|
|
{
|
|
try
|
|
{
|
|
var currentId = _currentUser.GetCurrentUserId();
|
|
var chats = await _privateChatsGetter.GetUserChatsAsync(currentId);
|
|
|
|
var result = chats.Select(chat => new PrivateChatDto
|
|
{
|
|
ChatId = chat.Id,
|
|
FriendId = chat.UserAId == currentId ? chat.UserBId : chat.UserAId
|
|
}).ToList();
|
|
|
|
return Ok(result);
|
|
}
|
|
catch (UnauthorizedAccessException ex)
|
|
{
|
|
_logger.LogWarning(ex.Message);
|
|
return Forbid(ex.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, ex.Message);
|
|
return StatusCode(500, "Unexpected Error! Please try again later.");
|
|
}
|
|
}
|
|
} |