コード例 #1
0
ファイル: ArticleService.cs プロジェクト: Morr0/Atheer
        public async Task <ArticlePrimaryKey> Add(string userId, AddArticleRequest request)
        {
            var    article       = _articleFactory.Create(request, userId);
            string titleShrinked = article.TitleShrinked;

            // Check that no other article has same titleShrinked, else generate a new titleShrinked
            var key = new ArticlePrimaryKey(article.CreatedYear, titleShrinked);

            while (await Exists(key).CAF())
            {
                titleShrinked         = RandomiseExistingShrinkedTitle(ref titleShrinked);
                key.TitleShrinked     = titleShrinked;
                article.TitleShrinked = titleShrinked;
            }

            await using var transaction = await _context.Database.BeginTransactionAsync().CAF();

            await _context.Article.AddAsync(article).CAF();

            try
            {
                await _context.SaveChangesAsync().CAF();

                await transaction.CommitAsync().CAF();
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                _logger.LogError(e.StackTrace);
                throw new FailedOperationException();
            }

            return(key);
        }
コード例 #2
0
        public async Task <ArticlePrimaryKey> Add(string userId, AddArticleRequest request)
        {
            var article = _articleFactory.Create(request, userId);

            // Check that no other article has same id, else will generate another
            while (await Exists(article.Id).CAF())
            {
                article.Id = $"{article.Id}-";
            }

            await _client.Article().InsertOneAsync(article).CAF();

            return(new ArticlePrimaryKey
            {
                Id = article.Id,
            });
        }