using System; using System.Collections.Generic; using PizzaModel; using System.Data.Objects; using System.Linq; // Data access object for pizza order information // One of these is created for each transaction needing these calls // Has state: the EDM context object for this transaction, set up in service layer // Note no SaveChanges or transaction actions here--those are in the service layer namespace PizzaDAO { public class PizzaOrderDAO { private PizzaEntities context; // use a DAO for the lifetime of one context public PizzaOrderDAO(PizzaEntities ctx) { context = ctx; } public PizzaSize FindPizzaSizeByID(String sizeID) { ObjectQuery sizes = context.PizzaSize.Where("it.ID = " + sizeID); return sizes.First(); // or throw if none there } public List FindAllPizzaSizes() { return context.PizzaSize.ToList(); } public Topping FindToppingByID(String toppingID) { ObjectQuery toppings = context.Topping.Where("it.ID = " + toppingID); return toppings.First(); // or throw } public List FindAllToppings() { return context.Topping.ToList(); } public PizzaOrder InsertOrder(int roomNumber, PizzaSize size, List toppings, int day, int status) { PizzaOrder order = new PizzaOrder(); // add size to order--we only need to link in one direction, even tho a bidir association // i.e., we don't need to also execute size.Order.Add(order); order.PizzaSize = size; // add toppings to order-- foreach (Topping t in toppings) { order.Topping.Add(t); } order.Day = day; order.RoomNumber = roomNumber; order.Status = status; context.AddObject("PizzaOrder", order); return order; } // find first order with specified status public PizzaOrder FindFirstOrder(int status) { ObjectQuery orders = null; orders = context.PizzaOrder.Where("it.Status = " + status).OrderBy("it.ID").Top("1"); return orders.First(); } // Get orders, including toppings for a certain day and room number // and make sure they are pulled out of query within the Tx // by calling ToList, so they will be there after the Tx is finished. public List FindOrdersByRoom(int roomNumber, int day) { ObjectQuery orders = null; orders = context.PizzaOrder.Where("it.roomNumber = " + roomNumber) .Where("it.day = " + day) .Include("Topping").Include("PizzaSize"); return orders.ToList(); } // get all orders, without toppings, between day1 and day2 (inclusive) // The caller has to run the query here, by iterating through returned IEnumerable public IEnumerable FindOrdersByDays(int day1, int day2) { ObjectQuery orders = null; orders = context.PizzaOrder.Where("it.day >= " + day1) .Where("it.day <= " + day2); return orders; } } }