示例#1
0
        public async Task <ActionResult> Edit(CommentCardViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Details", new { viewModel.Comment.Id }));
            }

            // messy, should be a PostCommentModel on the viewModel
            PostCommentModel updateModel = new PostCommentModel
            {
                Text = viewModel.Comment.Text
            };
            await Mediator.Send(new UpdateCommentCommand(viewModel.Comment.Id, updateModel));

            return(RedirectToAction("Details", new { id = viewModel.Comment.Id }));
        }
        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));
        }