diff options
author | HombreLaser <sebastian-440@live.com> | 2022-11-21 16:34:39 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2022-11-21 16:34:39 -0600 |
commit | b3deb8c5d92dd616042ea9e265c197abe02f995c (patch) | |
tree | 235d1597fb1729ebbd0c898fada61e0a76b5a95e /Controllers/RafflesController.cs | |
parent | 3e67b71325f51c1a83a4cb7d78586690c4d89a0c (diff) |
Añadido controlador de rifas
Diffstat (limited to 'Controllers/RafflesController.cs')
-rw-r--r-- | Controllers/RafflesController.cs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Controllers/RafflesController.cs b/Controllers/RafflesController.cs new file mode 100644 index 0000000..775ef3b --- /dev/null +++ b/Controllers/RafflesController.cs @@ -0,0 +1,70 @@ +using AutoMapper; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.WebUtilities; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using BackendPIA.Forms; +using BackendPIA.Models; +using BackendPIA.Services; +using BackendPIA.Logics; + +namespace BackendPIA.Controllers { + [Route("api/raffles")] + [ApiController] + public class RafflesController : ControllerBase { + private readonly IRaffleService _service; + private readonly IMapper _mapper; + + public RafflesController(IMapper mapper, IRaffleService service) { + _service = service; + _mapper = mapper; + } + + [HttpGet] + public async Task<IEnumerable<Raffle>> Get([FromQuery] string name = "") { + return await _service.GetRaffles(name); + } + + [HttpGet("{id:int}")] + public async Task<ActionResult<Raffle>> Show(long id) { + var raffle = await _service.GetRaffle(id); + + if(raffle == null) + return NotFound("The resource couldn't be found"); + + return raffle; + } + + [Authorize(Roles = "Administrator")] + [HttpPost] + public async Task<ActionResult<Raffle>> Create(RaffleForm form) { + var raffle = await _service.CreateRaffle(_mapper.Map<Raffle>(form)); + + return raffle; + } + + [Authorize(Roles = "Administrator")] + [HttpPut("{id:int}")] + public async Task<ActionResult<Raffle>> Update(long id, RaffleForm form) { + var raffle = _mapper.Map<Raffle>(form); + raffle.Id = id; + var result = await _service.UpdateRaffle(raffle); + + if(result == null) + return NotFound("The resource couldn't be found."); + + return raffle; + } + + [Authorize(Roles = "Administrator")] + [HttpDelete("{id:int}")] + public async Task<ActionResult> Update(long id) { + var result = await _service.DeleteRaffle(id); + + if(!result) + return NotFound("The resource couldn't be found."); + + return StatusCode(303, new { Message = "The resource has been deleted"} ); + } + } +}
\ No newline at end of file |