public async Task <IActionResult> Delete([FromBody] FoodLogUpdateBindingModel model) { var user = await _userManager.GetUserAsync(User); var foodLog = _dbContext.FoodLogs.Find(model.Id); if (foodLog.UserId != user.Id) { return(Unauthorized("You are not authorized to delete this food log")); } _dbContext.FoodLogs.Remove(foodLog); await _dbContext.SaveChangesAsync(); return(Ok()); }
public async Task Add([FromBody] FoodLogUpdateBindingModel model) { var user = await _userManager.GetUserAsync(User); var food = _dbContext.Foods.Find(model.FoodId); var foodLog = new FoodLog() { FoodId = model.FoodId, Calories = food.CaloriesPer100Gr * model.Quantity / 100, Grams = model.Quantity, LoggedOn = DateTime.UtcNow, UserId = user.Id, FoodType = model.Type }; _dbContext.FoodLogs.Add(foodLog); await _dbContext.SaveChangesAsync(); }
public async Task <IActionResult> Update([FromBody] FoodLogUpdateBindingModel model) { var user = await _userManager.GetUserAsync(User); var foodLog = _dbContext.FoodLogs.Find(model.Id); if (foodLog.UserId != user.Id) { return(Unauthorized("You are not authorized to edit this food log")); } var food = await _dbContext.Foods.FindAsync(model.FoodId); foodLog.Calories = food.CaloriesPer100Gr * model.Quantity / 100; foodLog.FoodId = food.Id; foodLog.FoodType = model.Type; foodLog.Grams = model.Quantity; _dbContext.FoodLogs.Update(foodLog); await _dbContext.SaveChangesAsync(); return(Ok()); }