public ReservationModel MakeReservation(int projectionId, CreateReservationModel model)
        {
            return this.ExecuteOperationHandleExceptions(() =>
            {
                this.ValidateEmail(model.Email);

                var context = new CinemaReserveDbContext();
                var theProjection = context.Projections.FirstOrDefault(pr => pr.Id == projectionId);
                if (theProjection == null)
                {
                    throw new ArgumentNullException("projection is either non-existant or is not available");
                }

                var reservedSeatStatus = context.SeatStatus.FirstOrDefault(st => st.Status == "reserved") ?? new SeatStatus()
                {
                    Status = "reserved"
                };
                
                List<Seat> reservedSeats = new List<Seat>();

                using (TransactionScope tran = new TransactionScope())
                {
                    foreach (var seat in model.Seats)
                    {
                        var dbSeat = theProjection.Seats.FirstOrDefault(s => s.Row == seat.Row && s.Column == seat.Column);
                        if (dbSeat == null || dbSeat.Status.Status == "reserved")
                        {
                            throw new InvalidOperationException("Seat is not available");
                        }
                        dbSeat.Status = reservedSeatStatus;
                        reservedSeats.Add(dbSeat);
                    }
                    tran.Complete();
                }

                var reservation = new Reservation()
                {
                    ReservedSeats = reservedSeats,
                    UserEmail = model.Email.ToLower(),
                    UserCode = this.GenerateUserCode()
                };


                //context.Reservations.Add(reservation);
                theProjection.Reservations.Add(reservation);
                context.SaveChanges();

                var reservationModel = Parser.ToReservationModel(reservation);

                return reservationModel;
            });
        }
Exemplo n.º 2
0
 internal static ReservationModel ToReservationModel(Reservation reservation)
 {
     return new ReservationModel()
     {
         UserCode = reservation.UserCode
     };
 }