Ejemplo n.º 1
0
        public async Task <IActionResult> Index([FromForm] ReserveTicketResource resource)
        {
            var claimId = _claimsService.GetUserId(HttpContext.User.Identity as ClaimsIdentity);

            if (claimId == 0)
            {
                return(Unauthorized("You don't have permission to create a place"));
            }
            resource.UserId = claimId;

            var user = await _userRepository.GetUserByIdAsync(resource.UserId);

            if (user is null)
            {
                return(NotFound("User doesn't exists"));
            }

            var e = await _eventRepository.GetEventById(resource.EventId);

            if (e is null)
            {
                return(NotFound("Event doesn't exists"));
            }

            if (e.BeginDate <= DateTime.UtcNow)
            {
                return(BadRequest("Event is from past"));
            }

            var seatExists = await _seatRepository.AreAllSeatsInEventPlace(resource.SeatId, resource.EventId);

            if (!seatExists)
            {
                return(NotFound("Some seats not exists"));
            }

            var seatTaken = await _ticketRepository.AreTicketsExists(resource.EventId, resource.SeatId);

            if (!seatTaken)
            {
                return(BadRequest("Some seats are busy"));
            }

            await _ticketService.CreateTicket(resource);

            return(Ok("Ticket/s reserved"));
        }