Ejemplo n.º 1
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>());
        }