예제 #1
0
        //Get Post Comments
        public ActionResult GetPostComments(int postId)
        {
            if (!_workContext.CurrentCustomer.IsRegistered())
            {
                return(Unauthorized());
            }

            if (!_workContext.CurrentCustomer.IsInCustomerRole(RolesType.Administrators, true) && !_workContext.CurrentCustomer.IsInCustomerRole(RolesType.HaragAdmin, true))
            {
                return(Forbid());
            }

            if (postId == null || postId == 0)
            {
                return(NotFound());
            }
            var commentsInDb = _commentService.GetPostComments(postId);
            var comments     = new CommentOutputModel
            {
                Items = commentsInDb.Select(m => new CommentModel
                {
                    Id           = m.Id,
                    PostId       = m.PostId,
                    Text         = m.Text,
                    CommentedBy  = m.CommentedBy,
                    CommentOwner = m.Customer?.Username,
                    DateCreated  = m.DateCreated,
                    DateUpdated  = m.DateUpdated == null ? m.DateCreated : m.DateUpdated
                }).ToList()
            };

            return(Json(new { data = comments.Items }));
        }
        public void AddComment_Should_Add_Comment()
        {
            //arrange
            var user = new FanFictionUser
            {
                Id       = "user",
                UserName = "******"
            };

            var userManager = (UserManager <FanFictionUser>) this.Provider.GetService(typeof(UserManager <FanFictionUser>));

            userManager.CreateAsync(user).GetAwaiter();
            this.Context.SaveChanges();

            //public int Id { get; set; }

            //public int StoryId { get; set; }

            //public string Author { get; set; }

            //public string Message { get; set; }

            //public DateTime CommentedOn { get; set; }

            var comment = new CommentInputModel
            {
                StoryId       = 1,
                CommentAuthor = "CommentingUser",
                CommentedOn   = DateTime.Now.Date,
                Message       = "SomeComment",
            };

            var commentOut = new CommentOutputModel
            {
                Id          = 1,
                Author      = "CommentingUser",
                CommentedOn = DateTime.Now.Date,
                Message     = "SomeComment",
                StoryId     = comment.StoryId
            };

            //act
            this.CommentService.AddComment(comment);

            //assert
            var result = this.Context.Comments.First();

            var mappedResult = Mapper.Map <CommentOutputModel>(result);

            mappedResult.Should().BeEquivalentTo(commentOut);
        }
예제 #3
0
        public IActionResult GetAllPostComments(int PostId)
        {
            if (!_workContext.CurrentCustomer.IsRegistered())
            {
                return(Unauthorized());
            }

            var currentUserId = _workContext.CurrentCustomer.Id;

            ViewBag.UserName = _workContext.CurrentCustomer.Username;

            if (_workContext.CurrentCustomer.IsInCustomerRole(RolesType.Registered, true))
            {
                var modelFromDb = _commentService.GetCommentsByPostId(PostId);
                var outputModel = new CommentOutputModel
                {
                    //Links = GetLinksForPostComments(model, "Consultant.Comment.GetCommentsByPostId", PostId),
                    Items = modelFromDb.Select(m => new CommentModel()
                    {
                        Id             = m.Id,
                        Text           = m.Text,
                        PostId         = m.PostId,
                        IsCommentOwner = ((int)m.CustomerId == currentUserId),
                        DateCreated    = m.DateCreated,
                        DateUpdated    = m.DateUpdated,
                        CommentedBy    = m.CommentedBy,
                        UserFullName   = m.Customer.GetFullName(),
                        CommentOwner   = m.Customer.Username,
                        PostOwnerId    = (int)m.CustomerId
                                         //Photos = m.Photos.Select(p => p.Url).ToList()
                    }).ToList(),
                };



                return(PartialView("~/Themes/Pavilion/Views/Harag/Comment/CommentsOnPost.cshtml", outputModel));
            }
            else
            {
                return(Unauthorized());
            }
        }
