diff options
author | HombreLaser <sebastian-440@live.com> | 2022-11-23 17:56:41 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2022-11-23 17:56:41 -0600 |
commit | ccfe519bc75a95bae4f8f99b0b73a90c367f6272 (patch) | |
tree | c5ffaeed2fb03a5fbe92781e7e4f8823d7d233e0 /Controllers | |
parent | 8ea8f4f9a9bbb6db9abe1294163d2444d1e5da7b (diff) |
Añadido controlador de tickets
Diffstat (limited to 'Controllers')
-rw-r--r-- | Controllers/TicketsController.cs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/Controllers/TicketsController.cs b/Controllers/TicketsController.cs new file mode 100644 index 0000000..97a4a26 --- /dev/null +++ b/Controllers/TicketsController.cs @@ -0,0 +1,74 @@ +using AutoMapper; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using BackendPIA.Errors; +using BackendPIA.Forms; +using BackendPIA.Logics; +using BackendPIA.Models; +using BackendPIA.Services; + +namespace BackendPIA.Controllers { + [Route("api/raffles/{raffleId:int}/tickets")] + [ApiController] + public class TicketsController : ControllerBase { + private readonly IMapper _mapper; + private readonly ITicketService _ticket_service; + private readonly ApplicationDbContext _context; + private readonly UserManager<UserAccount> _manager; + + public TicketsController(IMapper mapper, ITicketService service, ApplicationDbContext context, UserManager<UserAccount> manager) { + _mapper = mapper; + _ticket_service = service; + _context = context; + _manager = manager; + } + + [Authorize] + [HttpGet] + public async Task<ActionResult<IEnumerable<TicketDTO>>> Index(long raffleId) { + var result = await _ticket_service.GetTickets(raffleId); + + if(result == null) + return StatusCode(404, new NotFoundError(404, "The resource doesn't exist")); + + return Ok(_mapper.Map<List<TicketDTO>>(result)); + } + + [Authorize] + [HttpGet("{id:int}")] + public async Task<ActionResult<TicketDTO>> Show(long raffleId, long id) { + var result = await _ticket_service.GetTicket(raffleId, id); + + if(result == null) + return StatusCode(404, new NotFoundError(404, "The resource doesn't exist")); + + return Ok(_mapper.Map<TicketDTO>(result)); + } + + [Authorize] + [HttpPost] + public async Task <ActionResult<TicketDTO>> Create(long raffleId, TicketForm form) { + string email = HttpContext.User.Claims.Where(c => c.Type.Contains("email")).First().Value; + var user = await _manager.FindByEmailAsync(email); + CreateTicketLogic logic = new CreateTicketLogic(_ticket_service, _context, _mapper, form, user, raffleId); + var result = await logic.Call(); + + if(!result) + return StatusCode(422, new InvalidInputError(422, logic.ErrorMessage)); + + return Ok(_mapper.Map<TicketDTO>(logic.Created)); + } + + [Authorize(Roles = "Administrator")] + [HttpDelete("{id:int}")] + public async Task <ActionResult> Delete(long raffleId, long id) { + bool result = await _ticket_service.DeleteTicket(raffleId, id); + + if(!result) + return StatusCode(404, new NotFoundError(404, "The ticket couldn't be found.")); + + return Ok(new { Message = "The ticket has been deleted."}); + } + } +}
\ No newline at end of file |