public void AddListValue(ViewProducts vp) { using (ApplicationDbContext ctx = new ApplicationDbContext()) { var SetValue = from e in ctx.Products join e2 in ctx.Orderdetails on e.Id equals e2.ProductId select new { PID = e.Id, PNA = e.ProductName, Qua = e.Quantity, Pri = e.Price, Amo = e2.Amount }; foreach (var item in SetValue) { ShowIngrediens si = new ShowIngrediens(); si.ProductID = item.PID; si.ProductName = item.PNA; si.Quantity = item.Qua; si.Price = item.Pri; si.Amount = item.Amo; vp.TotalSum += (decimal.ToDouble(si.Price) * si.Amount); vp.UserID = User.FindFirstValue(ClaimTypes.NameIdentifier); vp.Productslist.Add(si); } } }
public IActionResult Varukorg(int ID, string Empty, int RemoveID, int ProductID, string Apply) { ViewProducts vp = new ViewProducts(); var varukorg = Request.Cookies.SingleOrDefault(c => c.Key == "Varukorg"); if (RemoveID == 0) { using (ApplicationDbContext ctx = new ApplicationDbContext()) { string cookieString; var recipeProductsIds = from e in ctx.RecipeDetails where e.RecipeId == ID select e.ProductId; if (Apply == "Add" || Apply == "Remove") { cookieString = varukorg.Value; } else if (varukorg.Value != "" && varukorg.Value != null && ID != 0) { cookieString = varukorg.Value + "," + string.Join(",", recipeProductsIds); } else { cookieString = varukorg.Value + string.Join(",", recipeProductsIds); } if (cookieString == "") { RemoveFromOrderdetails(); } var productIds = cookieString.Split(",").Select(c => int.Parse(c)); var Check = from e in ctx.Orderdetails select e; if (Check.Count() == 0) { var products = from e in ctx.Products where productIds.Contains(e.Id) select e; if (!cookieString.Equals("")) { foreach (var item in products) { Orderdetails od = new Orderdetails(); od.ProductId = item.Id; od.Amount = 1; using (ApplicationDbContext c = new ApplicationDbContext()) { if (!c.Orderdetails.Any(A => A.ProductId == item.Id)) { c.Orderdetails.Add(od); c.SaveChanges(); } } ShowIngrediens si = new ShowIngrediens(); si.ProductID = item.Id; si.ProductName = item.ProductName; si.Quantity = item.Quantity; si.Price = item.Price; si.Amount = 1; vp.TotalSum += (decimal.ToDouble(si.Price) * si.Amount); vp.UserID = User.FindFirstValue(ClaimTypes.NameIdentifier); vp.Productslist.Add(si); } } } else { using (ApplicationDbContext c = new ApplicationDbContext()) { var modify = from m in c.Orderdetails where m.ProductId == ProductID select m; if (Apply == "Add") { foreach (var items in modify) { items.Amount++; } } else if (Apply == "Remove") { foreach (var items in modify) { items.Amount--; if (items.Amount == 0) { RemoveItem(ProductID, vp); vp.StatusMessage = "Produkten har tagits bort"; return(View(vp)); } } } c.SaveChanges(); foreach (var additem in productIds) { Orderdetails od = new Orderdetails() { ProductId = additem, Amount = 1 }; if (!c.Orderdetails.Any(A => A.ProductId == additem)) { c.Orderdetails.Add(od); c.SaveChanges(); } } } AddListValue(vp); } Response.Cookies.Append("Varukorg", cookieString, new Microsoft.AspNetCore.Http.CookieOptions { Expires = DateTime.Now.AddMinutes(60.0) }); ctx.SaveChanges(); } } if (Empty == "Empty") { using (ApplicationDbContext ctx = new ApplicationDbContext()) { foreach (var item in ctx.Orderdetails) { ctx.Orderdetails.Remove(item); } ctx.SaveChanges(); Response.Cookies.Delete("Varukorg"); vp.Productslist.Clear(); vp.TotalSum = 0; return(View(vp)); } } else if (RemoveID != 0) { RemoveItem(RemoveID, vp); } return(View(vp)); }
public IActionResult ViewRecipe(int id, int portion = 4) { if (portion < 1) { portion = 1; } ViewProducts vp = new ViewProducts(); using (SqlConnection con = new SqlConnection("Server=(localdb)\\Mssqllocaldb; Database= TranbarDB; MultipleActiveResultSets=true")) { con.Open(); string SQL = @"select Recipename,EstimatedTime, ProductName, ProductQuantity,Measurement,RE.Id, PR.Id from Products PR INNER JOIN RecipeDetails RD ON PR.ID = RD.ProductID INNER JOIN MeasurementUnit MU ON RD.MeasurementUnitID = MU.Id INNER JOIN Recipe RE ON RD.RecipeID = RE.ID where RE.Id = @ID"; SqlCommand cmd = new SqlCommand(SQL, con); cmd.Parameters.AddWithValue("@ID", id); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { ShowIngrediens SI = new ShowIngrediens(); vp.RecipeID = rdr.GetInt32(5); vp.RecipeName = rdr.GetString(0); vp.EstimatedTime = rdr.GetInt32(1); SI.ProductName = rdr.GetString(2); SI.ProductQuantity = rdr.GetDecimal(3); SI.Measurement = rdr.GetString(4); vp.Productslist.Add(SI); } con.Close(); // Andra queryn -- Hämtar Stegnr och instruktioner con.Open(); string SQL2 = @"select Stepnumber, Instructions from Recipe r inner join RecipeSteps rs on rs.RecipeID = r.ID where r.ID = @ID2"; SqlCommand command = new SqlCommand(SQL2, con); command.Parameters.AddWithValue("@ID2", id); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { RecipeSteps RS = new RecipeSteps(); RS.Stepnumber = reader.GetInt32(0); RS.Instructions = reader.GetString(1); vp.StepList.Add(RS); } con.Close(); } vp.Portion = portion; if (portion >= 1) { vp.Productslist.ForEach(pl => { pl.ProductQuantity = portion * (pl.ProductQuantity * 1 / 4); }); } ; return(View(vp)); }