public int ModifyCartItem(CartModel model, int id) { try { ScUserCart cart = new ScUserCart(); cart.UserCartId = id; cart.UserId = model.UserId; cart.ItemId = model.ItemId; cart.Quantity = model.Quantity; dbContext.Entry(cart).State = Microsoft.EntityFrameworkCore.EntityState.Modified; dbContext.SaveChanges(); return(1); } catch (Exception ex) { return(0); } }
public int DeleteCartItem(int id) { try { ScUserCart kid = dbContext.ScUserCart.Find(id); if (kid != null) { dbContext.ScUserCart.Remove(kid); dbContext.SaveChanges(); return(1); } else { return(0); } } catch (Exception ex) { return(0); } }
public int AddToCart(CartModel model) { try { var item = (from cart in dbContext.ScUserCart where cart.UserId == model.UserId && cart.ItemId == model.ItemId select cart).FirstOrDefault(); if (item != null) { ScUserCart userCart = dbContext.ScUserCart.Find(item.UserCartId); userCart.UserCartId = userCart.UserCartId; userCart.UserId = model.UserId; userCart.ItemId = model.ItemId; userCart.Quantity = item.Quantity + model.Quantity; dbContext.Entry(userCart).State = Microsoft.EntityFrameworkCore.EntityState.Modified; dbContext.SaveChanges(); model.UserCartId = userCart.UserCartId; } else { ScUserCart newCart = new ScUserCart(); newCart.UserId = model.UserId; newCart.ItemId = model.ItemId; newCart.Quantity = model.Quantity; dbContext.Add(newCart); dbContext.SaveChanges(); model.UserCartId = newCart.UserCartId; } return(model.UserCartId); } catch (Exception ex) { return(0); } }