Created SessionController and UserSessionReader

+ tests for UserSessionReader
This commit is contained in:
Artemy
2025-07-24 17:03:51 +07:00
parent 19f1ccdfd5
commit 15f9370dbe
7 changed files with 224 additions and 1 deletions
@@ -87,6 +87,8 @@ public static class ConfigurationProgramExtensions
services.AddAutoMapper(typeof(MappingProfile));
services.AddScoped<IHubUserAccessor, HubUserAccessor>();
services.AddScoped<IUserSessionReader, UserSessionReader>();
}
public static void AddRepositories(this IServiceCollection services)
@@ -21,5 +21,7 @@ public class MappingProfile : Profile
.AfterMap<UserToUserDtoMappingAction>();
CreateMap<Friendship, FriendshipDto>();
CreateMap<UserSession, SessionDto>();
}
}
+109
View File
@@ -0,0 +1,109 @@
using AutoMapper;
using Govor.Application.Interfaces.Infrastructure.Extensions;
using Govor.Application.Interfaces.UserSession;
using Govor.Contracts.DTOs;
using Govor.Data.Repositories.Exceptions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Govor.API.Controllers;
[ApiController]
[Authorize(Roles = "Admin,User")]
[Route("api/[controller]")]
public class SessionController : Controller
{
private readonly ILogger<SessionController> _logger;
private readonly ICurrentUserService _currentUserService;
private readonly IUserSessionReader _userSessionReader;
private readonly IUserSessionRevoker _userSessionRevoker;
private readonly IMapper _mapper;
public SessionController(
ILogger<SessionController> logger,
ICurrentUserService currentUserService,
IUserSessionReader userSessionReader,
IUserSessionRevoker userSessionRevoker,
IMapper mapper)
{
_logger = logger;
_currentUserService = currentUserService;
_userSessionReader = userSessionReader;
_userSessionRevoker = userSessionRevoker;
_mapper = mapper;
}
[HttpGet("all")]
public async Task<IActionResult> GetAllSessions()
{
try
{
var sessions = await _userSessionReader.GetAllSessionsAsync(_currentUserService.GetCurrentUserId());
return Ok(_mapper.Map<List<SessionDto>>(sessions));
}
catch (UnauthorizedAccessException 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.");
}
}
[HttpDelete("close/{sessionId}")]
public async Task<IActionResult> CloseSession(Guid sessionId)
{
try
{
if (sessionId == Guid.Empty)
return BadRequest("Invalid sessionId.");
await _userSessionRevoker.CloseSessionByIdAsync(sessionId,
_currentUserService.GetCurrentUserId());
return Ok();
}
catch (InvalidOperationException ex)
{
_logger.LogError(ex, ex.Message);
return BadRequest(ex.Message);
}
catch (UnauthorizedAccessException ex)
{
_logger.LogWarning(ex, ex.Message);
return Forbid(ex.Message);
}
catch (NotFoundException ex)
{
_logger.LogWarning(ex, ex.Message);
return NotFound(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return StatusCode(500, "Unexpected Error! Please try again later.");
}
}
[HttpDelete("close/all")]
public async Task<IActionResult> CloseAllSessions()
{
try
{
await _userSessionRevoker.CloseAllSessionsAsync(_currentUserService.GetCurrentUserId());
return Ok();
}
catch (UnauthorizedAccessException 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.");
}
}
}