public ActionResult EditOwnPost(int id, TopicUpdateDto topicUpdateDto) { //gather userID from identity //get post //check if post user id matches identity id //if it does, run code //if not, return 'not allowed HttpResponse' var topicModelFromRepo = _repo.GetTopicById(id); if (topicModelFromRepo == null) { return(NotFound()); } _mapper.Map(topicUpdateDto, topicModelFromRepo); _repo.UpdateTopic(topicModelFromRepo); _repo.SaveChanges(); return(NoContent()); }
public IActionResult UpdateTopic(int id, [FromBody] TopicUpdateDto updateTopic) { var topicEntity = _transActionRepo.GetTopic(id); if (topicEntity == null) { return(NotFound()); } if (updateTopic == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } string userGuid = UserHelper.GetUserGuid(_httpContextAccessor); var getUser = _transActionRepo.GetUsers().FirstOrDefault(c => c.Guid == userGuid); topicEntity.UserId = getUser.UserId; _mapper.Map(updateTopic, topicEntity); if (!_transActionRepo.Save()) { return(StatusCode(500, "A problem happened while handling your request.")); } return(GetTopicById(id)); }
public void UpdateExisitingTopicReturnsNoContentPassed() { var id = 2; var topicUpdate = new TopicUpdateDto() { Title = "Test Title", Body = "Test Body" }; var noContentResult = _controller.UpdateTopic(id, topicUpdate); Assert.IsType <NoContentResult>(noContentResult); }
[Authorize(Policy = Policies.User)] //TODO: make sure the User is the owner of the topic to allow editing it public ActionResult UpdateTopic(int id, TopicUpdateDto topicUpdateDto) { var topicModelFromRepo = _repository.GetTopicById(id); if (topicModelFromRepo == null) { return(NotFound()); } _mapper.Map(topicUpdateDto, topicModelFromRepo); _repository.UpdateTopic(topicModelFromRepo); _repository.SaveChanges(); return(NoContent()); }
public void UpdateExisitingTopicChangesTopicPassed() { var id = 1; var topicUpdate = new TopicUpdateDto() { Title = "Test Title", Body = "Test Body" }; var noContentResult = _controller.UpdateTopic(id, topicUpdate); Assert.Equal(true, _repository.GetTopicById(id).Title == topicUpdate.Title); Assert.Equal(true, _repository.GetTopicById(id).Body == topicUpdate.Body); }
public IActionResult UpdateTopic(int id, [FromBody] TopicUpdateDto updateTopic, int page = 1, int pageSize = 25) { if (!ModelState.IsValid) { return(BadRequest(new TransActionResponse(ModelState))); } var topicEntity = _unitOfWork.Topic.GetTopicById(id); if (topicEntity == null) { return(StatusCode(404, new TransActionResponse("Topic Not Found"))); } string userGuid = UserHelper.GetUserGuid(_httpContextAccessor); var user = _unitOfWork.User.GetCurrentUser(userGuid); if (user.Role.Name.ToLower() == "admin" || user.UserId == topicEntity.UserId) { _mapper.Map(updateTopic, topicEntity); _unitOfWork.Topic.Update(topicEntity); var mess = topicEntity.TraTopicMessage.FirstOrDefault(); mess.Body = updateTopic.Body; if (!_unitOfWork.Save()) { return(StatusCode(500, new TransActionResponse("A problem happened while handling your request."))); } return(GetTopicById(id)); } else { return(StatusCode(StatusCodes.Status401Unauthorized, new TransActionResponse())); } }