Stable server

was added removing messages
many fixes bugs and moving to result pattern from throwing exceptions
This commit is contained in:
Artemy
2026-08-22 19:28:42 +07:00
parent 27fdcf85da
commit 3ec61fa2c0
36 changed files with 452 additions and 278 deletions
@@ -9,22 +9,21 @@ namespace Govor.API.Controllers.AdminStuff;
[ApiController]
[Route("api/admin/[controller]")]
[Authorize(Roles = "Admin")]
[Authorize]//(Roles = "Admin")
public class UsersController : Controller
{
private readonly ILogger<UsersController> _logger;
private readonly IUsersAdministration _users;
public UsersController(ILogger<UsersController> logger,
IUsersAdministration users,
IInvitationGenerator invitationGenerator)
public UsersController(
ILogger<UsersController> logger,
IUsersAdministration users)
{
_logger = logger;
_users = users;
}
[HttpGet]
[HttpGet("all")]
public async Task<IActionResult> AllUsers()
{
try
+16 -26
View File
@@ -1,10 +1,14 @@
using AutoMapper;
using Govor.API.Common.Extensions;
using Govor.Application.Infrastructure.Extensions;
using Govor.Application.Messages;
using Govor.Contracts.Requests;
using Govor.Contracts.Responses;
using Govor.Domain.Common;
using Govor.Domain.Models.Messages;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SmartRes;
namespace Govor.API.Controllers;
@@ -40,21 +44,14 @@ public class ChatLoadController : Controller
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(
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);
query.After)).Map(messages => _mapper.Map<List<MessageResponse>>(messages));
return result.ToActionResult();
}
catch (Exception ex)
{
@@ -73,21 +70,15 @@ public class ChatLoadController : Controller
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 result = (await _messagesLoader.LoadMessagesInUserChat(
userId,
_currentUser.GetCurrentUserId(),
query.StartMessageId,
query.Before,
query.After)
).Map(messages => _mapper.Map<List<MessageResponse>>(messages));
var response = _mapper.Map<List<MessageResponse>>(result);
return Ok(response);
}
catch (ArgumentException ex)
{
_logger.LogWarning(ex, ex.Message);
return BadRequest(ex.Message);
return result.ToActionResult();
}
catch (Exception ex)
{
@@ -95,5 +86,4 @@ public class ChatLoadController : Controller
return StatusCode(500, "Unexpected Error! Please try again later.");
}
}
}
@@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers.Friends;
[Authorize]
[Route("api/friends")]
[ApiController]
@@ -2,11 +2,14 @@ using AutoMapper;
using Govor.Application.Friends;
using Govor.Application.Infrastructure.Extensions;
using Govor.Contracts.DTOs;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers.Friends;
[Authorize]
[Route("api/friends")]
[ApiController]
public class FriendshipController : Controller
{
private readonly ILogger<FriendshipController> _logger;
@@ -40,11 +43,6 @@ public class FriendshipController : Controller
return Ok(response);
}
catch (UnauthorizedAccessException ex)
{
_logger.LogWarning(ex, ex.Message);
return Forbid(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
@@ -63,16 +61,6 @@ public class FriendshipController : Controller
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);
+2 -2
View File
@@ -21,11 +21,11 @@ public class InviteController : ControllerBase
[Authorize]
[HttpGet("{code}")]
public IActionResult JoinGroup(string code)
public async Task<IActionResult> JoinGroup(string code)
{
try
{
_groupService.AddUserToGroupByInvitationAsync(_currentUser.GetCurrentUserId(), code);
var groupRes = await _groupService.AddUserToGroupByInvitationAsync(_currentUser.GetCurrentUserId(), code);
var group = _groupService.GetGroupByInviteCode(code);
+21 -26
View File
@@ -1,3 +1,4 @@
using Govor.API.Common.Extensions;
using Govor.Application.Infrastructure.Extensions;
using Govor.Application.Medias;
using Govor.Contracts.Requests;
@@ -70,19 +71,22 @@ public class MediaController : Controller
_logger.LogInformation("Uploaded file {FileName} from user {UserId}",
media.FileName, media.UploaderId);
return Ok(result);
if (result.IsFailure)
{
_logger.LogWarning("Uploaded file {FileName} from user {UserId} finished with error: {Error}",
media.FileName,
media.UploaderId,
result.Error);
}
return result.ToActionResult();
}
catch (UnauthorizedAccessException ex)
{
_logger.LogWarning(ex, ex.Message);
return Forbid(ex.Message);
}
catch (InvalidOperationException ex)
{
_logger.LogWarning(ex, ex.Message);
return BadRequest(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error uploading media");
@@ -101,26 +105,17 @@ public class MediaController : Controller
[HttpGet("download/{id}")]
public async Task<IActionResult> Download(Guid id)
{
try
{
var userId = _currentUserService.GetCurrentUserId();
var userId = _currentUserService.GetCurrentUserId();
if (!await _accesser.HasAccessAsync(id, userId))
return Forbid();
if (!await _accesser.HasAccessAsync(id, userId))
return Forbid();
var media = await _mediaService.GetMediaByIdAsync(id);
return File(media.Data, media.MimeType, Path.GetFileName(media.FileName));
}
catch (KeyNotFoundException ex)
{
_logger.LogWarning(ex, ex.Message);
return NotFound("Media not found");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error downloading media");
return StatusCode(500, "Internal server error");
}
var mediaResult = await _mediaService.GetMediaByIdAsync(id);
if (mediaResult.IsFailure)
return mediaResult.ToActionResult();
var media = mediaResult.Value;
return File(media.Data, media.MimeType, Path.GetFileName(media.FileName));
}
}
+16 -16
View File
@@ -1,4 +1,5 @@
using AutoMapper;
using Govor.API.Common.Extensions;
using Govor.API.Hubs;
using Govor.Application.Infrastructure.Extensions;
using Govor.Application.Medias;
@@ -9,6 +10,7 @@ using Govor.Domain.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using SmartRes;
namespace Govor.API.Controllers;
@@ -43,10 +45,9 @@ public class ProfileController : ControllerBase
[HttpPost("avatar")] // api/profile/avatar
public async Task<IActionResult> UploadAvatar([FromForm] AvatarUploadRequest request)
{
var userId = _currentUserService.GetCurrentUserId();
if (request.FromFile == null || request.FromFile.Length == 0)
if (request?.FromFile == null || request.FromFile.Length == 0)
{
return BadRequest("File is empty.");
}
@@ -65,11 +66,13 @@ public class ProfileController : ControllerBase
String.Empty,
MediaOwnerType.Avatar,
userId);
var mediaInfo = await _mediaService.UploadMediaAsync(media);
await _profileService.SetNewIcon(userId, mediaInfo.MediaId);
return Ok(mediaInfo);
var mediaInfo = await _mediaService.UploadMediaAsync(media)
.TapAsync(m => _logger.LogInformation("Uploaded avatar file {filename} by user {id}.",
request.FromFile.FileName, userId))
.TapAsync(mediaInfo => _profileService.SetNewIcon(userId, mediaInfo.MediaId));
return mediaInfo.ToActionResult();
}
catch (System.Exception ex)
{
@@ -92,12 +95,9 @@ public class ProfileController : ControllerBase
var userId = _currentUserService.GetCurrentUserId();
var result = await _profileService.GetUserProfileAsync(userId);
if(result.IsFailure)
return NotFound(result.Error);
var user = result.Value;
var dto = _mapper.Map<UserProfileDto>(user);
return Ok(dto);
return result
.Map(user => _mapper.Map<UserProfileDto>(user))
.ToActionResult();
}
catch (UnauthorizedAccessException ex)
{
@@ -116,10 +116,10 @@ public class ProfileController : ControllerBase
{
try
{
var user = await _profileService.GetUserProfileAsync(id);
var user = (await _profileService.GetUserProfileAsync(id))
.Map(user => _mapper.Map<UserProfileDto>(user));
var dto = _mapper.Map<UserProfileDto>(user);
return Ok(dto);
return user.ToActionResult();
}
catch (UnauthorizedAccessException ex)
{
@@ -1,3 +1,4 @@
using Govor.API.Common.Extensions;
using Govor.Application.Infrastructure.Extensions;
using Govor.Application.PushNotifications;
using Govor.Contracts.Requests;
@@ -39,13 +40,13 @@ public class PushTokensController : Controller
var currentId = _currentUser.GetCurrentUserId();
var currentSessionId = _currentSession.GetUserSessionId();
await _pushTokenService.AddOrUpdateTokenAsync(
var result = await _pushTokenService.AddOrUpdateTokenAsync(
userId: currentId,
sessionId: currentSessionId,
token: req.Token,
platform: req.Platform);
return Ok();
return result.ToActionResult();
}
catch (ArgumentException ex)
{
+9 -29
View File
@@ -1,4 +1,5 @@
using AutoMapper;
using Govor.API.Common.Extensions;
using Govor.Application.Infrastructure.Extensions;
using Govor.Application.Users.UserSessions;
using Govor.Contracts.DTOs;
@@ -40,12 +41,10 @@ public class SessionController : Controller
{
try
{
var sessions = await _userSessionReader.GetAllSessionsAsync(_currentUserService.GetCurrentUserId());
if(sessions.IsFailure)
return NotFound(sessions.Error);
return Ok(_mapper.Map<List<SessionDto>>(sessions.Value));
var sessions = (await _userSessionReader.GetAllSessionsAsync(_currentUserService.GetCurrentUserId()))
.Map(sessions => _mapper.Map<List<SessionDto>>(sessions));
return sessions.ToActionResult();
}
catch (UnauthorizedAccessException ex)
{
@@ -70,15 +69,7 @@ public class SessionController : Controller
var res = await _userSessionRevoker.CloseSessionByIdAsync(sessionId,
_currentUserService.GetCurrentUserId());
if (res.IsFailure)
return BadRequest(res.Error);
return Ok();
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, ex.Message);
return BadRequest(ex.Message);
return res.ToActionResult();
}
catch (UnauthorizedAccessException ex)
{
@@ -97,19 +88,11 @@ public class SessionController : Controller
{
try
{
var res = await _userSessionRevoker.CloseSessionByIdAsync(
var res = await _userSessionRevoker.CloseSessionByIdAsync(
_currentUserSessionService.GetUserSessionId(),
_currentUserService.GetCurrentUserId());
if(res.IsFailure)
return NotFound(res.Error);
return Ok();
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, ex.Message);
return BadRequest(ex.Message);
return res.ToActionResult();
}
catch (UnauthorizedAccessException ex)
{
@@ -130,10 +113,7 @@ public class SessionController : Controller
{
var res = await _userSessionRevoker.CloseAllSessionsAsync(_currentUserService.GetCurrentUserId());
if(res.IsFailure)
return NotFound(res.Error);
return Ok();
return res.ToActionResult();
}
catch (UnauthorizedAccessException ex)
{