Exemple #1
0
        private static async Task MigrateArticles(Data originalDbContext, IMongoClient targetMongoClient,
                                                  ILogger logger)
        {
            logger.LogCritical("Starting the article collection");
            var articleCollection = targetMongoClient.Article();

            await originalDbContext.Article.AsNoTracking()
            .Include(x => x.Tags)
            .ForEachAsync(async(article) =>
            {
                string articleId  = $"{article.CreatedYear.ToString()}-{article.TitleShrinked}";
                var targetArticle = await(await articleCollection.FindAsync(x => x.Id == articleId).CAF()).FirstOrDefaultAsync()
                                    .CAF();

                if (targetArticle is null)
                {
                    article.Id      = articleId;
                    article.TagsIds = article.Tags.Select(x => x.TagId).ToList();

                    await articleCollection.InsertOneAsync(article).CAF();
                    logger.LogCritical("Article with id: {id} was added to MongoDB", articleId);
                }
                else
                {
                    logger.LogCritical("Article with id: {id} already exists in MongoDB", articleId);
                }
            }).CAF();

            logger.LogCritical("Finished the article collection");
        }
Exemple #2
0
        public async Task AddOrUpdateTagsPerArticle(ArticlePrimaryKey articlePrimaryKey, IEnumerable <string> titles)
        {
            var article = await(await _client.Article().FindAsync(x => x.Id == articlePrimaryKey.Id).CAF())
                          .FirstOrDefaultAsync().CAF();

            if (article?.TagsIds?.Count > 0)
            {
                await UpdateTags(article, titles).CAF();
            }
            else
            {
                await AddTags(article, titles).CAF();
            }

            await _client.Article().FindOneAndReplaceAsync(x => x.Id == articlePrimaryKey.Id, article).CAF();
        }
Exemple #3
0
        public async Task <ArticleViewModel> Get(ArticlePrimaryKey primaryKey, string viewerUserId = null)
        {
            var article = await(await _client.Article().FindAsync(x => x.Id == primaryKey.Id).CAF())
                          .FirstOrDefaultAsync().CAF();

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

            var author = await(await _client.User().FindAsync(x => x.Id == article.AuthorId).CAF())
                         .FirstOrDefaultAsync().CAF();
            var tags = (await(await _client.Tag().FindAsync(x => article.TagsIds.Contains(x.Id)).CAF()).ToListAsync()
                        .CAF());

            // TODO enable article series to bring articles in same series

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