public IActionResult CreateNutrition(NutritionDTO data) { var user = db.Users.Include(x => x.Nutritions).FirstOrDefault(u => u.Username == data.Username); var nutrition = db.Nutrition.FirstOrDefault(n => n.UserId == user.Id); if (nutrition != null) { nutrition.Calories = data.Calories; nutrition.Protein = data.Protein; nutrition.Carbohydrates = data.Carbohydrates; nutrition.Fats = data.Fats; db.Nutrition.Update(nutrition); } else { nutrition = new Nutrition() { Calories = data.Calories, Protein = data.Protein, Carbohydrates = data.Carbohydrates, Fats = data.Fats }; user.Nutritions.Add(nutrition); } db.SaveChanges(); return(Ok()); }
public List <string> GetNutrition(NutritionDTO data) { var userId = db.Users.FirstOrDefault(x => x.Username == data.Username).Id; var nutrition = db.Nutrition.FirstOrDefault(x => x.UserId == userId); var list = new List <string>(); if (nutrition != null) { list.Add(nutrition.Calories); list.Add(nutrition.Protein); list.Add(nutrition.Carbohydrates); list.Add(nutrition.Fats); } return(list); }