summaryrefslogtreecommitdiff
path: root/Controllers/TicketsController.cs
blob: 97a4a26a89a9097f7c6bd63171defbfc5b7e88c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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."});
        }
    }
}