コード例 #1
0
        private List <ArticleViewModel> GetFullArticles(GetList query)
        {
            var isLoggedIn = false;

            User user = null;

            if (User.Identity.Name != null)
            {
                user = UserService.Get(long.Parse(User.Identity.Name));

                if (user != null)
                {
                    isLoggedIn = true;
                }
            }

            var result = new List <ArticleViewModel>();

            var articles = ArticleService.GetAllArticles()
                           .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted)
                           .Skip((query.PageNumber - 1) * query.PageSize)
                           .Take(query.PageSize)
                           .ToList();

            articles.ForEach(article =>
            {
                var likes = ArticleLikeService.GetAll()
                            .Where(x => x.Article == article)
                            .ToList();

                var comments = ArticleCommentService.GetAll()
                               .Where(x => x.Article == article)
                               .ToList();

                var isLiked = false;

                if (isLoggedIn)
                {
                    if (likes.FirstOrDefault(x => x.Author == user) != null)
                    {
                        isLiked = true;
                    }
                }

                var articleViewModel           = new ArticleViewModel(article, likes, comments, isLiked);
                articleViewModel.Author.Rating = ReviewService.GetSpecialistRating(article.Author);

                result.Add(articleViewModel);
            });

            return(result);
        }
コード例 #2
0
        public IActionResult LikeArticle(long id)
        {
            var article = ArticleService.Get(id);

            if (article == null)
            {
                return(NotFound(new ResponseModel
                {
                    Success = false,
                    Message = "Статья не найдена"
                }));
            }

            User user = null;

            if (User.Identity.Name != null)
            {
                user = UserService.Get(long.Parse(User.Identity.Name));
            }

            if (user == null)
            {
                return(NotFound(new ResponseModel
                {
                    Success = false,
                    Message = "Пользователь не найден"
                }));
            }

            var like = ArticleLikeService.GetAll()
                       .FirstOrDefault(x => x.Article == article && x.Author == user);

            if (like != null)
            {
                ArticleLikeService.Delete(like);
            }
            else
            {
                ArticleLikeService.Create(like = new ArticleLike
                {
                    Article = article,
                    Author  = user
                });
            }

            return(Ok(new ResponseModel()));
        }
コード例 #3
0
        private ArticleViewModel GetFullArticle(long id)
        {
            var article = ArticleService.Get(id);

            if (article == null)
            {
                return(null);
            }

            var isLoggedIn = false;

            User user = null;

            if (User.Identity.Name != null)
            {
                user = UserService.Get(long.Parse(User.Identity.Name));

                if (user != null)
                {
                    isLoggedIn = true;
                }
            }

            var likes = ArticleLikeService.GetAll()
                        .Where(x => x.Article == article)
                        .ToList();

            var comments = ArticleCommentService.GetArticleComments(article);

            var isLiked = false;

            if (isLoggedIn)
            {
                if (likes.FirstOrDefault(x => x.Author == user) != null)
                {
                    isLiked = true;
                }
            }

            var result = new ArticleViewModel(article, likes, comments, isLiked);

            result.Author.Rating = ReviewService.GetSpecialistRating(article.Author);

            return(result);
        }
コード例 #4
0
        private List <ArticleViewModel> GetFullArticles(GetArticlesList query)
        {
            var isLoggedIn = false;

            User user = null;

            if (User.Identity.Name != null)
            {
                user = UserService.Get(long.Parse(User.Identity.Name));

                if (user != null)
                {
                    isLoggedIn = true;
                }
            }

            var result   = new List <ArticleViewModel>();
            var articles = new List <Article>();

            if (query.OrderBy == OrderBy.ASC)
            {
                switch (query.SortBy)
                {
                case ArticlesSort.Comments:
                {
                    articles.AddRange(ArticleService.GetAll()
                                      .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList()
                                      .OrderBy(x => ArticleCommentService.GetArticleCommentsCount(x))
                                      .Skip((query.PageNumber - 1) * query.PageSize)
                                      .Take(query.PageSize)
                                      .ToList());

                    break;
                }

                case ArticlesSort.Likes:
                {
                    articles.AddRange(ArticleService.GetAll()
                                      .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList()
                                      .OrderBy(x => ArticleLikeService.GetArticleLikesCount(x))
                                      .Skip((query.PageNumber - 1) * query.PageSize)
                                      .Take(query.PageSize)
                                      .ToList());

                    break;
                }

                case ArticlesSort.Date:
                {
                    articles.AddRange(ArticleService.GetAll()
                                      .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList()
                                      .OrderBy(x => x.Date)
                                      .Skip((query.PageNumber - 1) * query.PageSize)
                                      .Take(query.PageSize)
                                      .ToList());

                    break;
                }

                default:
                {
                    articles.AddRange(ArticleService.GetAll()
                                      .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList()
                                      .OrderBy(x => x.Date)
                                      .Skip((query.PageNumber - 1) * query.PageSize)
                                      .Take(query.PageSize)
                                      .ToList());

                    break;
                }
                }
            }
            else
            {
                switch (query.SortBy)
                {
                case ArticlesSort.Comments:
                {
                    articles.AddRange(ArticleService.GetAll()
                                      .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList()
                                      .OrderByDescending(x => ArticleCommentService.GetArticleCommentsCount(x))
                                      .Skip((query.PageNumber - 1) * query.PageSize)
                                      .Take(query.PageSize)
                                      .ToList());

                    break;
                }

                case ArticlesSort.Likes:
                {
                    articles.AddRange(ArticleService.GetAll()
                                      .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList()
                                      .OrderByDescending(x => ArticleLikeService.GetArticleLikesCount(x))
                                      .Skip((query.PageNumber - 1) * query.PageSize)
                                      .Take(query.PageSize)
                                      .ToList());

                    break;
                }

                case ArticlesSort.Date:
                {
                    articles.AddRange(ArticleService.GetAll()
                                      .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList()
                                      .OrderByDescending(x => x.Date)
                                      .Skip((query.PageNumber - 1) * query.PageSize)
                                      .Take(query.PageSize)
                                      .ToList());

                    break;
                }

                default:
                {
                    articles.AddRange(ArticleService.GetAll()
                                      .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList()
                                      .OrderByDescending(x => x.Date)
                                      .Skip((query.PageNumber - 1) * query.PageSize)
                                      .Take(query.PageSize)
                                      .ToList());

                    break;
                }
                }
            }

            articles.ForEach(article =>
            {
                var likes = ArticleLikeService.GetAll()
                            .Where(x => x.Article == article)
                            .ToList();

                var comments = ArticleCommentService.GetAll()
                               .Where(x => x.Article == article)
                               .ToList();

                var isLiked = false;

                if (isLoggedIn)
                {
                    if (likes.FirstOrDefault(x => x.Author == user) != null)
                    {
                        isLiked = true;
                    }
                }

                var articleViewModel           = new ArticleViewModel(article, likes, comments, isLiked);
                articleViewModel.Author.Rating = ReviewService.GetSpecialistRating(article.Author);

                result.Add(articleViewModel);
            });

            return(result);
        }