public async Task <IActionResult> PutDayTotal(int id, DayTotal dayTotal) { if (id != dayTotal.DayTotalID) { return(BadRequest()); } _context.Entry(dayTotal).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DayTotalExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutFood(int id, Food food) { if (id != food.FoodID) { return(BadRequest()); } var f00d = await _context.Food.AsNoTracking().FirstOrDefaultAsync(f => f.FoodID == id); var dayTotal = await _context.DayTotal.FirstOrDefaultAsync(d => d.Date == food.Date); //If the entry is empty and we didnt give a blank food if (dayTotal == null && food.Carbs + food.Fats + food.Protein != 0) { DayTotal.TotalCarbs = food.Carbs * food.Servings; DayTotal.TotalFats = food.Fats * food.Servings; DayTotal.TotalProtein = food.Protein * food.Servings; DayTotal.Date = food.Date; DayTotal.RealDate = food.RealDate; _context.DayTotal.Add(DayTotal); } //if the entry exists and we didnt give it a blank food if (dayTotal != null) { dayTotal.TotalCarbs += (food.Carbs * food.Servings) - (f00d.Carbs * f00d.Servings); dayTotal.TotalFats += (food.Fats * food.Servings) - (f00d.Fats * f00d.Servings); dayTotal.TotalProtein += (food.Protein * food.Servings) - (f00d.Protein * f00d.Servings); dayTotal.Date = food.Date; if (dayTotal.TotalCarbs + dayTotal.TotalFats + dayTotal.TotalProtein == 0) { _context.DayTotal.Remove(dayTotal); } else { _context.Attach(dayTotal).State = EntityState.Modified; } } if (dayTotal != null && food.Carbs + food.Fats + food.Protein == 0) { _context.DayTotal.Remove(dayTotal); } _context.Entry(food).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FoodExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }