mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
Created SessionController and UserSessionReader
+ tests for UserSessionReader
This commit is contained in:
@@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user