Ejemplo n.º 1
0
        public async Task <IActionResult> Reserve(string id, ReserveTicketInputModel input)
        {
            var ticketsInput = input.TicketInputModels.Select(x => x.Value);
            var projection   = await filmProjectionBusiness.GetAsync(id);

            if (projection == null)
            {
                return(RedirectToAction("Index", "Projections"));
            }

            //If same seats are added add error
            if (ticketsInput.Any(ticket => ticketsInput.Count(others => others.Seat == ticket.Seat) != 1))
            {
                ModelState.AddModelError("Duplicate", "Can't buy same seats more than once");
            }

            //Check for concurrency reservation conflicts
            var soldSeats      = projection.ProjectionTickets.Select(x => x.Seat).ToList();
            var availableSeats = Enumerable.Range(1, projection.TotalTickets).Where(x => !soldSeats.Contains(x));

            if (ticketsInput.Any(ticket => !availableSeats.Contains(ticket.Seat)))
            {
                ModelState.AddModelError("Reserved", "Someone got ahead of you and reserved one of your tickets.");
            }

            if (!ModelState.IsValid)
            {
                var orderedAvailableSeats = availableSeats.OrderBy(x => x).ToDictionary(x => x.ToString());
                input.AvailableSeats = new SelectList(orderedAvailableSeats, "Key", "Value");
                return(this.View(input));
            }

            var      userId    = userManager.GetUserId(User);
            DateTime timeStamp = DateTime.UtcNow;
            var      tickets   = ticketsInput.Select(ticket => mapper.MapToProjectionTicket(userId,
                                                                                            ticket,
                                                                                            projection,
                                                                                            timeStamp));
            await projectionTicketBusiness.AddMultipleAsync(tickets);

            return(RedirectToAction("Index", "Tickets"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Reserve(string id)
        {
            var projection = await filmProjectionBusiness.GetAsync(id);

            if (projection == null)
            {
                return(RedirectToAction("Index", "Projections"));
            }

            var soldSeats                      = projection.ProjectionTickets.Select(x => x.Seat).ToList();
            var availableSeats                 = Enumerable.Range(1, projection.TotalTickets).Where(x => !soldSeats.Contains(x));
            var orderedAvailableSeats          = availableSeats.OrderBy(x => x).ToDictionary(x => x.ToString());
            ReserveTicketInputModel inputModel = new ReserveTicketInputModel
            {
                AvailableSeats = new SelectList(orderedAvailableSeats, "Key", "Value"),
                ChildrenPrice  = projection.TicketPrices.ChildrenPrice,
                StudentPrice   = projection.TicketPrices.StudentPrice,
                AdultPrice     = projection.TicketPrices.AdultPrice,
            };

            return(View(inputModel));
        }