public async Task UpdatePostAsync(UpdatePostDTO dto) { var post = await FindPostAsync(dto.Post.Id); var updatedPost = _mapper.Map <Post>(dto.Post); //Update the values for the post _mapper.Map(updatedPost, post); //Update the users set to the given post. foreach (var username in dto.UserNames) { var updateUserPostsDto = new UpdateUserPostsDTO { UserName = username, PostIds = new int[] { post.Id } }; await SetUserPostsAsync(updateUserPostsDto); } //Update the roles given to the post. var modDto = new ModifyPostRolesDTO() { Id = dto.Post.Id, Roles = dto.Roles }; await SetPostRolesAsync(modDto); if (!await _postRepository.SaveAll()) { throw new ApplicationException("Could not update the given post."); } }
public async Task <IActionResult> Post(UpdatePostModel model) { var post = await PostSvc.GetByIdAsync(model.Id); if (post == null) { return(new JsonResult(new APIResult <long> { ErrorMsg = "帖子不存在" }) { StatusCode = 400 }); } var statu = await StatuSvc.GetByIdAsync(model.PostStatusId); if (statu == null) { return(new JsonResult(new APIResult <long> { ErrorMsg = "帖子状态不存在" }) { StatusCode = 400 }); } UpdatePostDTO dto = new UpdatePostDTO(); dto.Id = model.Id; dto.PostStatusId = model.PostStatusId; dto.IsEssence = model.IsEssence; await PostSvc.UpdateAsync(dto); return(Ok()); }
public async Task UpdateAsync(UpdatePostDTO dto) { using (PostContext ctx = new PostContext()) { BaseService <PostEntity> bs = new BaseService <PostEntity>(ctx); var post = await bs.GetAll().SingleAsync(e => e.Id == dto.Id); post.PostStatusId = dto.PostStatusId; post.IsEssence = dto.IsEssence; await ctx.SaveChangesAsync(); } }
public async Task <IActionResult> UpdatePost(UpdatePostDTO updatePostDTO, int postId) { var userId = GetUserId(); // Does User exist? if (await UserExists(userId) != true) { return(Unauthorized()); } var postToUpdate = await _Repo.Posts.FirstOrDefaultAsync(x => x.Id == postId); // Does Post exist? if (postToUpdate == null) { return(BadRequest("Post does not exist")); } // Is User an Admin or the Post's Owner? if (IsAdmin() == false || postToUpdate.AuthorId != int.Parse(userId)) { return(Unauthorized()); } if (postToUpdate.Content == JsonSerializer.Serialize(updatePostDTO.Content)) { return(await GetPost(postToUpdate.Id)); } postToUpdate.Content = JsonSerializer.Serialize(updatePostDTO.Content); postToUpdate.Edited = true; postToUpdate.LastEditDate = DateTime.Now; _Repo.Update(postToUpdate); await _Repo.SaveChangesAsync(); return(await GetPost(postToUpdate.Id)); }
public async Task <bool> Update([FromBody] UpdatePostDTO req) { UpdatePostCommand command = new UpdatePostCommand(req); return(await _bus.SendCommandAsync(command)); }
public async Task <IActionResult> UpdatePost(int id, UpdatePostDTO dto) { await _postService.UpdatePostAsync(dto); return(NoContent()); }
public UpdatePostCommand(UpdatePostDTO post) { Post = post; }