You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
3.1 KiB
79 lines
3.1 KiB
using Asp.Versioning;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Server.Dto.Request;
|
|
using Server.Dto.Response;
|
|
using Server.IServices;
|
|
|
|
namespace Server.Controller.v1
|
|
{
|
|
[ApiController]
|
|
[ApiVersion("1.0")]
|
|
[Route("api/v{version:apiVersion}/[controller]")]
|
|
public class SessionsController : ControllerBase
|
|
{
|
|
private readonly ILogger<SessionsController> _logger;
|
|
private readonly ISessionService _dataServices;
|
|
|
|
public SessionsController(ILogger<SessionsController> logger, ISessionService dataServices)
|
|
{
|
|
_logger = logger;
|
|
_dataServices = dataServices;
|
|
}
|
|
|
|
[HttpGet]
|
|
[ProducesResponseType(typeof(IEnumerable<ResponseSessionDto>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> GetSessions([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 5, [FromQuery] bool ascending = true)
|
|
{
|
|
var sessions = await _dataServices.GetSessions(pageIndex, pageSize, ascending);
|
|
return sessions.TotalCount == 0 ? NoContent() : Ok(sessions);
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
[ProducesResponseType(typeof(ResponseSessionDto), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> GetSessionById(string id)
|
|
{
|
|
var session = await _dataServices.GetSessionById(id);
|
|
return session == null ? NotFound() : Ok(session);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(ResponseSessionDto), StatusCodes.Status201Created)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> CreateSession([FromBody] RequestSessionDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var createdSession = await _dataServices.CreateSession(request);
|
|
return CreatedAtAction(nameof(GetSessionById), new { id = createdSession.Id }, createdSession);
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
[ProducesResponseType(typeof(ResponseSessionDto), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> UpdateSession(string id, [FromBody] RequestSessionDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var updatedSession = await _dataServices.UpdateSession(id, request);
|
|
return updatedSession == null ? NotFound() : Ok(updatedSession);
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> DeleteSession(string id)
|
|
{
|
|
var deleted = await _dataServices.DeleteSession(id);
|
|
return deleted ? NoContent() : NotFound();
|
|
}
|
|
}
|
|
} |