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 ExercicesTemplateController : ControllerBase { private readonly ILogger _logger; private readonly IExerciceService _dataServices; public ExercicesTemplateController(ILogger logger, IExerciceTemplateService dataServices) { _logger = logger; _dataServices = dataServices; } [HttpGet] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [AllowAnonymous] public async Task 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 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 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 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 DeleteExercice(string id) { var deleted = await _dataServices.DeleteExercice(id); return deleted ? NoContent() : NotFound(); } }