public int LoadOverviewRecipesCount(IRequestRecipe requestData) { using (LudwigRecipeContext context = new LudwigRecipeContext()) { List <IRecipeOverviewData> data = new List <IRecipeOverviewData>(); var recipeQuery = context.Recipes.AsQueryable(); int categoryId = 0; if (!String.IsNullOrEmpty(requestData.CategoryUrl)) { Category category = context.Categories.FirstOrDefault(x => x.Url == requestData.CategoryUrl); if (category != null) { categoryId = category.Id; recipeQuery = recipeQuery.Where(x => x.Categories.FirstOrDefault(y => y.Id == category.Id) != null); } } int subCategoryId = 0; if (!String.IsNullOrEmpty(requestData.SubCategoryUrl)) { SubCategory subCategory = context.SubCategories.FirstOrDefault(x => x.Url == requestData.SubCategoryUrl); if (subCategory != null) { recipeQuery = recipeQuery.Where(x => x.SubCategories.FirstOrDefault(y => y.Id == subCategory.Id && y.CategoryId == categoryId) != null); } } if (requestData.ForPublicWeb) { recipeQuery = recipeQuery.Where(x => x.IsPublished == true && x.PublishDate <= DateTime.Now); } if (!requestData.IsFriend) { recipeQuery = recipeQuery.Where(x => x.IsOnlyForFriends == false); } try { var recipes = recipeQuery.ToList(); return(recipes.Count()); } catch (Exception exception) { } return(0); } }
public List <IRecipeOverviewData> LoadOverviewRecipes(IRequestRecipe requestData) { using (LudwigRecipeContext context = new LudwigRecipeContext()) { List <IRecipeOverviewData> data = new List <IRecipeOverviewData>(); var recipeQuery = context.Recipes.AsQueryable(); if (!String.IsNullOrEmpty(requestData.SearchTerm)) { recipeQuery = recipeQuery.Where(x => x.Name.Contains(requestData.SearchTerm)); } if (!String.IsNullOrEmpty(requestData.CategoryUrl)) { Category category = context.Categories.FirstOrDefault(x => x.Url == requestData.CategoryUrl); if (category != null) { recipeQuery = recipeQuery.Where(x => x.Categories.FirstOrDefault(y => y.Id == category.Id) != null); } } if (!String.IsNullOrEmpty(requestData.SubCategoryUrl)) { SubCategory subCategory = context.SubCategories.FirstOrDefault(x => x.Url == requestData.SubCategoryUrl); if (subCategory != null) { recipeQuery = recipeQuery.Where(x => x.SubCategories.FirstOrDefault(y => y.Id == subCategory.Id) != null); } } if (requestData.ForPublicWeb) { recipeQuery = recipeQuery.Where(x => x.IsPublished == true && x.PublishDate <= DateTime.Now); } if (!requestData.IsFriend) { recipeQuery = recipeQuery.Where(x => x.IsOnlyForFriends == false); } recipeQuery = recipeQuery.OrderByDescending(x => x.PublishDate); if (requestData.Skip > 0) { recipeQuery = recipeQuery.Skip(requestData.Skip); } if (requestData.Top > 0) { recipeQuery = recipeQuery.Take(requestData.Top); } try { var recipes = recipeQuery.ToList(); foreach (var recipe in recipes) { data.Add(new RecipeOverviewData() { Id = recipe.Id, Name = recipe.Name, Url = recipe.Url, Description = recipe.Description, PublishDate = recipe.PublishDate, TeaserImageUrl = recipe.TeaserImageUrl }); } } catch (Exception exception) { } return(data); } }