test to make server

This commit is contained in:
Artemy
2026-02-08 22:30:29 +07:00
parent cc2921d257
commit 0a43e35797
56 changed files with 949 additions and 442 deletions
+5 -5
View File
@@ -11,7 +11,7 @@ namespace Govor.API.Controllers;
[ApiController]
[Authorize(Roles = "Admin, User")]
[Route("api/chats")]
[Route("api")]
public class ChatLoadController : Controller
{
private readonly ICurrentUserService _currentUser;
@@ -31,9 +31,9 @@ public class ChatLoadController : Controller
_mapper = mapper;
}
[HttpGet("group-messages")]
[HttpGet("groups/{groupId:guid}/messages")]
public async Task<IActionResult> GetGroupMessages(
Guid chatId,
Guid groupId,
[FromQuery] MessageQuery query)
{
try
@@ -42,7 +42,7 @@ public class ChatLoadController : Controller
return BadRequest("Values must be non-negative and total must not exceed 100.");
var result = await _messagesLoader.LoadMessagesInChatGroup(
chatId,
groupId,
_currentUser.GetCurrentUserId(),
query.StartMessageId,
query.Before,
@@ -74,7 +74,7 @@ public class ChatLoadController : Controller
}
}
[HttpGet("user-messages")]
[HttpGet("user/{userId:guid}/messages")]
public async Task<IActionResult> GetUserMessages(
Guid userId,
[FromQuery] MessageQuery query)
@@ -0,0 +1,95 @@
using Govor.Application.Interfaces;
using Govor.Application.Interfaces.Infrastructure.Extensions;
using Govor.Core.Models;
using Govor.Core.Repositories.PrivateChats;
using Govor.Core.Repositories.Users;
using Govor.Data.Repositories.Exceptions;
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 IUsersRepository _usersRepository;
private readonly IVerifyFriendship _verifyFriendship;
private readonly IPrivateChatsRepository _privateChats;
private readonly ILogger<ChatLoadController> _logger;
public PrivateChatController(
ICurrentUserService currentUser,
IUsersRepository usersRepository,
IVerifyFriendship verifyFriendship,
IPrivateChatsRepository privateChats,
ILogger<ChatLoadController> logger)
{
_currentUser = currentUser;
_usersRepository = usersRepository;
_verifyFriendship = verifyFriendship;
_privateChats = privateChats;
_logger = logger;
}
[HttpGet("user/{friendId:guid}/private-chat")]
public async Task<IActionResult> GetChatByFriendId(Guid friendId)
{
try
{
var currentId = _currentUser.GetCurrentUserId();
if (!await _usersRepository.ExistsByIdAsync(friendId))
{
_logger.LogWarning("User not exist {0}", friendId);
return NotFound("User not exist.");
}
await _verifyFriendship.VerifyAsync(currentId, friendId);
var chatId = await GetPrivateChatsIdAsync(currentId, friendId);
return Ok(chatId);
}
catch (UnauthorizedAccessException ex)
{
_logger.LogWarning(ex.Message);
return Forbid(ex.Message);
}
catch (NotFoundException ex)
{
_logger.LogWarning(ex, ex.Message);
return BadRequest(ex.Message);
}
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.");
}
}
private async Task<Guid> GetPrivateChatsIdAsync(Guid userA, Guid userB)
{
Guid recipientId;
// Makes new PrivateChat if not exist
if (!_privateChats.Exist(userA, userB))
{
recipientId = Guid.NewGuid();
await _privateChats.AddAsync(new PrivateChat()
{ Id = recipientId, UserAId = userA, UserBId = userB });
}
else
{
recipientId = (await _privateChats.GetByMembersAsync(userA, userB)).Id;
}
return recipientId;
}
}
@@ -72,14 +72,6 @@ public class ProfileController : ControllerBase
await _profileService.SetNewIcon(userId, mediaInfo.MediaId);
var iconId = mediaInfo.MediaId;
var payload = new { userId, iconId };
await _profileHubContext.Clients.All.SendAsync(
"AvatarUpdated",
payload
);
return Ok(mediaInfo);
}
catch (System.Exception ex)