Пример #1
0
        private RecipeModel Load(int id = 0)
        {
            var model = GetModel();

            if (id != 0)
            {
                var recipe = RecipeRepository.GetById(id);
                if (recipe != null)
                {
                    var category         = RecipeRepository.GetCategoryById(recipe.CategoryId).HtmlDecode();
                    var specials         = RecipeRepository.GetSpecialsForRecipe(recipe.Guid);
                    var selectedSpecials = (from item in specials
                                            from special in model.AllSpecials
                                            where item.SpecialId == special.Id
                                            select special).ToList();
                    var selectedSpecialsAsString = (from item in specials
                                                    from special in model.AllSpecials
                                                    where item.SpecialId == special.Id
                                                    select special).Aggregate(string.Empty, (current, special) => current + (special.Name.ToLower() + ", "));
                    var dishType  = string.Empty;
                    var avgRating = RatingRepository.GetAvarage(recipe.Guid);
                    if (recipe.DishTypeId.HasValue)
                    {
                        dishType = RecipeRepository.GetDishTypeById(recipe.DishTypeId.Value);
                    }

                    if (selectedSpecialsAsString.EndsWith(", "))
                    {
                        selectedSpecialsAsString = selectedSpecialsAsString.TrimEnd(',', ' ');
                    }

                    model.Recipe = recipe;
                    model.Recipe.Name.HtmlDecode();
                    model.Recipe.Ingredients.HtmlDecode();
                    model.Recipe.Description.HtmlDecode();
                    model.Category             = category;
                    model.DishType             = dishType;
                    model.AvarageRating        = avgRating;
                    model.Specials             = selectedSpecialsAsString;
                    model.SelectedSpecials     = selectedSpecials;
                    SessionHandler.CurrentGuid = model.Recipe.Guid;
                    SessionHandler.CurrentId   = model.Recipe.Id.ToString();
                }
                else
                {
                    LogHandler.Log(nameof(RecipeController), LogType.Info, string.Format("Recipe with id: {0} could not be found", id));
                    throw new HttpException(404, "Not found");
                }
            }
            return(model);
        }