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)); }
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); }
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)); }
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 }")); } }
/// <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); }
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); }
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>()); }
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>()); }
public async Task CreateAsync(string title, string content, string fullNameAuthor) { await _articleRepository.AddAsync(new Article(title, content, fullNameAuthor)); }
public async Task <ArticleEntity> Add(ArticleEntity item) { return(await _repository.AddAsync(item)); }