예제 #1
0
        public async Task <IActionResult> SearchArticles(ArticleSearchPagingOption options)
        {
            try
            {
                if ((options.Filter == ArticleFilterEnum.MyArticles || options.Filter == ArticleFilterEnum.MyFavorites ||
                     options.Filter == ArticleFilterEnum.MyLikes) && String.IsNullOrEmpty(options.ProfilerId))
                {
                    return(BadRequest(new { message = "ProfilerId is not specified in the search options" }));
                }

                var searchResult = await databaseService.SearchArticlesAsyc(options);

                return(Ok(searchResult));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
예제 #2
0
 public Task <ArticleSearchPagingResult> SearchArticlesAsyc(ArticleSearchPagingOption options)
 {
     throw new NotImplementedException();
 }
예제 #3
0
        public async Task <ArticleSearchPagingResult> SearchArticlesAsyc(ArticleSearchPagingOption options)
        {
            // Start with the articles collection
            var query = articles.AsQueryable();

            // filter the search key
            if (!string.IsNullOrEmpty(options.SearchKey))
            {
                query = query.Where(x => x.Content.ToLower().Contains(options.SearchKey.ToLower()) ||
                                    x.Title.ToLower().Contains(options.SearchKey.ToLower()));
            }

            // Join the needed collections
            var joinedQuery = GenerateArticleDetailsQuery(query, options.ProfilerId);

            switch (options.Filter)
            {
            case ArticleFilterEnum.MyArticles:
                joinedQuery = joinedQuery.Where(x => x.Author.Id == options.ProfilerId);
                break;

            case ArticleFilterEnum.MyLikes:
                joinedQuery = joinedQuery.Where(x => x.Rankings.Any(x => x.DidILike == true && x.UserId == options.ProfilerId));
                break;

            case ArticleFilterEnum.MyFavorites:
                joinedQuery = joinedQuery.Where(x => x.Rankings.Any(x => x.DidIFavor == true && x.UserId == options.ProfilerId));
                break;
            }

            switch (options.SortBy)
            {
            case SortByEnum.UpdatedOn:
                if (options.SortOrder == SortOrderEnum.Ascending)
                {
                    joinedQuery = joinedQuery.OrderBy(x => x.UpdatedOn);
                }
                else
                {
                    joinedQuery = joinedQuery.OrderByDescending(x => x.UpdatedOn);
                }
                break;

            case SortByEnum.Views:
                if (options.SortOrder == SortOrderEnum.Ascending)
                {
                    joinedQuery = joinedQuery.OrderBy(x => x.Views);
                }
                else
                {
                    joinedQuery = joinedQuery.OrderByDescending(x => x.Views);
                }
                break;

            case SortByEnum.Likes:
                if (options.SortOrder == SortOrderEnum.Ascending)
                {
                    joinedQuery = joinedQuery.OrderBy(x => x.Likes);
                }
                else
                {
                    joinedQuery = joinedQuery.OrderByDescending(x => x.Likes);
                }
                break;

            case SortByEnum.Favors:
                if (options.SortOrder == SortOrderEnum.Ascending)
                {
                    joinedQuery = joinedQuery.OrderBy(x => x.Favors);
                }
                else
                {
                    joinedQuery = joinedQuery.OrderByDescending(x => x.Favors);
                }
                break;
            }

            var docsCount = await joinedQuery.CountAsync();

            var documents = await joinedQuery.Skip((options.CurrentPage - 1) *options.PageSize).Take(options.PageSize).ToListAsync();

            var articleDetailsList = new List <ArticleDetails>();

            foreach (var item in documents)
            {
                item.Content = null; // Don't return the content
                var articleDetails = new ArticleDetails()
                {
                    Id            = item.Id,
                    Title         = item.Title,
                    Abstract      = item.Abstract,
                    UpdatedOn     = item.UpdatedOn,
                    CreatedOn     = item.CreatedOn,
                    IsApproved    = item.IsApproved,
                    IsPrivate     = item.IsPrivate,
                    Views         = item.Views,
                    Content       = item.Content,
                    Categories    = item.Categories,
                    GalleryImages = item.GalleryImages,
                    Author        = item.Author,
                    CommentsCount = item.CommentsCount,

                    Ranking = new ArticleRankingDetails(item.Id, options.ProfilerId, item.Rankings)
                };
                articleDetailsList.Add(articleDetails);
            }

            return(new ArticleSearchPagingResult(articleDetailsList, docsCount, options));
        }