public PizzaRepository UpdatePizza(int pizzaId, PizzaRepository pizzaBody) { PizzaRepository pizza = new PizzaRepository(); try { var pizzaProps = typeof(PizzaRepository).GetProperties(); using (var db = new PizzaDbContext()) { foreach (var pizzaProp in pizzaProps) { pizza = db.Pizza.Where(e => e.pizzaId == pizzaId).FirstOrDefault(); string pizzaPropToString = pizzaProp.Name; bool isPrimaryId = Regex.IsMatch(pizzaPropToString, @"\w+Id$"); var pizzaProperty = pizzaBody.GetType().GetProperty(pizzaPropToString); if (!isPrimaryId) { if (pizzaProperty.GetValue(pizzaBody) != null) { var getPizzaBodyPropValue = pizzaProp.GetValue(pizzaBody); pizza.GetType().GetProperty(pizzaPropToString).SetValue(pizza, pizzaProperty.GetValue(pizzaBody)); pizzaProperty.SetValue(pizza, pizzaProp.GetValue(pizzaBody)); } } } db.SaveChanges(); } } catch (Exception) { return(null); } return(pizza); }
// TODO : Methods for the order and orderItem table public OrderRepository GetNewOrderToInsert() { PizzaRepository pizzaList = new PizzaRepository(); int pizzaIdToOrderFirst = pizzaList.GetAllPizzas().Select(pizz => pizz.pizzaId).FirstOrDefault(); int pizzaIdToOrderLast = pizzaList.GetAllPizzas().Select(pizz => pizz.pizzaId).LastOrDefault(); List <OrderItemRepository> newOrderItems = new List <OrderItemRepository>() { new OrderItemRepository() { pizzaItem = pizzaIdToOrderFirst }, new OrderItemRepository() { pizzaItem = pizzaIdToOrderLast } }; OrderRepository newOrder = new OrderRepository() { customerName = "Kinder", customerPhone = "0000000000", orderItems = newOrderItems }; return(newOrder); }
// TODO : Implement all proper exection handling and response message public int CreateOrder(PizzaRepository pizzaBody) { try { using (var db = new PizzaDbContext()) { db.Pizza.Add(pizzaBody); db.SaveChanges(); } } catch (Exception) { return(0); } return(pizzaBody.pizzaId); }
public PizzaRepository GetPizza(int pizzaId) { PizzaRepository getPizza = new PizzaRepository(); try { using (var db = new PizzaDbContext()) { getPizza = db.Pizza.Where(pizz => pizz.pizzaId == pizzaId).First(); } } catch (Exception) { return(null); } return(getPizza); }
public PizzaRepository UpdatePizzaToppingList(int pizzaid) { PizzaRepository pizza = new PizzaRepository(); try { using (var db = new PizzaDbContext()) { pizza = db.Pizza.Where(element => element.pizzaId == pizzaid).FirstOrDefault(); var pizzaToppings = new List <PizzaToppingsRepository>(); pizzaToppings = db.PizzaToppings.Where(e => e.pizzaId == pizzaid).ToList(); pizza.toppingsList = pizzaToppings; } } catch (Exception) { return(null); } return(pizza); }