Exemple #1
0
        public async Task <IActionResult> BuyTicket(SessionBuyTicketModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }
                var orderid = await _appOrder.Create(model);

                return(RedirectToAction("Checkout", "Order", new { id = orderid, idSession = model.Id }));
            }
            catch (Exception e)
            {
                ViewBag.Success = false;
                ViewBag.Message = e.Message;
                return(View(model));
            }
        }
        public async Task <int> Create(SessionBuyTicketModel model)
        {
            if (!model.Seats.Any(x => x.Selected))
            {
                throw new Exception("Selecione pelo menos 1 poltrona");
            }
            try
            {
                var session = await _context.Sessions
                              .Include(x => x.Room)
                              .Include(x => x.Movie)
                              .FirstOrDefaultAsync(x => x.Id == model.Id);

                var selectedSeats = model.Seats.Where(x => x.Selected).ToList();

                var seats = await _context.Seats.Where(x => selectedSeats.Select(y => y.Id).Contains(x.Id)).ToListAsync();

                var order = new Order(DateTime.Now);

                foreach (var item in seats)
                {
                    var ticket    = new Ticket(session, item);
                    var orderitem = new OrderItem(order, ticket, 1, null, 20);
                    order.OrderItems.Add(orderitem);
                }
                order.Calculate();
                await _context.Orders.AddAsync(order);

                await _context.SaveChangesAsync();

                return(order.Id);
            }
            catch
            {
                throw new Exception("Não foi possível realizar a operação. Tente novamente");
            }
        }