public bool CreateMeal(MealCreate model) { var entity = new Meal() { UserId = _userId, MealName = model.MealName, Category = model.Category, ListOfFoodIds = model.ListOfFoodIds, CreatedUtc = DateTimeOffset.Now.Date }; using (var ctx = new ApplicationDbContext()) { foreach (int i in entity.ListOfFoodIds) { var foodMealEntity = new FoodMeal() { MealId = entity.MealId, FoodId = i }; ctx.FoodMeals.Add(foodMealEntity); entity.ListOfFoods.Add(ctx.FoodItems.Find(i)); } ctx.DailyMeals.Add(entity); return(ctx.SaveChanges() > 0); } }
public bool UpdateMeal(MealEdit model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .DailyMeals .Single(e => e.MealId == model.MealId && e.UserId == _userId); var foodMealsToRemove = ctx.FoodMeals.Where(e => e.MealId == entity.MealId); foreach (FoodMeal item in foodMealsToRemove) { ctx.FoodMeals.Remove(item); } foreach (int i in model.ListOfFoodIds) { var foodMealEntity = new FoodMeal() { MealId = model.MealId, FoodId = i }; ctx.FoodMeals.Add(foodMealEntity); } entity.MealName = model.MealName; entity.Category = model.Category; entity.ListOfFoodIds = model.ListOfFoodIds; entity.ModifiedUtc = DateTimeOffset.UtcNow.Date; return(ctx.SaveChanges() > 0); } }
private void Reset() { _foodMeal = new FoodMeal(); GbAddEdit.DataContext = _foodMeal; BtnSave.Content = GbAddEdit.Header = "افزودن"; BtnSave.Background = new SolidColorBrush(Color.FromRgb(92, 184, 92)); BindGrid(); }
private void GrdList_OnMouseDoubleClick(object sender, MouseButtonEventArgs e) { if (GrdList.Items.Count > 0) { _foodMeal = (FoodMeal)GrdList.SelectedItem; GbAddEdit.DataContext = _foodMeal; GbAddEdit.Header = $"ویرایش '{_foodMeal.Title}'"; BtnSave.Content = "ویرایش"; BtnSave.Background = new SolidColorBrush(Color.FromRgb(247, 194, 44)); } }
public FoodItemDetail GetFoodItemById(int id) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .FoodItems .Single(e => e.FoodId == id && e.UserId == _userId); List <int> meals = entity.ListOfMealIds; foreach (int i in entity.ListOfMealIds) { var foodMealEntity = new FoodMeal() { FoodId = entity.FoodId, MealId = i }; ctx.FoodMeals.Find(foodMealEntity); } var foodMealList = GetFoodMealsByFoodId(id); foreach (FoodMealListItem foodMeal in foodMealList) { entity.ListOfMealIds.Add(foodMeal.MealId); } var mealList = new List <string>(); foreach (int i in entity.ListOfMealIds) { mealList.Add(ctx.DailyMeals.Find(i).MealName); } return (new FoodItemDetail { FoodId = entity.FoodId, FoodName = entity.FoodName, Calories = entity.Calories, Protein = entity.Protein, Fat = entity.Fat, Carbs = entity.Carbs, CreatedUtc = entity.CreatedUtc.Date, ModifiedUtc = entity.ModifiedUtc, ListOfMealNames = mealList }); } }