예제 #4
0
        public void AddCommentShouldAddComment()
        {
            //arrange
            var user = new GetShreddedUser
            {
                Id       = "UserId",
                UserName = "******"
            };

            var userManager =
                (UserManager <GetShreddedUser>) this.Provider.GetService(typeof(UserManager <GetShreddedUser>));

            userManager.CreateAsync(user).GetAwaiter();
            this.Context.SaveChanges();

            var comment = new CommentInputModel
            {
                DiaryId     = 1,
                CommentUser = "******",
                CommentedOn = DateTime.Now.Date,
                Message     = "Comment",
            };

            var commentOut = new CommentOutputModel
            {
                Id          = 1,
                User        = "******",
                CommentedOn = DateTime.Now.Date,
                Message     = "Comment",
                DiaryId     = comment.DiaryId
            };

            //act
            this.CommentService.AddComment(comment);

            //assert
            var result = this.Context.Comments.First();

            var mappedResult = Mapper.Map <CommentOutputModel>(result);

            mappedResult.Should().BeEquivalentTo(commentOut);
        }
예제 #5
0
        public IActionResult GetCommentsByPostId(PagingParams pagingParams, int PostId)
        {
            PagedList <Z_Consultant_Comment> model = null;
            bool isPostClosed = _postService.IsClosed(PostId);

            if (!isPostClosed)
            {
                if (!_workContext.CurrentCustomer.IsRegistered())
                {
                    return(Unauthorized());
                }

                var currentUserId = _workContext.CurrentCustomer.Id;
                ViewBag.UserName = _workContext.CurrentCustomer.Username;

                if (_workContext.CurrentCustomer.IsInCustomerRole(RolesType.Consultant, true))
                {
                    ViewBag.UserRole = "Consultant";
                    if (!_postService.IsAnswered(PostId) && !_postService.IsReserved(PostId))
                    {
                        model = _commentService.GetCommentsByPostId(pagingParams, PostId);
                    }
                    else if (_postService.IsConsultantAuthToPost(PostId, currentUserId))
                    {
                        model = _commentService.GetCommentsByPostId(pagingParams, PostId);
                    }
                    else
                    {
                        return(Forbid());
                    }
                }
                else if (_workContext.CurrentCustomer.IsInCustomerRole(RolesType.Registered, true))
                {
                    ViewBag.UserRole = "Registered";
                    if (_postService.IsCustomerAuthToPost(PostId, currentUserId))
                    {
                        model = _commentService.GetCommentsByPostId(pagingParams, PostId);
                    }
                    else
                    {
                        return(Forbid());
                    }
                }
                else
                {
                    return(Unauthorized());
                }
            }
            else
            {
                model = _commentService.GetCommentsByPostId(pagingParams, PostId);
            }


            Response.Headers.Add("X-Pagination", model.GetHeader().ToJson());



            var outputModel = new CommentOutputModel
            {
                Paging = model.GetHeader(),
                Links  = GetLinksForPostComments(model, "Consultant.Comment.GetCommentsByPostId", PostId),
                Items  = model.List.Select(m => new CommentModel()
                {
                    Id           = m.Id,
                    Text         = m.Text,
                    PostId       = m.PostId,
                    DateCreated  = m.DateCreated,
                    DateUpdated  = m.DateUpdated,
                    CommentedBy  = m.CommentedBy,
                    CommentOwner = (m.Customer == null) ? m.Consultant.Username : m.Customer.Username,
                    Photos       = m.Photos.Select(p => p.Url).ToList()
                }).ToList(),
            };

            if (isPostClosed)
            {
                return(PartialView("~/Themes/Pavilion/Views/Consultant/Comment/CommentsOnClosedPost.cshtml", outputModel));
            }
            else
            {
                return(PartialView("~/Themes/Pavilion/Views/Consultant/Comment/CommentsOnOpenPost.cshtml", outputModel));
            }
        }