public ActionResult <CommentOutDto> Update(string id, [FromBody] CommentUpdateDto commentInDto) { var comment = _commentRepository.GetById(id); //Check if this comment exists if (comment == null) { return(BadRequest(new Message("The comment with id: " + id + " doesn't exist."))); } var tokenUser = HttpContext.User; //If request is not sent by owner, finish here if (!AuthorizationHelpers.IsAuthorizedUser(tokenUser, comment.OwnerId)) { return(Unauthorized(new Message("Unauthorized user."))); } if (!string.IsNullOrEmpty(commentInDto.Content)) { comment.Content = commentInDto.Content; } //Update table if (_commentRepository.Update(comment)) { var commentOutDto = _mapper.Map <CommentOutDto>(comment); return(Ok(commentOutDto)); } return(BadRequest(new Message("Error when updating comment with id: " + id))); }
public async Task <CommentDto> UpdateAsync(CommentUpdateDto dto) { if (dto.Id == Guid.Empty) { throw new ArticleException(ArticleErrorCodes.CommentIdCannotBeNull, "Comment Id field is mandatory.", null); } var entity = await _articleRepository.GetByIdAsync(dto.Id); if (entity == null) { throw new ArticleException(ArticleErrorCodes.CommentCouldNotBeFound, "Comment could not be found.", dto); } if (string.IsNullOrEmpty(dto.Body)) { throw new ArticleException(ArticleErrorCodes.CommentBodyCannotBeNull, "Comment Body field is mandatory.", dto); } entity.Body = dto.Body; entity = await _articleRepository.UpdateAsync(entity); return(entity.Adapt <CommentDto>()); }
public async Task <IActionResult> Update(CommentUpdateDto commentUpdateDto) { if (ModelState.IsValid) { var result = await _commentService.UpdateAsync(commentUpdateDto, LoggedInUser.UserName); if (result.ResultStatus == ResultStatus.Success) { var commentUpdateAjaxModel = JsonSerializer.Serialize(new CommentUpdateAjaxViewModel { CommentDto = result.Data, CommentUpdatePartial = await this.RenderViewToStringAsync("_CommentUpdatePartial", commentUpdateDto) }, new JsonSerializerOptions { ReferenceHandler = ReferenceHandler.Preserve }); return(Json(commentUpdateAjaxModel)); } } var commentUpdateAjaxErrorModel = JsonSerializer.Serialize(new CommentUpdateAjaxViewModel { CommentUpdatePartial = await this.RenderViewToStringAsync("_CommentUpdatePartial", commentUpdateDto) }); return(Json(commentUpdateAjaxErrorModel)); }
public void CanUpdateComment() { _context.Users.Add(_testUser); _context.Products.Add(_testProduct); var comment = new Comment { Id = 3, Message = "Test comment", CreatedAt = DateTime.Now, Product = _testProduct, User = _testUser }; _context.Comments.Add(comment); _context.SaveChanges(); var updatedComment = new CommentUpdateDto { CommentId = comment.Id, Message = "An updated message" }; _commentService.UpdateComment(updatedComment); _commentService.Save(); var product = _productRepository.GetProduct(_testProduct.Id); var theComment = product.Comments.FirstOrDefault(x => x.Id == 3); Assert.AreEqual(theComment.Message, updatedComment.Message); }
public async Task UpdateComment(CommentUpdateDto commentUpdate) { var comment = await _repository.GetById(commentUpdate.Id); comment.Content = commentUpdate.NewContent; comment.LastEditDate = commentUpdate.LastEditDate; await _repository.Update(comment); }
public IActionResult Update(CommentUpdateDto comment) { var result = _commentservice.Update(comment); if (result.Success) { return(Ok(result.Message)); } return(BadRequest(result.Message)); }
public async Task <IActionResult> Update(CommentUpdateDto commentUpdateDto) { var result = await _commentService.Update(commentUpdateDto); if (result.Success) { return(Ok(result.Message)); } return(BadRequest(result.Message)); }
public async Task <IActionResult> UpdateComment([FromRoute] int id, [FromQuery] CommentUpdateDto commentUpdateDto) { if (id != commentUpdateDto.Id) { return(BadRequest()); } await _commentService.UpdateAsync(_mapper.Map <Comment>(commentUpdateDto)); return(NoContent()); }
public IResult Update(CommentUpdateDto commentUpdateDto, string modifiedByName) { var oldComment = _unitOfWork.Comment.Get(c => c.Id == commentUpdateDto.Id); var comment = _mapper.Map <CommentUpdateDto, Comment>(commentUpdateDto, oldComment); comment.ModifiedByName = modifiedByName; var updatedComment = _unitOfWork.Comment.Update(comment); _unitOfWork.Save(); return(new Result(ResultStatus.Success, $"{updatedComment.Text} adlı yorum başarı ile güncellenmiştir")); }
public bool UpdateCommentById(int commentId, CommentUpdateDto commentDto) { var comment = _context.Comments.Find(commentId); comment.Id = commentDto.Id; comment.OwnerId = commentDto.OwnerId; comment.Content = commentDto.Content; _context.Comments.Update(comment); return(_context.SaveChanges() == 1); }
public void UpdateComment(CommentUpdateDto comment) { var commentToUpdate = _context.Comments.FirstOrDefault(x => x.Id == comment.CommentId); if (commentToUpdate == null) { throw new AppException("Comment not found"); } commentToUpdate.Message = comment.Message; Save(); }
public ActionResult <CommentReadDto> Update(CommentUpdateDto commentUpdateDto) { Comment comment = _commentRepository.Get(commentUpdateDto.Id); if (comment == null) { return(BadRequest("Comment with that id doesn't exist.")); } comment = _mapper.Map(commentUpdateDto, comment); _commentRepository.Update(comment); _logger.Log("Update comment"); return(Ok(comment)); }
public void Update2(CommentUpdateDto comment) { using (var context = new BlogDbContext()) { var entity = context.Comments.Where(w => w.Id == comment.Id).SingleOrDefault(); if (entity != null) { entity.Content = comment.Content; //entity.Updated = Convert.ToDateTime(comment.Updated); entity.Updated = DateTime.Now; context.SaveChanges(); } } }
public async Task CommentUpdate(CommentUpdateDto commentUpdateDto) { using (var context = new ProductInformationContext()) { var entity = await context.Comments.Where(w => w.Id == commentUpdateDto.Id).SingleOrDefaultAsync(); if (entity != null) { entity.Content = commentUpdateDto.Content; //entity.Updated = Convert.ToDateTime(comment.Updated); entity.Updated = DateTime.Now; await context.SaveChangesAsync(); } } }
public ActionResult <CommentReadDto> UpdateComment(int id, CommentUpdateDto dto) { var oldCom = _repository.Find(id); if (oldCom == null) { return(NotFound()); } _mapper.Map(dto, oldCom); _repository.Save(); return(NoContent());// Ok(oldAcc); }
public async Task <IDataResult <CommentDto> > Update(CommentUpdateDto commentUpdateDto, string modifiedByName) { var comment = _mapper.Map <Comment>(commentUpdateDto); comment.ModifiedByName = modifiedByName; var updatedComment = await _unitOfWork.Comment.UpdateAsync(comment); await _unitOfWork.SaveAsync(); return(new DataResult <CommentDto>(ResultStatus.Success, new CommentDto { Comment = updatedComment, ResultStatus = ResultStatus.Success, })); }
public async Task <IDataResult <CommentDto> > UpdateComment(CommentUpdateDto commentUpdateDto) { var comment = _mapper.Map <SerieComment>(commentUpdateDto); var updatedComment = await _unitOfWork.SerieComments.UpdateAsync(comment); updatedComment.SerieId = comment.Id; await _unitOfWork.SaveAsync(); return(new DataResult <CommentDto>(ResultStatus.Success, $"{commentUpdateDto.Title} başlıklı yorum başarıyla güncellenmiştir.", new CommentDto { SerieComment = updatedComment, ResultStatus = ResultStatus.Success, Message = $"{commentUpdateDto.Title} başlıklı yorum başarıyla güncellenmiştir." })); }
public async Task <string> Update(CommentUpdateDto input) { var Item = repository.GetSingel(input.Id); if (Item == null) { return("null"); } Item.Id = input.Id; Item.comment = input.comment; Item.Rate = input.Rate; Item.RestaurantId = input.RestaurantId; await repository.Save(); return($"This {input.Id} Was Updated "); }
public async Task <IDataResult <CommentDto> > UpdateAsync(CommentUpdateDto commentUpdateDto, string modifiedByName) { var oldComment = await UnitOfWork.Comments.GetAsync(c => c.Id == commentUpdateDto.Id); var comment = Mapper.Map <CommentUpdateDto, Comment>(commentUpdateDto, oldComment); comment.ModifiedByName = modifiedByName; var updatedComment = await UnitOfWork.Comments.UpdateAsync(comment); await UnitOfWork.SaveAsync(); return(new DataResult <CommentDto>(ResultStatus.Success, Messages.Comment.Update(comment.CreatedByName), new CommentDto { Comment = updatedComment, })); }
public async Task <ActionResult> UpdateComment(int id, CommentUpdateDto comment) { var commentModelFromRepo = await _repository.GetCommentByIdAsync(id); if (commentModelFromRepo == null) { return(NotFound()); } _mapper.Map(comment, commentModelFromRepo); _repository.UpdateComment(commentModelFromRepo); await _repository.SaveChangesAsync(); return(NoContent()); }
public ActionResult Update(int commentId, CommentUpdateDto commentDto) { var comment = _service.GetCommentById(commentId); if (comment.OwnerId != int.Parse(User.Identity.Name)) { return(Unauthorized()); } if (_service.UpdateCommentById(commentId, commentDto)) { return(Ok()); } return(NotFound()); }
public async Task <IActionResult> Update([FromRoute] string id, [FromBody] CommentUpdateDto dto) { var userId = User.GetUserId(); var comment = await _repo.GetComment(id); if (comment.CreatedById != userId) { return(Unauthorized()); } comment.Text = dto.Text; if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest()); }
public async Task <IActionResult> Update(int id, [FromForm] CommentUpdateDto commentUpdateDto) { if (id != commentUpdateDto.Id) { return(BadRequest("Invalid Id")); } var updatedComment = await _commentService.FindByIdAsync(id); updatedComment.Id = commentUpdateDto.Id; updatedComment.Description = commentUpdateDto.Description; updatedComment.AuthorEmail = commentUpdateDto.AuthorEmail; updatedComment.AuthorName = commentUpdateDto.AuthorName; updatedComment.IsApproved = commentUpdateDto.IsApproved; await _commentService.UpdateAsync(updatedComment); return(NoContent()); }
public async Task<IActionResult> UpdateCommentForPost([FromRoute] Guid postId, [FromRoute] Guid id, [FromBody] CommentUpdateDto commentToUpdate) { //model state validation is not required due to the [ApiController] attribute automatically returning UnprocessableEntity (see startup.cs) //when model binding fails if (!await _postRepository.PostExistsAsync(postId)) { return NotFound(); } var commentFromRepo = await _commentRepository.GetCommentAsync(postId, id); if (commentFromRepo == null) { return NotFound(); } //fetch the user id from the JWT via HttpContext. Then get the user from the repository. This is to ensure that an authorized user //is calling the API with a valid user id var user = await _userRepository.GetUserAsync(User.GetUserId()); //check if the user is an administrator or owner of the object var isAdmin = User?.IsAdministrator() ?? false; var userIsAdminOrOwner = isAdmin ? isAdmin : (user?.Id != commentFromRepo.UserId ? false : true); if (!userIsAdminOrOwner) { //returning status code instead of Forbid() as forbid only works with authentication handlers return StatusCode(403, new { Error = "You cannot modify other users submissions unless you are an administrator." }); } _mapper.Map(commentToUpdate, commentFromRepo); //map fields from CommentUpdateDto to Comment that was fetched from repository _commentRepository.UpdateComment(commentFromRepo); //Call to empty method. This is just for information and is not required if (!await _commentRepository.SaveChangesAsync()) { throw new Exception($"Error updating Comment {commentFromRepo.Id} to the database"); } return NoContent(); }
public IActionResult Put([FromBody] CommentUpdateDto comment) { var commentInDb = _commentService.GetComment(comment.CommentId); if (commentInDb == null) { return(NotFound()); } if (commentInDb.User.Id != int.Parse(User.Identity.Name)) { return(Unauthorized()); } try { _commentService.UpdateComment(comment); return(Ok()); } catch (AppException ex) { return(BadRequest(new { message = ex.Message })); } }
public async Task <IActionResult> UpdateComment([FromBody] CommentUpdateDto commentUpdate) { await _commentService.UpdateComment(commentUpdate); return(NoContent()); }
public IActionResult UpdateComment([FromHeader(Name = "CommunicationKey")] string key, [FromBody] CommentUpdateDto comment) { if (commentRepository.GetCommentByID(comment.CommentID) == null) { return(StatusCode(StatusCodes.Status400BadRequest, "There is no comment with given ID!")); } var oldComment = commentRepository.GetCommentByID(comment.CommentID); var newComment = mapper.Map <Comments>(comment); if (oldComment.ProductID != newComment.ProductID) { return(StatusCode(StatusCodes.Status400BadRequest, "Product ID can not be changed!")); } try { newComment.UserID = oldComment.UserID; newComment.CommentDate = oldComment.CommentDate; mapper.Map(newComment, oldComment); commentRepository.SaveChanges(); logger.Log(LogLevel.Information, contextAccessor.HttpContext.TraceIdentifier, "", String.Format("Successfully updated comment with ID {0} in database", comment.CommentID), null); return(StatusCode(StatusCodes.Status200OK)); } catch (Exception ex) { logger.Log(LogLevel.Error, contextAccessor.HttpContext.TraceIdentifier, "", String.Format("Comment with ID {0} not updated, message: {1}", comment.CommentID, ex.Message), null); return(StatusCode(StatusCodes.Status500InternalServerError, "Update error")); } }
[TransactionScopeAspect]//+++ public IResult Update(CommentUpdateDto comment) { _commentDal.Update2(comment); return(new SuccessResult(Messages.CommentUpdated)); }
public async Task <IResult> Update(CommentUpdateDto commentUpdateDto) { await _commentDal.CommentUpdate(commentUpdateDto); return(new SuccessResult(Messages.CommentUpdated)); }
public async Task <IActionResult> UpdateComment([FromBody] CommentUpdateDto model) { var result = await _commentService.UpdateAsync(model); return(CreatedAtAction(nameof(GetCommentById), new { id = result.Id }, null)); }