public bool UpdateSnacksQuantity(UsersFruits object1) { UsersFruits userFruits = new UsersFruits(); bool update = false; using (ApplicationDbContext db = new ApplicationDbContext()) { try { userFruits = db.UsersFruits.Where(x => x.FruitId == object1.FruitId && x.UserId == object1.UserId).FirstOrDefault(); userFruits.QuantitySnacks = object1.QuantitySnacks; if (object1.QuantityBreakfast > 0 || object1.QuantityDinner > 0 || object1.QuantityLunch > 0 || object1.QuantitySnacks > 0) { db.UsersFruits.Attach(userFruits); db.Entry(userFruits).State = System.Data.Entity.EntityState.Modified; update = true; } else { db.UsersFruits.Remove(userFruits); } db.SaveChanges(); } catch { } } return(update); }
public void FindByIdAndChangeStateChosenSnacks(int id, string userId, double quantity) { UsersFruits userfruits = new UsersFruits(); Fruit fruit; using (ApplicationDbContext db = new ApplicationDbContext()) { fruit = db.Fruits.SingleOrDefault(x => x.FruitId == id); userfruits = db.UsersFruits.SingleOrDefault(x => x.UserId == userId && x.FruitId == id); if (userfruits == null) { UsersFruits userfruits1 = new UsersFruits() { FruitId = id, UserId = userId, QuantitySnacks = quantity }; db.UsersFruits.Add(userfruits1); } else { userfruits.QuantitySnacks = quantity; } db.SaveChanges(); } }
public IHttpActionResult Post(UsersFruits object1) { if (!ModelState.IsValid) { return(BadRequest()); } _userFruitsRepositoty.CreateQuantity(object1); return(Ok()); }
public void CreateQuantity(UsersFruits object1) { UsersFruits userFruits = new UsersFruits(); using (ApplicationDbContext db = new ApplicationDbContext()) { db.UsersFruits.Add(new UsersFruits { UserId = object1.UserId, FruitId = object1.FruitId, QuantityBreakfast = object1.QuantityBreakfast, QuantityDinner = object1.QuantityDinner, QuantityLunch = object1.QuantityLunch, QuantitySnacks = object1.QuantitySnacks }); db.SaveChanges(); } }
public IHttpActionResult Patch(UsersFruits object1) { return(Ok(_userFruitsRepositoty.UpdateLunchQuantity(object1))); }