public async Task <ServiceResponse <PostDto> > UpDatePostAsync(int id, UpdatePostDto dto) { ServiceResponse <PostDto> response = new ServiceResponse <PostDto>(); try { var post = await _db.Posts.Include(c => c.User).FirstOrDefaultAsync(x => x.Id == id); if (post.User.Id == GetUserId()) { post.Title = dto.Title; post.Description = dto.Description; _db.Posts.Update(post); await _db.SaveChangesAsync(); response.Data = _mapper.Map <PostDto>(post); } else { response.Success = false; response.Message = "Post not found"; } }catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return(response); }
public async Task <IActionResult> Update([FromForm] UpdatePostDto updatePostDto) { UpdatePostViewModel updatePostViewModel = CustomMapper.GetUpdatePostViewModel(updatePostDto); if (updatePostDto.PhotoFile != null) { var post = await _postService.GetById(updatePostDto.Id); string existingFilePath = post.HeaderPhotoPath; try { _fileService.UpdateLocalFile(updatePostDto.PhotoFile, existingFilePath); } catch (IOException) { } } var result = await _postService.Update(updatePostViewModel); if (result.IsValid) { return(Ok()); } return(BadRequest(result.Errors)); }
public async Task <IActionResult> SetRevisionToPost(string postId, [FromBody] UpdatePostDto postDto) { var pId = Guid.Parse(postId); var newStatus = await BlogService.SetRevisionToPost(pId, postDto.Approved, postDto.Username); return(Ok(newStatus)); }
public void UpdatePost(UpdatePostDto updatePost) { var existingPost = _postRepository.GetById(updatePost.Id); var post = _mapper.Map(updatePost, existingPost); _postRepository.Update(post); }
public void Update(UpdatePostDto postDto) { Post postForUpdate = DbSet.Find(postDto.PostId); postForUpdate.MessageStringContent = postDto.MessageStringContent; postForUpdate.DateUpdate = DateTime.Now; }
public async Task <IActionResult> EditPost(UpdatePostDto dto) { if (ModelState.IsValid) { Post postToUpdate = await _db.Posts.FindAsync(dto.PostId); if (postToUpdate is null) { return(NotFound()); } postToUpdate.Text = dto.Text; postToUpdate.Title = dto.Title; postToUpdate.CategoryId = dto.CategoryId; postToUpdate.DateModified = DateTimeOffset.Now; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewBag.ParentCategories = await _categoryService.GetParentCategoriesInSelectList(); ViewBag.ChildCategories = await _categoryService.GetChildCategoriesByParentCategoryIdInSelectList(dto.ParentCategoryId); return(View(dto)); }
public void Execute(UpdatePostDto request) { var type = Context.Posts.Find(request.Id); if (type == null) { throw new EntityNoFound(); } var id = type.Id; var slike = new Domen.Picture { Id = request.Id, Name = request.Pictures, PostId = id }; type.Pictures.Add(slike); type.Id = request.Id; type.Name = request.Name; type.Text = request.Text; Context.SaveChanges(); }
public async Task UpdatePostAsync(UpdatePostDto updatePost) { var existingPost = await _postRepository.GetByIDAsync(updatePost.Id); var post = _mapper.Map(updatePost, existingPost); await _postRepository.UpdateAsync(post); }
public async Task <ServiceResponse <GetPostDto> > UpdatePost(UpdatePostDto editedPost, int id) { ServiceResponse <GetPostDto> serviceResponse = new ServiceResponse <GetPostDto>(); try { Post postToEdit = await _context.Posts.FirstOrDefaultAsync((p) => p.Id == id); postToEdit.Title = editedPost.Title; postToEdit.Description = editedPost.Description; postToEdit.Date = DateTime.Now; if (editedPost.Image != null) { File.Delete("wwwroot\\" + postToEdit.Image); string filePath = Path.Combine("wwwroot", "images", DateTime.Now.Ticks + Path.GetRandomFileName() + editedPost.Image.FileName); using (var stream = System.IO.File.Create(filePath)) { await editedPost.Image.CopyToAsync(stream); postToEdit.Image = filePath.Substring(8, filePath.Length - 8); } } _context.Posts.Update(postToEdit); await _context.SaveChangesAsync(); serviceResponse.Data = _mapper.Map <GetPostDto>(postToEdit); } catch (System.Exception ex) { serviceResponse.Success = false; serviceResponse.Message = "No se pudo encontrar el post a editar"; } return(serviceResponse); }
public virtual async Task <PostWithDetailsDto> UpdateAsync(Guid id, UpdatePostDto input) { return(await RequestAsync <PostWithDetailsDto>(nameof(UpdateAsync), new ClientProxyRequestTypeValue { { typeof(Guid), id }, { typeof(UpdatePostDto), input } })); }
public async Task <ActionResult> OnPost(Guid id, UpdatePostDto post) { var editedPost = await _postAppService.UpdateAsync(id, post); var blog = await _blogAppService.GetAsync(editedPost.BlogId); return(Redirect(Url.Content($"~/blog/{blog.ShortName}/{editedPost.Title}"))); }
public async Task <PostViewModel> UpdatePostAsync(Post post, UpdatePostDto updatePostDto) { post.Title = updatePostDto.Title; post.Content = updatePostDto.Content; await _postRepository.UpdateAsync(post); return(_mapper.Map <PostViewModel>(post)); }
public async Task <IActionResult> UpdatePost([FromBody] UpdatePostDto postDto) { await _mediator.Send(new UpdatePostCommand { PostDto = postDto }); return(Ok()); }
public async Task <IActionResult> updatePost([FromBody] UpdatePostDto postDto) { if (!ModelState.IsValid) { return(Ok()); } postDto.UserId = Convert.ToInt32(User.FindFirst(ClaimTypes.NameIdentifier).Value); return(Ok(await _post.updatePost(postDto))); }
public static UpdatePostViewModel GetUpdatePostViewModel(UpdatePostDto updatePostDto) { return(new UpdatePostViewModel( updatePostDto.Id, updatePostDto.Title, updatePostDto.Content, updatePostDto.BlogId, updatePostDto.CategoryId, updatePostDto.IsActive)); }
public async Task UpdatePostAsync(UpdatePostDto updatePostDto) { UpdatePostValidate(updatePostDto); var post = await GetPostAsync(updatePostDto.Id); _mapper.Map(updatePostDto, post); post.Date = DateTime.Now; await _dbContext.SaveChangesAsync(); }
public void UpdatePost(UpdatePostDto updatePostDto) { UpdatePostValidate(updatePostDto); var post = GetPost(updatePostDto.Id); _mapper.Map(updatePostDto, post); post.Date = DateTime.Now; _dbContext.SaveChanges(); }
public async Task <IActionResult> Update([FromBody] UpdatePostDto dto, [FromRoute] int id) { ServiceResponse <PostDto> response = await _postService.UpDatePostAsync(id, dto); if (response.Data is null) { return(NotFound()); } return(Ok(response)); }
public async Task <IActionResult> UpdatePost(UpdatePostDto updatePost) { Post post = await _repository.GetPost(updatePost.Id); post.Name = updatePost.Name; post.Content = updatePost.Content; post.ModifiedOn = DateTime.UtcNow; _repository.Update(post); return(Ok(await _repository.SaveAll())); }
public async Task Put(Guid id, [FromBody] UpdatePostDto updatePostDto) { var updatePostViewModel = new UpdatePostViewModel() { InitialPostId = id, Text = updatePostDto.Text, Title = updatePostDto.Title }; await _postService.UpdatePostAsync(updatePostViewModel); }
public async Task <IActionResult> UpdatePost(string id, [FromBody] UpdatePostDto updatePostDto) { var result = await Mediator.Send(new UpdatePostCommand { UpdatePostDto = updatePostDto, Id = id, UserId = AuthorizedUserId }); return(Ok(result)); }
private void UpdatePostValidate(UpdatePostDto updatePostDto) { if (updatePostDto == null) { throw new ArgumentNullException(nameof(updatePostDto)); } if (!updatePostDto.Validate()) { throw new InvalidOperationException(); } }
public async Task <IActionResult> Update(UpdatePostDto updatePost) { var userOwnsPost = await _postService.UserOwnsPostAsync(updatePost.Id, User.FindFirstValue(ClaimTypes.NameIdentifier)); if (!userOwnsPost) { return(BadRequest(new Response(false, "You do not own this post"))); } await _postService.UpdatePostAsync(updatePost); return(NoContent()); }
public async void Posts_PUT_Returns_400() { UpdatePostDto post = new UpdatePostDto() { Text = "New post", Title = string.Empty }; var response = await this.HttpClient.PostAsync($"/api/v1/posts/", new StringContent(JsonConvert.SerializeObject(post), Encoding.UTF8, "application/json")); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); }
public async void Posts_PUT_Returns_404() { UpdatePostDto post = new UpdatePostDto() { Text = "Updated post", Title = "Updated title" }; var response = await this.HttpClient.PutAsync($"/api/v1/posts/{Guid.NewGuid()}", new StringContent(JsonConvert.SerializeObject(post), Encoding.UTF8, "application/json")); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); }
public async Task <IActionResult> UpdateAsync(UpdatePostDto updatePost) { var post = await _postService.GetPostByIdAsync(updatePost.Id); if (post == null) { return(NotFound()); } await _postService.UpdatePostAsync(updatePost); return(NoContent()); }
public async Task <ActionResult> UpdatePost(UpdatePostDto updatePostDto) { try { await _postService.UpdatePostAsync(updatePostDto); } catch { BadRequest(); } return(NoContent()); }
public async Task<ActionResult> OnPost() { var post = new UpdatePostDto { BlogId = Post.BlogId, Title = Post.Title, Url = Post.Url, Content = Post.Content }; var editedPost = await _postAppService.UpdateAsync(Post.Id, post); var blog = await _blogAppService.GetAsync(editedPost.BlogId); return Redirect(Url.Content($"~/blog/{blog.ShortName}/{editedPost.Url}")); }
public async Task <IActionResult> UpdateCurrentPost(int id, UpdatePostDto updatepost) { if (ModelState.IsValid) { var postFromService = await _blogService.GetPostById(id); _mapper.Map(updatepost, postFromService); if (await _uow.SaveChangesAsync() > 0) { return(NoContent()); } throw new System.Exception($"Updating post {id} failed on save"); } return(BadRequest()); }
public async Task <IActionResult> PutAsync(int id, [FromBody] UpdatePostDto updatePostDto) { var post = await _postService.GetAsync(id); if (post == null) { return(NotFound()); } var updatePost = _mapper.Map <Post>(updatePostDto); await _postService.UpdatePost(id, updatePost); return(NoContent()); }