public async Task<ArticlesListModel> GetArticlesInCategory(string categoryUrl, string filter, string orderBy, SortDirection sortDirection, Guid? currentUserId) { ArticlesListModel articlesListModel = null; using (PoshBoutiqueData dataContext = new PoshBoutiqueData()) { Category category = await dataContext.Categories.FirstOrDefaultAsync(c => c.UrlName == categoryUrl); if (category == null && !categoryUrl.Equals(ArticlesProvider.ALL_CATEGORIES_URL_NAME, StringComparison.InvariantCultureIgnoreCase)) { return null; } articlesListModel = new ArticlesListModel(); articlesListModel.Category = new CategoriesConverter().ToModel(category); IQueryable<Article> articlesQuery = dataContext.Articles .Where(article => article.Visible); if (category != null) { articlesQuery = articlesQuery.Where(article => article.Categories.Any(c => c.Id == category.Id)); } if (!string.IsNullOrEmpty(filter)) { articlesQuery = articlesQuery .Where(article => article.Title.Contains(filter) || article.Description.Contains(filter) || article.MaterialDescription.Contains(filter)); } if (!string.IsNullOrEmpty(orderBy)) { articlesQuery = articlesQuery.Sort(orderBy, sortDirection); } List<Article> articlesList = await articlesQuery.ToListAsync(); HashSet<int> userLikes = this.GetUserLikes(currentUserId); ArticlesConverter converter = new ArticlesConverter(); articlesListModel.Articles = articlesList.Select(a => converter.ToModel(a, userLikes)).ToList(); } return articlesListModel; }
public async Task<IEnumerable<ArticleModel>> GetRecommendedArticles() { using (PoshBoutiqueData dataContext = new PoshBoutiqueData()) { Article[] articlesInCollection = await dataContext.Articles.Where(a => a.IsRecommended && a.Visible).OrderByDescending(a => a.DateCreated).ToArrayAsync(); if (articlesInCollection.Length == 0) { return null; } ArticlesConverter converter = new ArticlesConverter(); ArticleModel[] articlesInCollectionModels = articlesInCollection.Select(a => converter.ToModel(a, null)).ToArray(); return articlesInCollectionModels; } }
public IEnumerable<ArticleModel> GetArticlesByIds(IEnumerable<int> articleIds) { using (PoshBoutiqueData dataContext = new PoshBoutiqueData()) { Article[] articlesInCollection = dataContext.Articles.Where(a => articleIds.Contains(a.Id) && a.Visible).OrderByDescending(a => a.DateCreated).ToArray(); if (articlesInCollection.Length == 0) { return null; } ArticlesConverter converter = new ArticlesConverter(); ArticleModel[] articlesInCollectionModels = articlesInCollection.Select(a => converter.ToModel(a, null)).ToArray(); return articlesInCollectionModels; } }
public async Task<IEnumerable<ArticleModel>> GetLikedArticles(Guid userId) { using (PoshBoutiqueData dataContext = new PoshBoutiqueData()) { IQueryable<UserLike> userLikesQuery = dataContext.UserLikes.Where(ul => ul.UserId == userId); Article[] likedArticles = await dataContext.Articles.Join(userLikesQuery, a => a.Id, ul => ul.ArticleId, (a, ul) => a).OrderBy(a => a.Title).ToArrayAsync(); if (likedArticles.Length == 0) { return new ArticleModel[0]; } ArticlesConverter converter = new ArticlesConverter(); ArticleModel[] likedArticlesModels = likedArticles.Select(a => { ArticleModel likedArticleModel = converter.ToModel(a, null); likedArticleModel.IsLiked = true; return likedArticleModel; }).ToArray(); return likedArticlesModels; } }
public async Task<IEnumerable<ArticleModel>> GetFeaturedArticles(Guid? currentUserId) { using (PoshBoutiqueData dataContext = new PoshBoutiqueData()) { Article[] featuredArticles = await dataContext.Articles.Where(a => a.IsFeatured && a.Visible).OrderByDescending(a => a.DateCreated).ToArrayAsync(); if (featuredArticles.Length == 0) { return null; } HashSet<int> userLikes = this.GetUserLikes(currentUserId); ArticlesConverter converter = new ArticlesConverter(); ArticleModel[] featuredArticlesModels = featuredArticles.Select(a => converter.ToModel(a, userLikes)).ToArray(); return featuredArticlesModels; } }
public async Task<IEnumerable<ArticleModel>> GetArticlesInCollection(int collectionId, Guid? currentUserId) { using (PoshBoutiqueData dataContext = new PoshBoutiqueData()) { Article[] articlesInCollection = await dataContext.Articles.Where(a => a.CollectionId == collectionId && a.Visible).OrderByDescending(a => a.DateCreated).ToArrayAsync(); if (articlesInCollection.Length == 0) { return null; } HashSet<int> userLikes = this.GetUserLikes(currentUserId); ArticlesConverter converter = new ArticlesConverter(); ArticleModel[] articlesInCollectionModels = articlesInCollection.Select(a => converter.ToModel(a, userLikes)).ToArray(); return articlesInCollectionModels; } }
public async Task<IEnumerable<ArticleModel>> GetRelatedArticles(int articleId) { using (PoshBoutiqueData dataContext = new PoshBoutiqueData()) { Article parentArticle = await dataContext.Articles.FindAsync(articleId); if (parentArticle == null) { return null; } Article[] relatedArticles = parentArticle.RelatedArticles.OrderByDescending(a => a.DateCreated).ToArray(); if (relatedArticles.Length == 0) { return null; } ArticlesConverter converter = new ArticlesConverter(); ArticleModel[] relatedArticlesModels = relatedArticles.Select(a => converter.ToModel(a, null)).ToArray(); return relatedArticlesModels; } }