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
2.9 KiB
79 lines
2.9 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 ExercicesController : ControllerBase
|
|
{
|
|
private readonly ILogger<ExercicesController> _logger;
|
|
private readonly IExerciceService _dataServices;
|
|
|
|
public ExercicesController(ILogger<ExercicesController> logger, IExerciceService dataServices)
|
|
{
|
|
_logger = logger;
|
|
_dataServices = dataServices;
|
|
}
|
|
|
|
[HttpGet]
|
|
[ProducesResponseType(typeof(IEnumerable<ResponseExerciceDto>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> GetExercices([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 5, [FromQuery] bool ascending = true)
|
|
{
|
|
var exercices = await _dataServices.GetExercices(pageIndex, pageSize, ascending);
|
|
return exercices.TotalCount == 0 ? NoContent() : Ok(exercices);
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
[ProducesResponseType(typeof(ResponseExerciceDto), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> GetExerciceById(string id)
|
|
{
|
|
var exercice = await _dataServices.GetExerciceById(id);
|
|
return exercice == null ? NotFound() : Ok(exercice);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(ResponseExerciceDto), StatusCodes.Status201Created)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> CreateExercice([FromBody] RequestExerciceDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var createdExercice = await _dataServices.CreateExercice(request);
|
|
return CreatedAtAction(nameof(GetExerciceById), new { id = createdExercice.Id }, createdExercice);
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
[ProducesResponseType(typeof(ResponseExerciceDto), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> UpdateExercice(string id, [FromBody] RequestExerciceDto request)
|
|
{
|
|
if (!ModelState.IsValid) return BadRequest(ModelState);
|
|
|
|
var updatedExercice = await _dataServices.UpdateExercice(id, request);
|
|
return updatedExercice == null ? NotFound() : Ok(updatedExercice);
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[RequireHttps]
|
|
[AllowAnonymous]
|
|
public async Task<IActionResult> DeleteExercice(string id)
|
|
{
|
|
var deleted = await _dataServices.DeleteExercice(id);
|
|
return deleted ? NoContent() : NotFound();
|
|
}
|
|
} |