Esempio n. 1
0
        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());
            }
        }
Esempio n. 2
0
        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());
            }
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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());
            }
        }
Esempio n. 5
0
 public void Add(Models.Comment comment)
 {
     this.context.Add(comment);
 }
Esempio n. 6
0
 public void Delete(Models.Comment comment)
 {
     this.context.Remove(comment);
 }