public static bool SellSomeShawarma(string name) { if (name == null) { throw new ArgumentNullException(nameof(name)); } using (var sb = new ShawarmaBaseEntities()) { Shawarma shawarma = sb.Shawarma.FirstOrDefault(shaw => shaw.ShawarmaName == name); if (shawarma == null) { return(false); } foreach (var ingradient in shawarma.ShawarmaRecipe) { if (ingradient.Weight > ingradient.Ingradient.TotalWeight) { return(false); } ingradient.Ingradient.TotalWeight -= ingradient.Weight; } return(SaveIt(sb)); } }
public static bool SetNewPrice(string name, decimal price, string comment, string sellingPointTitle) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (comment == null) { throw new ArgumentNullException(nameof(comment)); } if (sellingPointTitle == null) { throw new ArgumentNullException(nameof(sellingPointTitle)); } if (price <= 0) { throw new ArgumentOutOfRangeException(nameof(price)); } using (var sb = new ShawarmaBaseEntities()) { Shawarma shawarma = sb.Shawarma.FirstOrDefault(sh => sh.ShawarmaName == name); if (shawarma == null) { return(false); } SellingPoint sp = sb.SellingPoint.FirstOrDefault(s => s.ShawarmaTitle == sellingPointTitle); if (sp == null) { return(false); } PriceController pc = sb.PriceController.FirstOrDefault(p => p.ShawarmaId == shawarma.ShawarmaId && p.SellingPointId == sp.SellingPointId); if (pc == null) { pc = new PriceController { Price = price, Comment = comment, SellingPointId = sp.SellingPointId, ShawarmaId = shawarma.ShawarmaId }; sb.PriceController.Add(pc); } else { pc.Price = price; pc.Comment = comment; } return(SaveIt(sb)); } }
public static bool CreateRecipe(string name, string[] ingradients, int[] weights, int cookingTime) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (ingradients == null || weights == null) { throw new ArgumentNullException(nameof(ingradients)); } if (ingradients.Length == 0 || weights.Length == 0 || ingradients.Length != weights.Length) { throw new ArgumentException(); } using (var sb = new ShawarmaBaseEntities()) { if (!sb.Shawarma.Any(recipe => recipe.ShawarmaName == name)) { return(false); } Shawarma shawarma = new Shawarma { ShawarmaName = name, CookingTime = cookingTime }; sb.Shawarma.Add(shawarma); if (sb.Ingradient.Count(ingr => ingradients.Contains(ingr.IngradientName)) != ingradients.Length) { return(false); } var ingradientsInBase = sb.Ingradient.Where(ingr => ingradients.Contains(ingr.IngradientName)); for (int i = 0; i < ingradients.Length; i++) { var sr = new ShawarmaRecipe { IngradientId = ingradientsInBase.First(ing => ing.IngradientName == ingradients[i]).IngradientId, ShawarmaId = shawarma.ShawarmaId, Weight = weights[i] }; sb.ShawarmaRecipe.Add(sr); } return(SaveIt(sb)); } }