public async Task <IActionResult> CreateComment([FromBody] AddCommentResource commentResource) { Models.Photo photo = await this.photoRepository.GetAsync(commentResource.PhotoId); Models.Comment parentComment = commentResource.ParentId.HasValue ? await commentRepository.GetAsync(commentResource.ParentId.Value) : null; bool isParentCommentFromSamePhoto = (parentComment != null) && (parentComment.Photo.Id == commentResource.PhotoId); if (photo != null && (!commentResource.ParentId.HasValue || isParentCommentFromSamePhoto)) { var comment = this.mapper.Map <AddCommentResource, Models.Comment>(commentResource); var currentUser = new Models.User() { UserName = User.FindFirstValue(ClaimTypes.Name) }; comment.Author = this.userRepository.GetOrAdd(currentUser); comment.Photo = photo; this.commentRepository.Add(comment); await this.unitOfWork.CompleteAsync(); CommentResource resourceResult = mapper.Map <Models.Comment, CommentResource>(comment); resourceResult.ConnectionId = commentResource.ConnectionId; NotifyCommentAdded(comment.Id, commentResource.ConnectionId); return(Ok(resourceResult)); } else { return(BadRequest()); } }
public async Task <IActionResult> UpdateComment([FromRoute] int id, [FromForm] CommentResource commentResource) { Models.Comment comment = await commentRepository.GetAsync(id); if (comment != null) { var userName = User.FindFirstValue(ClaimTypes.Name); if (comment.Author.UserName != userName) { return(Forbid()); } if (string.IsNullOrEmpty(commentResource.Content)) { return(BadRequest()); } comment.Content = commentResource.Content; comment.ModifiedDate = DateTime.UtcNow; await this.unitOfWork.CompleteAsync(); var outputCommentResource = mapper.Map <Models.Comment, CommentResource>(comment); return(Ok(outputCommentResource)); } else { return(NotFound()); } }
private IEnumerable <int> GetDeletedIds(Models.Comment comment) { List <int> deletedIds = new List <int>(); var replies = GetReplies(comment.Id); foreach (Models.Comment reply in replies) { deletedIds.AddRange(GetDeletedIds(reply)); } deletedIds.Add(comment.Id); return(deletedIds); }
public async Task <IActionResult> DeleteComment([FromRoute] int id) { Models.Comment comment = await commentRepository.GetAsync(id); if (comment != null) { var userName = User.FindFirstValue(ClaimTypes.Name); if (comment.Author.UserName != userName) { return(Forbid()); } List <Models.Comment> replies = this.commentRepository.GetReplies(id).ToList(); this.commentRepository.DeleteAll(replies); this.commentRepository.Delete(comment); await this.unitOfWork.CompleteAsync(); return(Ok()); } else { return(NotFound()); } }
public void Add(Models.Comment comment) { this.context.Add(comment); }
public void Delete(Models.Comment comment) { this.context.Remove(comment); }