public async Task <bool> Handle(ArticleUpdate request, CancellationToken cancellationToken) { new ArticleUpdateValidator().ValidateAndThrow(request); var article = await DB.ArticlesOfUser(request.UserId) .Where(a => a.Id == request.Id) .Select(a => new { Article = a, a.ArticleCollection, }) .SingleOrDefaultAsync(cancellationToken: cancellationToken); if (article is null) { return(false); } var normalisedRequestText = request.Text?.Normalize(); article.Article.Name = request.Name?.Trim().Normalize() ?? article.Article.Name; article.Article.Text = normalisedRequestText ?? article.Article.Text; article.Article.ConlluDocument = normalisedRequestText is null ? article.Article.ConlluDocument : await ConlluService.ToConlluDocument(article.ArticleCollection.LanguageCode, normalisedRequestText); article.ArticleCollection.UpdatedAt = Clock.GetCurrentInstant(); return(true); }
public async Task <ArticleViewModel> Handle(ArticleCreate request, CancellationToken cancellationToken) { new ArticleCreateValidator().ValidateAndThrow(request); var articleCollectionQuery = DB.ArticleCollectionsOfUser(request.UserId) .Where(ac => ac.Id == request.ArticleCollectionId); var articleCollection = await articleCollectionQuery.SingleAsync(cancellationToken); var lastArticleOrderInCollection = await articleCollectionQuery .SelectMany(ac => ac.Articles) .OrderByDescending(a => a.Order) .Select(a => a.Order) .FirstOrDefaultAsync(cancellationToken); var now = Clock.GetCurrentInstant(); articleCollection.UpdatedAt = now; var normalisedText = request.Text.Normalize(); var article = new Article { Id = Guid.NewGuid(), ArticleCollectionId = articleCollection.Id, // It's fine to start from 1 as we don't really care the exact number. Order = lastArticleOrderInCollection + 1, Name = request.Name.Trim().Normalize(), Text = normalisedText, ConlluDocument = await ConlluService.ToConlluDocument(articleCollection.LanguageCode, normalisedText), CreatedAt = now, UpdatedAt = now, }; await DB.Unsafe.AddAsync(article, cancellationToken); var vm = Mapper.Map <ArticleViewModel>(article); // FIXME: Find a better way to not do this hack. vm.ReadingProgress = new(); return(vm); }