public async Task <ServiceResponse <int> > CreateAsync(ArticleDto article, int userId) { if (article == null) { throw new ArgumentNullException(nameof(article)); } var articlesWithSameBarcode = UnitOfWork.Get <Article>() .GetAll(a => a.Barcode != null && a.Barcode.Equals(article.Barcode) && a.ID != article.ID); if (articlesWithSameBarcode.Any()) { return(new ServiceResponse <int>(ServiceResponseStatus.AlreadyExists)); } var entity = article.ToEntity(); entity.UpdateCreatedFields(userId).UpdateModifiedFields(userId); entity.Recommendations.UpdateCreatedFields(userId).UpdateModifiedFields(userId); entity.Attachments.UpdateCreatedFields(userId).UpdateModifiedFields(userId); var added = UnitOfWork.Get <Article>().Add(entity); await UnitOfWork.SaveAsync(); return(new SuccessResponse <int>(added.ID)); }
public async Task <ServiceResponse <ArticleDto> > UpdateAsync(ArticleDto article, int userId) { if (article == null) { throw new ArgumentNullException(nameof(article)); } var existentEntity = await UnitOfWork.Get <Article>().GetAsync(t => t.ID == article.ID); if (existentEntity == null) { return(new ServiceResponse <ArticleDto>(ServiceResponseStatus.NotFound)); } var articlesWithSameBarcode = UnitOfWork.Get <Article>() .GetAll(a => a.Barcode != null && a.Barcode.Equals(article.Barcode) && a.ID != article.ID); if (articlesWithSameBarcode.Any()) { return(new ServiceResponse <ArticleDto>(ServiceResponseStatus.AlreadyExists)); } var entity = article.ToEntity(); existentEntity.Attachments = GetAttachments(existentEntity.ID).ToList(); existentEntity.Recommendations = GetRecommendations(existentEntity.ID).ToList(); existentEntity .UpdateFields(entity) .UpdateAttachments(entity, UnitOfWork, userId) .UpdateRecommendations(entity, UnitOfWork, userId) .UpdateModifiedFields(userId); UnitOfWork.Get <Article>().Update(existentEntity); await UnitOfWork.SaveAsync(); return(new SuccessResponse <ArticleDto>()); }
public async Task <ArticleDto> Create(ArticleDto article) { if (await _db.Articles.AnyAsync(x => x.Title == article.Title)) { throw new ApiException("Article with this title already exists"); } //article.Content = _htmlImagesService.ParseHtmlImages(article.Content); article.DatePublished = DateTime.UtcNow; var entity = article.ToEntity <Article, ArticleDto>(); entity.Content = string.Empty; // don't save without formatting content await _db.Articles.AddAsync(entity); await _db.SaveChangesAsync(); await UpdateArticleContent(entity, article.Content); return(entity.ToEntityDto <ArticleDto, Article>()); }