Example #1
0
 public ArticleViewModel(Atheer.Models.Article article, IList <Tag> tags, string authorFullName, ArticleSeriesArticles seriesArticles)
 {
     Article        = article;
     Tags           = tags;
     AuthorFullName = authorFullName;
     SeriesArticles = seriesArticles;
 }
Example #2
0
        public async Task <ArticleViewModel> Get(ArticlePrimaryKey key, string viewerUserId = null)
        {
            var article = await _context.Article.AsNoTracking()
                          .Include(x => x.Series)
                          .FirstOrDefaultAsync(x => x.CreatedYear == key.CreatedYear && x.TitleShrinked == key.TitleShrinked)
                          .CAF();

            if (article is null)
            {
                return(null);
            }
            if ((!article.EverPublished || article.ForceFullyUnlisted || article.Draft) && article.AuthorId != viewerUserId)
            {
                return(null);
            }
            // if (article.Draft && article.AuthorId != viewerUserId) return null;

            // Get author full name
            var author = await _context.User.AsNoTracking()
                         .Select(x => new
            {
                x.Id,
                x.Name
            })
                         .FirstOrDefaultAsync(x => x.Id == article.AuthorId).ConfigureAwait(false);

            // Get tags associated with article
            var tags = await(from ta in _context.TagArticle
                             join t in _context.Tag.AsNoTracking() on ta.TagId equals t.Id
                             where
                             ta.ArticleCreatedYear == key.CreatedYear &&
                             ta.ArticleTitleShrinked == key.TitleShrinked
                             select t
                             ).ToListAsync().ConfigureAwait(false);

            ArticleSeriesArticles articleSeriesArticles = new ArticleSeriesArticles();

            if (article.Series is not null)
            {
                var lightArticleViews = await _context.Article.AsNoTracking()
                                        .Where(x => x.SeriesId == article.SeriesId)
                                        .OrderBy(x => x.CreatedAt)
                                        .Select(x => new LightArticleView
                {
                    Title         = x.Title,
                    TitleShrinked = x.TitleShrinked,
                    CreatedYear   = x.CreatedYear
                })
                                        .ToListAsync().ConfigureAwait(false);

                articleSeriesArticles.SeriesId    = article.Series.Id;
                articleSeriesArticles.SeriesTitle = article.Series.Title;
                articleSeriesArticles.Articles    = lightArticleViews;
            }

            return(new ArticleViewModel(article, tags, author.Name, articleSeriesArticles));
        }