Пример #1
0
        public async Task <IResultModel> Add(ArticleAddModel model)
        {
            var entity = _mapper.Map <ArticleEntity>(model);
            //if (await _repository.Exists(entity))
            //{
            //return ResultModel.HasExists;
            //}

            var result = await _repository.AddAsync(entity);

            return(ResultModel.Result(result));
        }
Пример #2
0
        public async Task <Article> AddAsync(Article article, CancellationToken cancellationToken)
        {
            article.Id = _idGenerator.Generate();

            var ret = await _repository.GetByTitleAsync(article.Title, cancellationToken);

            if (ret != null && !string.IsNullOrEmpty(ret.Title))
            {
                return(await _repository.AddAsync(article, cancellationToken));
            }

            return(null);
        }
Пример #3
0
        public async Task <IActionResult> Post(ArticleDto article)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var articleToInsert = _mapper.Map <Article>(article);

            await _articleRepository.AddAsync(articleToInsert);

            await _unitOfWork.SaveChangesAsync();

            return(Ok(articleToInsert));
        }
Пример #4
0
        public async Task <ArticleResponse> SaveAsync(Article article)
        {
            try
            {
                await articleRepository.AddAsync(article);

                await unitOfWork.CompleteAsync();

                return(new ArticleResponse(article));
            }
            catch (Exception ex)
            {
                return(new ArticleResponse($"An error occurred when saving the article: { ex.Message }"));
            }
        }
Пример #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <HandleResultDto> Handle(CreateArticleCommand request, CancellationToken cancellationToken)
        {
            var articleCategory = new Article(request.ArticleDto.CategoryId, request.ArticleDto.Title, request.ArticleDto.Remark,
                                              request.ArticleDto.Content, request.ArticleDto.Value);

            articleCategory.SetTags(request.TagIds);

            await _articleRepository.AddAsync(articleCategory, cancellationToken);

            await _articleRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

            return(new HandleResultDto()
            {
                State = 1
            });
        }
            public async Task <Guid> Handle(AddArticleRequest request, CancellationToken cancellationToken)
            {
                //Check if category Exists
                if (!(await categoryRepository.Exists(request.CategoryId, cancellationToken)))
                {
                    throw new CategoryNotFoundException(request.CategoryId);
                }
                //Check if blog Exists
                if (!(await blogRepository.Exists(request.BlogId, cancellationToken)))
                {
                    throw new BlogNotFoundException(request.BlogId);
                }
                //Check if tags Exists
                List <Tag> tags = new List <Tag>();

                foreach (Guid tagId in request.TagsId.Take(5))
                {
                    var tag = await tagRepository.GetByIdAsync(tagId, cancellationToken);

                    if (tag == null)
                    {
                        throw new TagNotFoundException(tagId);
                    }
                    tags.Add(tag);
                }
                //check if MediaObject exists
                if (request.ThumbnailId != null)
                {
                    if (!(await mediaObjectRepository.Exists(request.ThumbnailId, cancellationToken)))
                    {
                        throw new MediaObjectNotFoundException(request.ThumbnailId);
                    }
                }
                Guid Id = repository.NextIdentifier();
                await repository.AddAsync(new Article
                {
                    Title       = request.Title,
                    Content     = request.Content,
                    Excerpt     = request.Excerpt ?? request.Content.Substring(20),
                    BlogId      = request.BlogId,
                    CategoryId  = request.CategoryId,
                    ThumbnailId = request.ThumbnailId,
                    Tags        = tags
                }, cancellationToken);

                return(Id);
            }
Пример #7
0
        public async Task <int> Handle(CreateArticleCommand request, CancellationToken ct)
        {
            Domain.ArticleAggregate.Article article = new Domain.ArticleAggregate.Article(
                request.Title,
                request.PublishDate,
                request.Content,
                new ImageReference(request.ThumbnailLocation, request.ThumbnailAltText),
                request.TagIds,
                false
                );

            await _repository.AddAsync(article);

            await _repository.UnitOfWork.SaveEntitiesAsync(ct);

            return(article.Id);
        }
Пример #8
0
        public async Task <ArticleDto> AddAsync(ArticleCreateDto dto)
        {
            if (string.IsNullOrEmpty(dto.Title))
            {
                throw new ArticleException(ArticleErrorCodes.ArticleTitleCannotBeNull, "Article Title field is mandatory.", dto);
            }

            if (string.IsNullOrEmpty(dto.Body))
            {
                throw new ArticleException(ArticleErrorCodes.ArticleBodyCannotBeNull, "Article Body field is mandatory.", dto);
            }

            if (dto.CategoryId == Guid.Empty)
            {
                throw new ArticleException(ArticleErrorCodes.ArticleCategoryCannotBeNull, "Article Category Id field is mandatory.", dto);
            }
            else
            {
                var categoryEntity = await _categoryRepository.GetByIdAsync(dto.CategoryId);

                if (categoryEntity == null || categoryEntity.Id == Guid.Empty)
                {
                    throw new ArticleException(ArticleErrorCodes.CategoryCouldNotBeFound, "Article Category Id could not be found..", dto);
                }
            }

            var entity = dto.Adapt <Domain.Article>();

            entity = await _articleRepository.AddAsync(entity);

            if (dto.TagIds != null && dto.TagIds.Any())
            {
                if (!await _tagRepository.CheckAllTagIdsExist(dto.TagIds))
                {
                    throw new ArticleException(ArticleErrorCodes.TagCouldNotBeFound, "Article cannot contain non-existed tagId", dto);
                }

                entity = await _articleRepository.AddTags(entity, dto.TagIds);
            }

            return(entity.Adapt <ArticleDto>());
        }
Пример #9
0
        public async Task <CommentDto> AddAsync(CommentCreateDto dto)
        {
            if (string.IsNullOrEmpty(dto.Body))
            {
                throw new ArticleException(ArticleErrorCodes.CommentBodyCannotBeNull, "Comment Body field is mandatory.", dto);
            }

            if (dto.ArticleId == Guid.Empty)
            {
                throw new ArticleException(ArticleErrorCodes.CommentArticleIdConnotBeNull, "Comment Article Id field is mandatory.", dto);
            }

            var articleEntity = _articleRepository.GetByIdAsync(dto.ArticleId);

            if (articleEntity == null)
            {
                throw new ArticleException(ArticleErrorCodes.ArticleCouldNotBeFound, "Article could not be found.", null);
            }
            var entity = dto.Adapt <Domain.Comment>();

            entity = await _articleRepository.AddAsync(entity);

            return(entity.Adapt <CommentDto>());
        }
Пример #10
0
 public async Task CreateAsync(string title, string content, string fullNameAuthor)
 {
     await _articleRepository.AddAsync(new Article(title, content, fullNameAuthor));
 }
Пример #11
0
 public async Task <ArticleEntity> Add(ArticleEntity item)
 {
     return(await _repository.AddAsync(item));
 }