示例#1
0
        public async Task <ViewResult> Details(int id, PagingParams pagingParams)
        {
            var commentModel = await Mediator.Send(new GetCommentQuery(id));

            var board = await Mediator.Send(new GetBoardQuery(commentModel.BoardId));

            GetArticleModel parentArticle;

            try
            {
                parentArticle = await Mediator.Send(new GetArticleQuery(commentModel.ParentArticleId));
            }
            catch (NotFoundException)
            {
                parentArticle = new GetArticleModel();
            }

            GetCommentModel parentComment;

            try
            {
                parentComment = await Mediator.Send(new GetCommentQuery(commentModel.ParentCommentId));
            }
            catch (NotFoundException)
            {
                parentComment = new GetCommentModel();
            }

            var childComments = await Mediator.Send(new GetCommentsByIdsQuery(commentModel.CommentIds, pagingParams));

            var user = await Mediator.Send(new GetPublicUserQuery(commentModel.UserId));

            GetPrivateUserModel privateUser = null;

            try
            {
                privateUser = await Mediator.Send(new GetAuthenticatedUserQuery());
            }
            catch (NotFoundException) { }
            catch (UnauthorizedException) { }

            var loggedIn     = privateUser != null && privateUser.Id != 0;
            var savedComment = loggedIn
                                ? privateUser.SavedComments.Contains(id)
                                : false;


            var model = new CommentDetailsViewModel
            {
                Board            = board,
                ChildCommentPage = new FrontendPage <GetCommentModel>(childComments),
                Comment          = commentModel,
                LoggedIn         = loggedIn,
                ParentArticle    = parentArticle,
                ParentComment    = parentComment,
                PostCommentModel = new PostCommentModel(),
                User             = user,
                UserSavedComment = savedComment,
                UserWroteComment = commentModel.UserId == user.Id
            };

            return(View(model));
        }
        public async Task <IViewComponentResult> InvokeAsync(GetCommentModel commentModel)
        {
            // TODO: test with deleted boards
            var getBoardQuery = new GetBoardQuery(commentModel.BoardId);
            var board         = await getBoardQuery.DefaultIfExceptionAsync(_mediator);

            var             getArticleQuery = new GetArticleQuery(commentModel.ParentArticleId);
            GetArticleModel parentArticle   = await getArticleQuery.DefaultIfExceptionAsync(_mediator);

            var             getCommentQuery = new GetCommentQuery(commentModel.ParentCommentId);
            GetCommentModel parentComment   = await getCommentQuery.DefaultIfExceptionAsync(_mediator);

            // TODO: will most likely return null when user is deleted
            var getUserQuery        = new GetPublicUserQuery(commentModel.UserId);
            GetPublicUserModel user = await getUserQuery.DefaultIfExceptionAsync(_mediator);


            string jwt = _apiJwtManager.GetToken();

            bool loggedIn           = false;
            bool saved              = false;
            bool userUpvoted        = false;
            bool userDownvoted      = false;
            bool userCreatedComment = false;

            try
            {
                var loggedInUser = await _mediator.Send(new GetAuthenticatedUserQuery());

                if (loggedInUser != null)
                {
                    loggedIn = true;
                }
                saved              = loggedInUser.SavedComments.Any(commentId => commentId == commentModel.Id);
                userUpvoted        = loggedInUser.LikedComments.Any(commentId => commentId == commentModel.Id);
                userDownvoted      = loggedInUser.DislikedComments.Any(commentId => commentId == commentModel.Id);
                userCreatedComment = commentModel.UserId == loggedInUser.Id;
            }
            catch (System.Exception)
            {
                loggedIn = false;
            }


            var model = new CommentCardViewModel
            {
                Comment            = commentModel,
                Board              = board,
                ParentArticle      = parentArticle,
                ParentComment      = parentComment,
                User               = user,
                Jwt                = jwt,
                LoggedIn           = loggedIn,
                Saved              = saved,
                UserUpvoted        = userUpvoted,
                UserDownvoted      = userDownvoted,
                UserCreatedComment = userCreatedComment
            };

            return(View(model));
        }