public void Clear(ClassLibrary1.Cart model) { var check = DB.Carts.Where(x => x.ProductID == model.ProductID && x.UserID == model.UserID).FirstOrDefault(); DB.Entry(check).State = Microsoft.EntityFrameworkCore.EntityState.Deleted; DB.SaveChanges(); }
public int Total(ClassLibrary1.Cart model) { int total = 0; var query = from r in DB.Carts where r.UserID == model.UserID select r; foreach (var i in query) { total = total + i.Total; } return(total); }
public bool CartRemove(ClassLibrary1.Cart model) { var check = DB.Carts.Where(x => x.ProductID == model.ProductID && x.UserID == model.UserID).FirstOrDefault(); if (check.Quantity == 1) { DB.Entry(check).State = Microsoft.EntityFrameworkCore.EntityState.Deleted; DB.SaveChanges(); return(true); } else if (check.Quantity > 1) { check.Quantity = check.Quantity - model.Quantity; check.Total = check.Total - (model.Quantity * check.Price); DB.SaveChanges(); return(true); } else { return(false); } }
public bool CartAdd(ClassLibrary1.Cart model) { var available = DB.Products.Where(x => x.productId == model.ProductID).FirstOrDefault(); int stock = available.ProductStock; if (stock > model.Quantity) { var check = DB.Carts.Where(x => x.ProductID == model.ProductID && x.UserID == model.UserID).FirstOrDefault(); if (check == null) { DB.Carts.Add(model); DB.SaveChanges(); var query = DB.Carts.Where(x => x.ID == model.ID).FirstOrDefault(); var query2 = DB.Products.Where(x => x.productId == model.ProductID).FirstOrDefault(); int price1 = query2.ProductPrice; string image = query2.ProductImage; string name = query2.ProductName; query.Price = price1; query.ProductImage = image; query.ProductName = name; query.Total = (query.Quantity * price1); DB.SaveChanges(); return(true); } else { check.Quantity = check.Quantity + model.Quantity; check.Total = check.Total + (model.Quantity * check.Price); DB.SaveChanges(); return(true); } } else { return(false); } }
public IEnumerable <ClassLibrary1.Cart> GetCart(ClassLibrary1.Cart model) { var query = from r in DB.Carts where r.UserID == model.UserID select r; return(query); }