public async Task <IActionResult> AddComment(CommentToCreateDto commentToCreate) { var comment = _mapper.Map <Comment>(commentToCreate); comment.article = await _repo.GetArticleAsync(commentToCreate.ArticleId); comment.Commenter = await _repo.GetUserAsync(commentToCreate.CommenterId); _repo.Add(comment); if (await _repo.SaveAll()) { return(Ok(comment)); } throw new Exception("Comment failed to post"); }
public async Task <IActionResult> AddComment(CommentToCreateDto commentToCreate) { var commentForAdd = _mapper.Map <Comment>(commentToCreate); commentForAdd.Ticket = await _repo.GetTicket(commentToCreate.TicketId); commentForAdd.Commenter = await _repo.GetUser(commentToCreate.CommenterId, false); commentForAdd.Updated = commentToCreate.Created; _repo.Add(commentForAdd); if (await _repo.SaveAll()) { return(Ok(commentForAdd)); } throw new Exception("Commect is not posted"); }
public async Task <ActionResult <CommentForReturnDto> > CreateComment([FromBody] CommentToCreateDto commentDto) { try { Comment commentToCreate = new Comment { Text = commentDto.Text, Created = DateTime.Now, Post = commentDto.Post, User = commentDto.User }; await _repo.Create(commentToCreate); if (await _repo.Save()) { return(CreatedAtAction(nameof(GetCommentById), new { id = commentToCreate.Id }, _mapper.Map <CommentForReturnDto>(commentDto))); } } catch (Exception e) { return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Failed to create the comment. Exception thrown when attempting to add data to the database: {e.Message}")); } return(BadRequest()); }
public async Task <ActionResult <CommentForReturnDto> > UpdateCommentById(int id, CommentToCreateDto commentDto) { try { var existingComment = await _repo.GetById(id); if (existingComment == null) { return(NotFound($"Could not find a comment with id: {id}")); } Comment commentToUpdate = _mapper.Map(commentDto, existingComment); commentToUpdate.Created = DateTime.Now; _repo.Update(commentToUpdate); if (await _repo.Save()) { return(CreatedAtAction(nameof(GetCommentById), new { id = commentToUpdate.Id }, _mapper.Map <CommentForReturnDto>(commentToUpdate))); } } catch (Exception e) { return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Failed to update comment. Exception thrown when attempting to retrieve data from the database: {e.Message}")); } return(BadRequest()); }