public async Task <IActionResult> EditDessert([FromBody] DessertInfo dessertInfo) { string emailAddress = HttpContext.User.Identity.Name; var restaurant = await _context.Restaurant .Where(rest => rest.EmailAddress.Equals(emailAddress)) .FirstOrDefaultAsync(); string dessertName = Request.Headers["dessertName"]; Dessert dessertToUpdate = await _context.Dessert .Where(d => d.Name.Equals(dessertName)) .FirstOrDefaultAsync(); dessertToUpdate.Name = dessertInfo.Name; dessertToUpdate.Price = dessertInfo.Price; dessertToUpdate.Description = dessertInfo.Description; dessertToUpdate.HouseSpecial = dessertInfo.HouseSpecial; if (ModelState.IsValid) { try { _context.Update(dessertToUpdate); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } } HttpContext.Response.StatusCode = 200; return(new JsonResult("Dessert successfully updated")); }
//[ValidateAntiForgeryToken] public async Task <JsonResult> CreateDessert([FromBody] DessertInfo dessertInfo) { string emailAddress = HttpContext.User.Identity.Name; var restaurant = await _context.Restaurant .Where(rest => rest.EmailAddress.Equals(emailAddress)) .FirstOrDefaultAsync(); if (checkDessertNameInSameRestaurant(dessertInfo.Name, restaurant.Id)) { HttpContext.Response.StatusCode = 400; return(new JsonResult("Dessert " + dessertInfo.Name + " already exists in restaurant")); } Dessert newDessert = new Dessert { Name = dessertInfo.Name, Description = dessertInfo.Description, Price = dessertInfo.Price, HouseSpecial = dessertInfo.HouseSpecial }; if (ModelState.IsValid) { newDessert.RestaurantId = restaurant.Id; _context.Add(newDessert); await _context.SaveChangesAsync(); //return RedirectToAction(nameof(Index), new { id = id }); } HttpContext.Response.StatusCode = 200; return(new JsonResult("Dessert successfully created")); }
public static DocumentModel CreateDocumentModel(OutputDocument outputDoc) { DocumentModel docModel = new DocumentModel(); docModel.IssueDate = outputDoc.IssueDate; foreach (var docLine in outputDoc.Lines) { DessertInfo dessert = new DessertInfo(); dessert.Name = docLine.Dessert.Name; dessert.Quantity = docLine.Quantity; foreach (var outputDocProdLine in docLine.OutputDocLineProducts) { Ingredient ingredient = new Ingredient(); ingredient.Name = outputDocProdLine.Product.ProductDetail.Name; ingredient.Quantity = outputDocProdLine.CalcualtedQuantity; ingredient.Unit = outputDocProdLine.Product.ProductDetail.Unit; ingredient.BatchNumber = outputDocProdLine.Product.BatchNumber; dessert.Ingredients.Add(ingredient); } dessert.Ingredients = dessert.Ingredients.OrderBy(x => x.Name).ToList(); docModel.Desserts.Add(dessert); } return(docModel); }
public List <DessertInfo> GetDessertsList() { List <Dessert> desserts = this.dessertShop.GetDesserts(); List <DessertInfo> dessertsList = new List <DessertInfo>(); foreach (Dessert dessert in desserts) { DessertInfo dessertInfo = new DessertInfo { Name = dessert.Name, Price = dessert.Price }; dessertsList.Add(dessertInfo); } return(dessertsList); }
public async Task <JsonResult> Desserts() { string emailAddress = HttpContext.User.Identity.Name; var restaurant = await _context.Restaurant .Where(rest => rest.EmailAddress.Equals(emailAddress)) .FirstOrDefaultAsync(); List <DessertInfo> dessertsToReturn = new List <DessertInfo>(); List <Dessert> restaurantDesserts = await _context.Dessert .Where(d => d.RestaurantId == restaurant.Id) .ToListAsync(); foreach (Dessert dessert in restaurantDesserts) { DessertInfo dessertInfo = new DessertInfo(dessert.Name, dessert.Price, dessert.Description, dessert.HouseSpecial); dessertsToReturn.Add(dessertInfo); } return(new JsonResult(dessertsToReturn)); }
private DocumentModel CreateDocumentModel(Dictionary <int, int> idsToQuantities) { Cursor = Cursors.WaitCursor; DocumentModel docModel = new DocumentModel(); docModel.IssueDate = DateTime.Now; //Saving OutputDocument OutputDocument outputDoc = new OutputDocument(); outputDoc.IssueDate = DateTime.Now.Date; using (var db = new PastryShopDbContext()) { using (var tr = db.Database.BeginTransaction()) { foreach (var item in idsToQuantities) { Dessert dessert = db.Desserts.Include("RecipeLines.ProductDetail").FirstOrDefault(d => d.Id == item.Key); DessertInfo dessertInfo = new DessertInfo(); dessertInfo.Name = dessert.Name; dessertInfo.Quantity = item.Value; //Saving OutputDocument OutputDocumentLine outputDocLine = new OutputDocumentLine(); outputDocLine.Quantity = item.Value; outputDocLine.Dessert = dessert; outputDocLine.OutputDocument = outputDoc; foreach (var recipeLine in dessert.RecipeLines) { var dateNow = DateTime.Now.Date; var productsInStore = db.Products .Where(p => p.ProductDetail.Id == recipeLine.ProductDetail.Id && p.AvailableQuantity > 0 && p.ExpiryDate >= dateNow ); int productsInStoreCount = productsInStore.Count(); double neededQuantity = item.Value * recipeLine.Quantity; var productInDbSum = productsInStore.Sum(p => (double?)(p.AvailableQuantity)) ?? 0; if (productsInStoreCount == 0 || productInDbSum < neededQuantity) { throw new Exception("Няма достатъчно количество " + recipeLine.ProductDetail.Name + " за " + dessert.Name); } while (neededQuantity > 0) { var product = productsInStore.FirstOrDefault(p => p.AvailableQuantity > 0 && p.ExpiryDate >= dateNow); // product can be null if at the time of execution // the query expiry date become < DateTime.Now.Date if (product == null) { throw new Exception("Няма достатъчно количество " + recipeLine.ProductDetail.Name + " за " + dessert.Name); } Ingredient ingredient = new Ingredient(); ingredient.Name = recipeLine.ProductDetail.Name; ingredient.Unit = recipeLine.ProductDetail.Unit; ingredient.BatchNumber = product.BatchNumber; //Saving OutputDocument OutputDocLineProduct outDocLine = new OutputDocLineProduct(); outDocLine.OutputDocLine = outputDocLine; outDocLine.Product = product; if (product.AvailableQuantity >= neededQuantity) { product.AvailableQuantity -= neededQuantity; ingredient.Quantity = neededQuantity; outDocLine.CalcualtedQuantity = neededQuantity; neededQuantity = 0; } else { neededQuantity -= product.AvailableQuantity; ingredient.Quantity = product.AvailableQuantity; product.AvailableQuantity = 0; } //Saving OutputDocument outDocLine.CalcualtedQuantity = ingredient.Quantity; outputDocLine.OutputDocLineProducts.Add(outDocLine); dessertInfo.Ingredients.Add(ingredient); db.SaveChanges(); } } outputDocLine.OutputDocument = outputDoc; outputDoc.Lines.Add(outputDocLine); docModel.Desserts.Add(dessertInfo); } db.OutputDocuments.Add(outputDoc); db.SaveChanges(); tr.Commit(); } } Cursor = Cursors.Default; return(docModel); }