using System; using System.Collections.Generic; using System.Transactions; using PizzaDAO; using PizzaModel; // stateless singleton object for student part of the service API namespace PizzaService { public class StudentService { public StudentService() { } public List AllPizzaSizes() { List all; using (PizzaEntities context = new PizzaEntities()) { PizzaOrderDAO orderDAO = new PizzaOrderDAO(context); try { using (TransactionScope tx = new TransactionScope()) { all = orderDAO.FindAllPizzaSizes(); tx.Complete(); } } catch (Exception e) { throw new ApplicationException("Error in accessing Pizza Sizes" + e.InnerException.Message, e); } } return all; } public List AllToppings() { List all; using (PizzaEntities context = new PizzaEntities()) { PizzaOrderDAO orderDAO = new PizzaOrderDAO(context); try { using (TransactionScope tx = new TransactionScope()) { all = orderDAO.FindAllToppings(); tx.Complete(); } } catch (Exception e) { throw new ApplicationException("Error in accessing Toppings" + e.InnerException.Message, e); } } return all; } // returns order ID public int MakeOrder(int roomNumber, String sizeID, List toppingIDs) { using (PizzaEntities context = new PizzaEntities()) { PizzaOrderDAO orderDAO = new PizzaOrderDAO(context); AdminDAO adminDAO = new AdminDAO(context); try { using (TransactionScope tx = new TransactionScope()) { PizzaSize size = orderDAO.FindPizzaSizeByID(sizeID); List tops = new List(); foreach (String t in toppingIDs) { Topping top = orderDAO.FindToppingByID(t); tops.Add(top); } // here with all good toppings, good size int day = adminDAO.FindCurrentDay(); int status = (int)PizzaOrder.OrderStatus.Preparing; PizzaOrder o = orderDAO.InsertOrder(roomNumber, size, tops, day, status); context.SaveChanges(); tx.Complete(); return o.ID; } } catch (Exception e) { throw new ApplicationException( "order insert failed" + e.Message, e); } } } // This provides PizzaOrder objects, with Topping and PizzaSize objects, // to the presentation layer, after the transaction is committed. // So any changes to them at that point have no effect on the DB. // The service API is set up so as not to accept domain objects // from the presentation layer, only ids specifying them. public List OrderStatus(int roomNumber) { List pizzaOrders = null; using (PizzaEntities context = new PizzaEntities()) { PizzaOrderDAO orderDAO = new PizzaOrderDAO(context); AdminDAO adminDAO = new AdminDAO(context); try { using (TransactionScope tx = new TransactionScope()) { pizzaOrders = orderDAO.FindOrdersByRoom(roomNumber, adminDAO.FindCurrentDay()); foreach (PizzaOrder order in pizzaOrders) { if (order.Status == (int)PizzaOrder.OrderStatus.Baked) // a student has seen it ready, so mark this pizza "finished" order.Status = (int)PizzaOrder.OrderStatus.Finished; } context.SaveChanges(); tx.Complete(); } } catch (Exception e) { throw new ApplicationException("Error in getting status" + e.InnerException.Message, e); } } return pizzaOrders; } } }