示例#1
0
        public async Task <ActionResult> ReserveSeat(int id, decimal price)
        {
            // check if there was a discount
            bool               discount   = false;
            TheatreSeat        newSeat    = new TheatreSeat();
            List <TheatreSeat> savedSeats = _ticketsToBuy.GetSelectedSeats();

            BookSeatResult bookSeatResult = new BookSeatResult()
            {
                Number = id
            };

            // look if seat already added, if yes remove it
            foreach (TheatreSeat savedSeat in savedSeats)
            {
                if (savedSeat.Id == id)
                {
                    _ticketsToBuy.DeleteSeat(id);
                    bookSeatResult.Result = "unsuccessful";

                    return(Json(bookSeatResult));
                }
            }

            // handle pricing if discount is to be applied
            DateTime time = DateTime.Now;

            if (((int)time.DayOfWeek < 5) && ((int)time.DayOfWeek > 0))
            {
                discount = true;
            }
            if (discount)
            {
                newSeat.Price        = price * 10 / 8;
                newSeat.ReducedPrice = price;
            }
            else
            {
                newSeat.Price = price;
            }

            //find the seat and check if not already booked
            var seat = await _context.BookedSeats.Include(x => x.Seat).FirstOrDefaultAsync(x => x.Id == id);

            if (seat.Booked != 2)
            {
                seat.ExpiryTime = DateTime.Now.AddMinutes(10);
                seat.Booked     = 1;

                newSeat.Booked       = seat.Booked;
                newSeat.ColumnLetter = seat.Seat.ColumnLetter;
                newSeat.Id           = seat.Id;
                newSeat.RowNumber    = seat.Seat.RowNumber;
                newSeat.Band         = seat.Seat.Band;

                // set a temporary lock on the seat
                try
                {
                    _context.Update(seat);
                    await _context.SaveChangesAsync();

                    bookSeatResult.Result = "finished successfully";
                }
                catch (DbUpdateConcurrencyException)
                {
                    bookSeatResult.Result = "unsuccessful";
                }
            }
            else if (seat.Booked == 2)
            {
                bookSeatResult.Message = "Seat could not be added, it is being booked by someone else.";
            }

            _ticketsToBuy.AddSeat(newSeat);


            return(Json(bookSeatResult));
        }
示例#2
0
文件: TicketsToBuy.cs 项目: mdLn1/SDP
 public void AddSeat(TheatreSeat seat)
 {
     selectedSeats.Add(seat);
 }