public async Task <EditArticleResponse> UpdateArticleAsync(ArticleEditDto article) { var response = new EditArticleResponse(); try { var dbArticle = await this.articleRepository.GetByIdAsync(article.ArticleId); if (dbArticle != null) { var now = DateTimeOffset.Now; dbArticle.Updated = now; var dbArticleVersion = dbArticle.ArticleVersions.FirstOrDefault(x => x.Id == article.ArticleVersionId); if (dbArticleVersion != null) { dbArticleVersion.Title = article.Title; dbArticleVersion.Content = article.Content; dbArticleVersion.IsPublished = article.Publish; dbArticleVersion.Updated = now; } else { dbArticle.ArticleVersions.Add(new ArticleVersion { LanguageId = article.LanguageId, Title = article.Title, Content = article.Content, IsPublished = article.Publish, Created = now, Updated = now }); } var update = this.articleRepository.Update(dbArticle); await this.unitOfWork.CommitAsync(); var updatedVersion = update.ArticleVersions.First(x => x.Id == article.ArticleVersionId); response.Article = new ArticleEditDto { Title = updatedVersion.Title }; response.Status = StatusCode.OK; } else { response.Status = StatusCode.NotFound; } } catch (Exception ex) { this.exceptionHandler.HandleException(ex); response.Status = StatusCode.InternalServerError; } return(response); }
public async Task <IActionResult> Update(int id, ArticleEditDto articleDto) { articleDto.EventDate = articleDto.EventDate.Date; if (id != articleDto.Id) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest()); } articleDto.EventDate = articleDto.EventDate.ToLocalTime(); var photoURL = articleDto.PhotoURL; var article = _mapper.Map <Article>(articleDto); article.PublishingDate = DateTime.Now; var articleResult = await _articleService.Update(article, photoURL); if (articleResult == null) { return(BadRequest()); } return(Ok(_mapper.Map <ArticleResultDto>(articleResult))); }
public async Task <IActionResult> Edit(ArticleEditDto article, IFormFile pic) { var updatedArticle = await _articleService.GetByIdAsync(article.Id); if (pic != null) { var picName = Guid.NewGuid() + Path.GetExtension(pic.FileName); var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img/article-cover/" + picName); using var stream = new FileStream(path, FileMode.Create); await pic.CopyToAsync(stream); updatedArticle.Data.ImagePath = picName; } if (ModelState.IsValid) { updatedArticle.Data.Title = article.Title; updatedArticle.Data.Content = article.Content; updatedArticle.Data.CategoryId = article.CategoryId; await _articleService.UpdateAsync(updatedArticle.Data); return(RedirectToAction("Index", "Article", new { area = "Admin" })); } return(View(article)); }
public async Task CreateArticleAsync() { // arrange var unitOfWork = this.CreateMockUnitOfWork(); var articleService = new ArticleService(unitOfWork, new NullExceptionHandler()); var request = new ArticleEditDto { LanguageId = 2, Title = "Title", Content = "Content", Publish = true }; // act var result = await this.articleService.CreateArticleAsync(request); // assert Assert.IsNotNull(result); Assert.AreEqual(StatusCode.OK, result.Status); var article = result.Article; Assert.AreEqual(4, article.ArticleId); Assert.AreEqual(8, article.ArticleVersionId); Assert.AreEqual("Title", article.Title); Assert.AreEqual("Content", article.Content); }
/// <summary> /// 新增Article /// </summary> //[AbpAuthorize(ArticleAppPermissions.Article_CreateArticle)] protected virtual async Task <ArticleEditDto> CreateArticleAsync(ArticleEditDto input) { //TODO:新增前的逻辑判断,是否允许新增 var entity = ObjectMapper.Map <Article>(input); entity = await _articleRepository.InsertAsync(entity); return(entity.MapTo <ArticleEditDto>()); }
public static IEnumerable <ArticleAssociationPair> CreateArticleAssociationInstructions( ArticleEditDto articleEditDto, IEnumerable <ClubArticle> clubArticles, IEnumerable <SquadArticle> squadArticles) { var pairs = new List <ArticleAssociationPair>(); // Iterate through the associations passed in Dto. // Match them as pair against existing squad and club articles. foreach (var association in articleEditDto.Associations) { var pair = new ArticleAssociationPair { Dto = association }; pairs.Add(pair); if (association.IsClub && clubArticles.Any(ca => ca.ClubId == association.AssociatedToId)) { // Should only be a one to one relationship between an article and a club. pair.Club = clubArticles.First(ca => ca.ClubId == association.AssociatedToId); } else if (!association.IsClub && squadArticles.Any(sa => sa.SquadId == association.AssociatedToId)) { // Should only be a one to one relationship between an article and a squad. pair.Squad = squadArticles.First(sa => sa.SquadId == association.AssociatedToId); } } // If there is no match from existing records we should create. // If we do match and the dto indicates it is not selected then remove existing. foreach (var pair in pairs) { if (pair.IsAssociated() && !pair.Dto.IsSelected) { pair.Action = ActionTypes.Delete; } else if (!pair.IsAssociated() && pair.Dto.IsSelected) { pair.Action = ActionTypes.Create; if (pair.Dto.IsClub) { pair.Club = new ClubArticle { ClubId = pair.Dto.AssociatedToId, ArticleId = articleEditDto.Id }; } else { pair.Squad = new SquadArticle { SquadId = pair.Dto.AssociatedToId, ArticleId = articleEditDto.Id }; } } } // If there any existing not matched to dtos then then should be removed. // TODO: not to worried about this we 'might' end up with trailing records. meh! return(pairs); }
/// <summary> /// 编辑Article /// </summary> //[AbpAuthorize(ArticleAppPermissions.Article_EditArticle)] protected virtual async Task UpdateArticleAsync(ArticleEditDto input) { //TODO: 更新前的逻辑判断,是否允许更新 var entity = await _articleRepository.GetAsync(input.Id.Value); input.MapTo(entity); // ObjectMapper.Map(input, entity); await _articleRepository.UpdateAsync(entity); }
public async Task <IActionResult> EditArticle(ArticleEditDto model, IFormFile pic) { if (ModelState.IsValid) { await _articleService.UpdateAsync(_mapper.Map <Article>(model), pic); return(RedirectToAction("Index", "Article", new { area = "Admin" })); } return(View(model)); }
/// <summary> /// 新增文章 /// </summary> //[AbpAuthorize(ArticleAppPermissions.Article_CreateArticle)] async Task <ArticleEditDto> CreateArticleAsync(ArticleEditDto input) { //TODO:新增前的逻辑判断,是否允许新增 var entity = input.MapTo <Article>(); entity = await _articleRepository.InsertAsync(entity); return(entity.MapTo <ArticleEditDto>()); }
/// <summary> /// 新增或更改文章 /// </summary> public async Task CreateOrUpdateArticleAsync(ArticleEditDto input) { if (input.Id.HasValue) { await UpdateArticleAsync(input); } else { await CreateArticleAsync(input); } }
/// <summary> /// 添加或者修改Article的方法 /// </summary> /// <param name="input"></param> /// <returns></returns> public async Task <ArticleEditDto> CreateOrUpdateArticleDto(ArticleEditDto input) { if (input.Id.HasValue) { return(await UpdateArticleAsync(input)); } else { return(await CreateArticleAsync(input)); } }
public IActionResult EditArticle([FromRoute] Guid articleId, [FromBody] ArticleEditDto articleEditDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _articleLogic.EditArticle(articleEditDto, articleId); return(NoContent()); }
/// <summary> /// 编辑Article /// </summary> //[AbpAuthorize(ArticleAppPermissions.Article_EditArticle)] protected virtual async Task <ArticleEditDto> UpdateArticleAsync(ArticleEditDto input) { //TODO:更新前的逻辑判断,是否允许更新 var entity = await _articleRepository.GetAsync(input.Id.Value); entity.LinkAddress = input.LinkAddress; input.MapTo(entity); // ObjectMapper.Map(input, entity); var result = await _articleRepository.UpdateAsync(entity); return(result.MapTo <ArticleEditDto>()); }
public async Task <EditArticleResponse> CreateArticleAsync(ArticleEditDto article) { var response = new EditArticleResponse(); try { var now = DateTimeOffset.Now; var newArticle = new Article { Created = now, Updated = now }; if (article.Publish) { newArticle.Published = now; } newArticle.ArticleVersions.Add(new ArticleVersion { LanguageId = article.LanguageId, Title = article.Title, Content = article.Content, IsPublished = article.Publish, Created = now, Updated = now }); var add = this.articleRepository.Insert(newArticle); await this.unitOfWork.CommitAsync(); var addVersion = add.ArticleVersions.First(); response.Article = new ArticleEditDto { ArticleId = add.Id, ArticleVersionId = addVersion.ArticleId, Title = addVersion.Title, Content = addVersion.Content }; response.Status = StatusCode.OK; } catch (Exception ex) { this.exceptionHandler.HandleException(ex); response.Status = StatusCode.InternalServerError; } return(response); }
/// <summary> /// ブログ記事を更新する。 /// </summary> /// <param name="userId">ユーザーID。</param> /// <param name="articleId">ブログ記事ID。</param> /// <param name="param">ブログ記事情報。</param> /// <returns>処理結果。</returns> /// <exception cref="NotFoundException">ブログ記事が存在しない場合。</exception> /// <exception cref="ForbiddenException">ユーザーのブログでない場合。</exception> public async Task UpdateArticle(int userId, int articleId, ArticleEditDto param) { var article = await this.articleRepository.FindOrFail(articleId); var blog = await this.blogRepository.FindOrFail(article.BlogId); if (blog.UserId != userId) { throw new ForbiddenException($"id={articleId} does not belong to me"); } this.mapper.Map(param, article); await this.articleRepository.Update(article); }
/// <summary> /// 通过Id获取文基本信息 (过时) /// </summary> public async Task <ArticleEditDto> GetArticleForEditAsync(NullableIdDto <long> input) { ArticleEditDto articleEditDto; if (input.Id.HasValue) { var entity = await _articleRepository.GetAsync(input.Id.Value); return(articleEditDto = entity.MapTo <ArticleEditDto>()); } else { return(articleEditDto = new ArticleEditDto()); } }
private async Task AddAssociationsToArticleEditDto(ArticleEditDto articleDto) { var squads = await _squadsRepository.GetAll(); var squadAssociations = FromModelToDto(squads); var club = await _clubsRepository.GetFirst(); var clubAssociations = FromModelToDTOs(club); // Need to call the ToArray to resolve all deferred executions. // This is needed because we are modifiying the IsSelected and // without resolution the objects in collections would refresh. articleDto.Associations = Enumerable.Concat(clubAssociations, squadAssociations) .ToArray(); }
private async Task UpdateArticle(ArticleEditDto articleDto) { var article = FromDtoToModel(articleDto); // Get the associations that already exist for this article. // This would include both squads and the club association. var clubArticles = await _articlesRepository.GetClubAssociationsForArticleId(articleDto.Id); var squadArticles = await _articlesRepository.GetSquadAssociationsForArticleId(articleDto.Id); var instructions = CreateArticleAssociationInstructions(articleDto, clubArticles, squadArticles); var articlesTo = ExtractArticleCollectionsFor(instructions); await _articlesRepository.UpdateWithAssociations( article, articlesTo.CreateForClub, articlesTo.DeleteForClub, articlesTo.CreateForSquad, articlesTo.DeleteForSquad); }
public async Task <IActionResult> Update(int id, ArticleEditDto model) { model.SetId(id); var result = await _repo.UpdateArticle(model); if (result == null) { return(StatusCode(StatusCodes.Status500InternalServerError, new Response <ArticleEditDto>() { Succeeded = false, Message = "ثبت مقاله با مشکل مواجه شد لطفا ورودی های خود را چک کرده و مجددا تلاش کنید" })); } return(Ok(new Response <ArticleEditDto>(result) { Message = "مقاله با موفقیت بروزرسانی شد" })); }
public async void TestPutArticle() { var now = DateTimeOffset.UtcNow; var article = new Article() { Subject = "Article for PutArticle", Body = "PutArticle Body", BlogId = 1000, Tags = new List <Tag>() { new Tag() { Name = "Blog" }, new Tag() { Name = "変更前" } } }; var db = this.factory.CreateDbContext(); db.Articles.Add(article); db.SaveChanges(); var body = new ArticleEditDto() { Subject = "Updated Article", Body = "Updated Article Body", Tags = new string[] { "変更後" } }; var response = await this.authedClient.PutAsJsonAsync($"/api/articles/{article.Id}", body); var responseString = await response.Content.ReadAsStringAsync(); Assert.True(response.IsSuccessStatusCode, responseString); var dbarticle = this.factory.CreateDbContext().Articles.Include(a => a.Tags).First(a => a.Id == article.Id); Assert.NotNull(dbarticle); Assert.Equal(body.Subject, dbarticle.Subject); Assert.Equal(body.Body, dbarticle.Body); Assert.True(dbarticle.UpdatedAt > now); Assert.NotEmpty(dbarticle.Tags.Where(n => n.Name == body.Tags.First())); foreach (var o in article.Tags) { Assert.Empty(dbarticle.Tags.Where(n => n.Name == o.Name)); } }
/// <summary> /// 导出Article为excel表 /// </summary> /// <returns></returns> //public async Task<FileDto> GetArticlesToExcel(){ //var users = await UserManager.Users.ToListAsync(); //var userListDtos = ObjectMapper.Map<List<UserListDto>>(users); //await FillRoleNames(userListDtos); //return _userListExcelExporter.ExportToFile(userListDtos); //} /// <summary> /// MPA版本才会用到的方法 /// </summary> /// <param name="input"></param> /// <returns></returns> public async Task <GetArticleForEditOutput> GetArticleForEdit(NullableIdDto <int> input) { var output = new GetArticleForEditOutput(); ArticleEditDto articleEditDto; if (input.Id.HasValue) { var entity = await _articleRepository.GetAsync(input.Id.Value); articleEditDto = entity.MapTo <ArticleEditDto>(); //articleEditDto = ObjectMapper.Map<List <articleEditDto>>(entity); } else { articleEditDto = new ArticleEditDto(); } output.Article = articleEditDto; return(output); }
public async Task <ArticleEditDto> UpdateArticle(ArticleEditDto model) { var article = await _context.Articles.Include(p => p.User).Include(p => p.ArticleTags).Include(a => a.ArticleHeadLines).FirstOrDefaultAsync(p => p.Id == model.Id); if (article == null) { return(null); } article.Title = model.Title; article.Description = model.Description; article.ArticleCategoryId = model.ArticleCategoryId; await Update(article); _context.ArticleTags.RemoveRange(article.ArticleTags); _context.ArticleTags.AddRange(_mapper.Map <List <ArticleTag> >(model.ArticleTags)); _context.ArticleHeadLines.RemoveRange(article.ArticleHeadLines); _context.ArticleHeadLines.AddRange(_mapper.Map <List <ArticleHeadLine> >(model.ArticleHeadLines)); await _context.SaveChangesAsync(); return(model); }
public async Task <IActionResult> Put([FromBody] ArticleEditDto articleDto) => await this.TakeAction(UpdateArticle(articleDto));
private async Task CreateArticle(ArticleEditDto articleDto) { var article = FromDtoToModel(articleDto); await _articlesRepository.Create(article); }
public async Task<EditArticleResponse> UpdateArticleAsync(ArticleEditDto article) { var response = new EditArticleResponse(); try { var dbArticle = await this.articleRepository.GetByIdAsync(article.ArticleId); if (dbArticle != null) { var now = DateTimeOffset.Now; dbArticle.Updated = now; var dbArticleVersion = dbArticle.ArticleVersions.FirstOrDefault(x => x.Id == article.ArticleVersionId); if (dbArticleVersion != null) { dbArticleVersion.Title = article.Title; dbArticleVersion.Content = article.Content; dbArticleVersion.IsPublished = article.Publish; dbArticleVersion.Updated = now; } else { dbArticle.ArticleVersions.Add(new ArticleVersion { LanguageId = article.LanguageId, Title = article.Title, Content = article.Content, IsPublished = article.Publish, Created = now, Updated = now }); } var update = this.articleRepository.Update(dbArticle); await this.unitOfWork.CommitAsync(); var updatedVersion = update.ArticleVersions.First(x => x.Id == article.ArticleVersionId); response.Article = new ArticleEditDto { Title = updatedVersion.Title }; response.Status = StatusCode.OK; } else { response.Status = StatusCode.NotFound; } } catch (Exception ex) { this.exceptionHandler.HandleException(ex); response.Status = StatusCode.InternalServerError; } return response; }
public async Task<EditArticleResponse> CreateArticleAsync(ArticleEditDto article) { var response = new EditArticleResponse(); try { var now = DateTimeOffset.Now; var newArticle = new Article { Created = now, Updated = now }; if (article.Publish) { newArticle.Published = now; } newArticle.ArticleVersions.Add(new ArticleVersion { LanguageId = article.LanguageId, Title = article.Title, Content = article.Content, IsPublished = article.Publish, Created = now, Updated = now }); var add = this.articleRepository.Insert(newArticle); await this.unitOfWork.CommitAsync(); var addVersion = add.ArticleVersions.First(); response.Article = new ArticleEditDto { ArticleId = add.Id, ArticleVersionId = addVersion.ArticleId, Title = addVersion.Title, Content = addVersion.Content }; response.Status = StatusCode.OK; } catch (Exception ex) { this.exceptionHandler.HandleException(ex); response.Status = StatusCode.InternalServerError; } return response; }
public async Task <IActionResult> PutArticle(int id, ArticleEditDto body) { await this.articleService.UpdateArticle(this.UserId, id, body); return(this.NoContent()); }