diff options
author | HombreLaser <sebastian-440@live.com> | 2022-11-26 00:25:57 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2022-11-26 00:25:57 -0600 |
commit | 9fa6c5512cf229bd84ba131577f4a8d2b1efb770 (patch) | |
tree | 8defdcf005f152e9eb0c32eaa1be80156f3a0e66 /Controllers | |
parent | 9a59a9560eda09236e2dc56897d139c50a85c827 (diff) |
Añadida lógica de rifa
Diffstat (limited to 'Controllers')
-rw-r--r-- | Controllers/RafflesController.cs | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/Controllers/RafflesController.cs b/Controllers/RafflesController.cs index 26b5a96..82e05a4 100644 --- a/Controllers/RafflesController.cs +++ b/Controllers/RafflesController.cs @@ -2,6 +2,8 @@ using AutoMapper; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using BackendPIA.Forms; +using BackendPIA.Errors; +using BackendPIA.Logics; using BackendPIA.Models; using BackendPIA.Services; @@ -10,10 +12,12 @@ namespace BackendPIA.Controllers { [ApiController] public class RafflesController : ControllerBase { private readonly IRaffleService _service; + private readonly IGameService _game_service; private readonly IMapper _mapper; - public RafflesController(IMapper mapper, IRaffleService service) { + public RafflesController(IMapper mapper, IRaffleService service, IGameService game_service) { _service = service; + _game_service = game_service; _mapper = mapper; } @@ -55,7 +59,7 @@ namespace BackendPIA.Controllers { [Authorize(Roles = "Administrator")] [HttpDelete("{id:int}")] - public async Task<ActionResult> Update(long id) { + public async Task<ActionResult> Delete(long id) { var result = await _service.DeleteRaffle(id); if(!result) @@ -63,5 +67,29 @@ namespace BackendPIA.Controllers { return StatusCode(303, new { Message = "The resource has been deleted"} ); } + + [Authorize] + [HttpGet("{id:int}/available_tickets")] + public async Task<ActionResult<IEnumerable<int>>> AvailableTickets(long id) { + IEnumerable<int> available_tickets = from number in Enumerable.Range(1, 54) select number; + IEnumerable<int> taken_tickets = await _service.GetTakenTickets(id); + + if(!taken_tickets.Any()) + return NotFound(new NotFoundError(404, $"The raffle with id {id} doesn't exist or doesn't have any tickets.")); + + return Ok(new { Numbers = available_tickets.Except(taken_tickets) }); + } + + [Authorize(Roles = "Administrator")] + [HttpPost("{id:int}/play")] + public async Task<ActionResult<IEnumerable<RaffleWinner>>> Play(long id) { + RafflePlayLogic logic = new RafflePlayLogic(_game_service, _service, id); + bool result = await logic.Call(); + + if(!result) + return BadRequest(new { ErrorMessage = logic.ErrorMessage }); + + return Ok(logic.Winners); + } } }
\ No newline at end of file |