private static WeeklyMenuViewModel GetWeeklyMenuAsViewModel(WeeklyMenu weeklyMenu, User user) { var viewModel = new WeeklyMenuViewModel { Id = weeklyMenu.Id, WeekNumber = weeklyMenu.WeekNumber, Description = weeklyMenu.Description, Approved = weeklyMenu.Approved, Recipes = new List<WeeklyMenuRecipeViewModel>() }; for (var index = 0; index < weeklyMenu.Recipes.Count; index++) { var recipe = weeklyMenu.Recipes[index]; var date = weeklyMenu.PublishDate.AddDays(index); var isFavorite = user.Favorites.FirstOrDefault(_ => _.Id == recipe.Id); viewModel.Recipes.Add(new WeeklyMenuRecipeViewModel { Date = DateTime.Now.AddDays(index).ToString("d/MM", CultureInfo.InvariantCulture), Id = recipe.Id, DayName = DayName(index), Name = recipe.Name, ImageUrl = recipe.ImageUrl, Rate = recipe.Rate, Time = recipe.Time, IsCurrent = date.Date == DateTime.Now.Date, IsFavorite = isFavorite != null, Severity = recipe.Severity, Description = recipe.Description }); } return viewModel; }
public HttpResponseMessage Put(WeeklyMenuViewModel viewModel) { _weeklyMenuService.UpdateWeeklyMenu(viewModel); return new HttpResponseMessage(HttpStatusCode.OK); }
public void UpdateWeeklyMenu(WeeklyMenuViewModel viewModel) { var weeklyMenu = _unitOfWork.WeeklyMenus.Get(_ => _.Id == viewModel.Id); weeklyMenu.Approved = viewModel.Approved; weeklyMenu.Description = viewModel.Description; weeklyMenu.Recipes.Clear(); foreach (var recipeToAdd in viewModel.Recipes.Select(recipe => _unitOfWork.Recipies.Get(_ => _.Id == recipe.Id))) { weeklyMenu.Recipes.Add(recipeToAdd); } _unitOfWork.Commit(); if (weeklyMenu.Approved) { _shoppingListService.CreateShoppingList(weeklyMenu); } }