using Cortex.Mediator; using Lutra.API.Requests; using Lutra.Application.Verspakketten; using Microsoft.AspNetCore.Mvc; namespace Lutra.API.Controllers { /// /// Provides access to verspakket resources. /// [ApiController] [Route("api/verspakketten")] [Produces("application/json")] public class VerspakkettenController(IMediator mediator) : ControllerBase { /// /// Gets a page of verspakketten. /// /// The number of items to skip. Default: 0. /// The maximum number of items to return. Default: 50. /// The field to sort by: Naam, PrijsInCenten, AverageCijferSmaak, or AverageCijferBereiden. Default: Naam. /// The sort direction: Ascending or Descending. Default: Ascending. /// The requested verspakket page. [HttpGet] [ProducesResponseType(typeof(GetVerspakketten.Response), StatusCodes.Status200OK)] public async Task Get( int skip = 0, int take = 50, VerspakketSortField sortField = VerspakketSortField.Naam, SortDirection sortDirection = SortDirection.Ascending) { return await mediator.SendQueryAsync( new GetVerspakketten.Query(skip, take, sortField, sortDirection)); } /// /// Gets a specific verspakket by ID. /// /// The verspakket ID. /// Returns 200 OK with the verspakket when found, or 404 Not Found when not found. [HttpGet("{id:guid}")] [ProducesResponseType(typeof(GetVerspakket.Response), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task> GetById(Guid id) { var result = await mediator.SendQueryAsync(new GetVerspakket.Query(id)); if (result?.Verspakket == null) { return NotFound(); } return Ok(result); } /// /// Creates a new verspakket. /// /// The verspakket values to create. /// Returns 201 Created with the created verspakket identifier. [HttpPost] [ProducesResponseType(typeof(CreateVerspakket.Response), StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task> Post([FromBody] CreateVerspakket.Command command) { try { var result = await mediator.SendCommandAsync(command); return CreatedAtAction(nameof(GetById), new { id = result.Id }, result); } catch (InvalidOperationException ex) { return BadRequest(ex.Message); } } /// /// Updates an existing verspakket with the provided values. /// /// The verspakket identifier. /// The updated verspakket values. /// /// Returns 204 No Content when the update succeeds. /// Returns 404 Not Found when the specified verspakket does not exist. /// [HttpPut("{id:guid}")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task Update(Guid id, [FromBody] UpdateVerspakketRequest request) { var command = new UpdateVerspakket.Command(id, request.Naam, request.PrijsInCenten, request.AantalPersonen, request.SupermarktId); await mediator.SendCommandAsync(command); return NoContent(); } /// /// Adds a beoordeling to an existing verspakket. /// /// The verspakket ID. /// The beoordeling values to add. /// Returns 201 Created with the created beoordeling identifier. [HttpPost("{id:guid}/beoordelingen")] [ProducesResponseType(typeof(AddBeoordeling.Response), StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task> AddBeoordeling(Guid id, [FromBody] AddBeoordelingRequest request) { try { var command = new AddBeoordeling.Command(id, request.CijferSmaak, request.CijferBereiden, request.Aanbevolen, request.Tekst); var result = await mediator.SendCommandAsync(command); return CreatedAtAction(nameof(GetById), new { id }, result); } catch (InvalidOperationException ex) { return BadRequest(ex.Message); } } } }