private async Task AddTags(Article article, IEnumerable <string> titles, bool setUpdated = false) { article.TagsIds ??= new List <string>(); foreach (string title in titles) { var tagId = _tagFactory.GetId(title); var tag = await(await _client.Tag().FindAsync(x => x.Id == tagId).CAF()).FirstOrDefaultAsync().CAF(); if (tag is null) { tag = _tagFactory.CreateTag(title); await _client.Tag().InsertOneAsync(tag).CAF(); } else if (setUpdated) { _tagFactory.UpdateTag(tag); } article.TagsIds.Add(tagId); } }
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())); }
private static async Task MigrateTags(Data originalDbContext, IMongoClient targetMongoClient, ILogger logger) { logger.LogCritical("Starting the tag collection"); var tagCollection = targetMongoClient.Tag(); await originalDbContext.Tag.AsNoTracking().ForEachAsync(async(tag) => { var targetTag = await(await tagCollection.FindAsync(x => x.Id == tag.Id).CAF()).FirstOrDefaultAsync() .CAF(); if (targetTag is null) { await tagCollection.InsertOneAsync(tag).CAF(); logger.LogCritical("Tag with id: {id} was added to MongoDB", tag.Id); } else { logger.LogCritical("Tag with id: {id} already exists in MongoDB", tag.Id); } }).CAF(); logger.LogCritical("Finished the tag collection"); }