Ejemplo n.º 1
0
 public void SeatCustomer(DateTime when, int reservationId, List<byte> tables, int waiterId)
 {
     var availableSeats = AvailableSeatingByDateTime(when.Date, when.TimeOfDay);
     using (var context = new eRestaurantContext())
     {
         List<string> errors = new List<string>();
         // Rule checking:
         // - Reservation must be in Booked status
         // - Table must be available - typically a direct check on the table, but proxied based on the mocked time here
         // - Table must be big enough for the # of customers
         var reservation = context.Reservations.Find(reservationId);
         if (reservation == null)
             errors.Add("The specified reservation does not exist");
         if (reservation != null && reservation.ReservationStatus != Reservation.Booked)
             //not null and not booked
             errors.Add("The reservation's status is not valid for seating. Only booked reservations can be seated.");
         var capacity = 0;
         foreach (var tableNumber in tables)
         {
             if (!availableSeats.Exists(x => x.Table == tableNumber))
                 errors.Add("Table " + tableNumber + " is currently not available");
             else
                 capacity += availableSeats.Single(x => x.Table == tableNumber).Seating;
         }
         if (capacity < reservation.NumberInParty)
             errors.Add("Insufficient seating capacity for number of customers. Alternate tables must be used.");
         if (errors.Count > 0)
             throw new BusinessRuleException("Unable to seat customer", errors);
         // 1) Create a blank bill with assigned waiter
         Bill seatedCustomer = new Bill()
         {
             BillDate = when,
             NumberInParty = reservation.NumberInParty,
             WaiterID = waiterId,
             ReservationID = reservation.ReservationID
         };
         context.Bills.Add(seatedCustomer);
         // 2) Add the tables for the reservation and change the reservation's status to arrived
         foreach (var tableNumber in tables)
             reservation.Tables.Add(context.Tables.Single(x => x.TableNumber == tableNumber));
         reservation.ReservationStatus = Reservation.Arrived;
         var updatable = context.Entry(context.Reservations.Attach(reservation));
         updatable.Property(x => x.ReservationStatus).IsModified = true;
         //updatable.Reference(x=>x.Tables).
         // 3) Save changes one and one only/comitted the change/more than two will roll back to the old data;
         context.SaveChanges(); 
     }
     //string message = String.Format("Not yet implemented. Need to seat reservation {0} for waiter {1} at tables {2}", reservationId, waiterId, string.Join(", ", tables));
     //throw new NotImplementedException(message);
 }
Ejemplo n.º 2
0
        public void SeatCustomer(DateTime when, byte tablenumber,
                                 int numberinparty, int waiterid)
        {
            //business logic checking should be done
            //before attempting to place data on the database
            //rule 1: is the seat available
            //rule 2: is the selected table capacity sufficient

            //get the available seats
            var availableseats = AvailableSeatingByDateTime(when.Date, when.TimeOfDay);

            //start my transaction
            using (eRestaurantContext context = new eRestaurantContext())
            { 
                //my transaction
                //create a holding list for possible business logic errors
                //this is needed for the MessageUserControl
                List<string> errors = new List<string>();

                if (!availableseats.Exists(foreachseat => foreachseat.Table == tablenumber))
                { 
                    //the table number is not available
                    errors.Add("Table is currently not available");
                }
                else if (!availableseats.Exists(foreachseat => foreachseat.Table == tablenumber
                                                         &&foreachseat.Seating >= numberinparty))          
                    
                {
                    //the table is available but not large enough
                    errors.Add("insufficient seating capacity fro number of customers");
                }

                //check if any errors to business rules exist
                if (errors.Count > 0)
                {
                    //throw an exception which will terminate the transaction
                    //BusinessRule Exception is part of the MessageUserControl setup
                    throw new BusinessRuleException("Unable to seat customer", errors);
                 }
                    //assume your data is valid
                    //create an instance of the Bill entity and fill with data
                    Bill seatedcustomer = new Bill();
                    seatedcustomer.BillDate = when;
                    seatedcustomer.NumberInParty = numberinparty;
                    seatedcustomer.WaiterID = waiterid;
                    seatedcustomer.TableID = tablenumber;
                    //issue the command to add a record to the Bill entity 
                context.Bills.Add(seatedcustomer);
                //save and commit the changes to the entity
                context.SaveChanges();
               
            }//end of the transaction
        }