public void SeatCustomer(DateTime when, int reservationId, List<byte> tables, int waiterId) { var availableSeats = AvailableSeatingByDateTime(when.Date, when.TimeOfDay); using (var context = new RestaurantContext()) { 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"); else if (reservation != null && reservation.ReservationStatus != Reservation.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 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); }
//the ccommand from the code behind is NOT using an ODS //therefore it does NOT need [DataObjectMethod] public void SeatCustomer(DateTime when, byte tablenumber, int numberinparty, int waiterid) { //business logic checking should be done before any database processing //rule 1: table must be available //rule 2: ttable size must be greater than or = to numberinparty //get the available seats var availableseats = AvailableSeatingByDateTime(when.Date, when.TimeOfDay); //start the transaction using (eRestaurantContext context = new eRestaurantContext()) { //create a list<> to hold error messages List<string> errors = new List<string>(); if (!availableseats.Exists(foreachseat => foreachseat.Table == tablenumber)) { //table is not available errors.Add("Table is currently not available"); } if (!availableseats.Exists(foreahcseat => foreahcseat.Table == tablenumber && foreahcseat.Seating >= numberinparty)) { errors.Add("Insufficient seating capacity for number of customers"); } //check of validation if (errors.Count > 0) { //there is a business rule exception throw new BusinessRuleException("Unable to seat customer", errors); } //we can assume that the data is valid at this point //in our system as soon as a customer is seated a Bill is started Bill seatcustomer = new Bill(); seatcustomer.BillDate = when; seatcustomer.NumberInParty = numberinparty; seatcustomer.WaiterID = waiterid; seatcustomer.TableID = tablenumber; seatcustomer.PaidStatus = false; //make the request to entityframework to add a record to the database context.Bills.Add(seatcustomer); //commit context.SaveChanges(); } //end of transaction }
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 //rule1: is the seat available //rule2: is the selected table capicity sufficicent // get the available seats var availableseatrs = AvailableSeatingByDateTime(when.Date, when.TimeOfDay); // start my transaction using (eRestaurantContext context = new eRestaurantContext()) { //create a holding list for possible business logic //this is need for the MessageUserControl List<string> errors = new List<string>(); if (!availableseatrs.Exists(foreachseat => foreachseat.Table == tablenumber)) { // the table numberis not available errors.Add("Table is currently not availabe"); } else if (!availableseatrs.Exists(foreachseat => foreachseat.Table == tablenumber && foreachseat.Seating >= numberinparty)) { //the table is available but not large enough errors.Add("Insufficient seating capacity for number of customers"); } //check if any errors to business rules exist if (errors.Count > 0) { //throw an exception which will terminate the transaction //BusinessRuleException 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 sand commin the changes to the entity context.SaveChanges(); } } }
public void SeatCustomer(DateTime when, byte tablenumber, int numberinparty, int waiterID) { //buisness logic checking sould be done before atampting to place the record on the DB. //rule 1 - is the seat abailable //rule2 - is the selected table capacity sufficiant // get the available seats var availabeseats = AvailableSeatingByDateTime(when.Date, when.Time) using (eRestaurantContext context = new eRestaurantContext) { //creaste a holding list for possinle Business logic errors //this is need for the Messege User Control List<string> errors = new List<string>(); if (!availabeseats.Exists(foreachseat => foreachseat.Table == tablenumber)) { errors.Add("Table is currently not available"); } else if(!availabeseats.Exists(foreachseat => foreachseat.Table == tablenumber && foreachseat.Seating >= numberinparty)) { errors.Add("Insufficient seating capacity"); } //chcek if any erros excits if(errors.Count > 0) { //throw an exception so the trasnaction exits throw new BusinessRuleException("Unable to seat Customer",errors); } //assume the data is valid //creat a instance of Bill Entity and fill with data Bill seatedcustomer = new Bill(); seatedcustomer.BillDate = when; seatedcustomer.NumberInParty = numberinparty; seatedcustomer.WaiterID = waiterID; seatedcustomer.TableID = tablenumber; context.Bills.Add(seatedcustomer); context.SaveChanges(); }//edn of transaction }