예제 #1
0
        public IViewComponentResult Invoke(GetPublicUserModel userModel, string imageDataUrl)
        {
            var model = new UserCardViewModel
            {
                User         = userModel,
                ImageDataUrl = imageDataUrl
            };

            return(View(model));
        }
        public async Task <IViewComponentResult> InvokeAsync(GetArticleModel articleModel, string imageDataUrl, bool displayText = false)
        {
            // TODO: test with deleted boards
            var           getBoardQuery = new GetBoardQuery(articleModel.BoardId);
            GetBoardModel board         = await getBoardQuery.DefaultIfExceptionAsync(_mediator);

            // TODO: returns null, which throws exception in view, if user is deleted
            var getuserQuery        = new GetPublicUserQuery(articleModel.UserId);
            GetPublicUserModel user = await getuserQuery.DefaultIfExceptionAsync(_mediator);

            string jwt = _apiJwtManager.GetToken();

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

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

                if (loggedInUser != null)
                {
                    loggedIn = true;
                }
                saved              = loggedInUser.SavedArticles.Any(articleId => articleId == articleModel.Id);
                userUpvoted        = loggedInUser.LikedArticles.Any(articleId => articleId == articleModel.Id);
                userDownvoted      = loggedInUser.DislikedArticles.Any(articleId => articleId == articleModel.Id);
                userCreatedArticle = loggedInUser.ArticleIds.Any(articleId => articleId == articleModel.Id);
            }
            catch { }

            var viewModel = new ArticleCardViewModel
            {
                Article            = articleModel,
                Board              = board,
                User               = user,
                Jwt                = jwt,
                LoggedIn           = loggedIn,
                Saved              = saved,
                UserUpvoted        = userUpvoted,
                UserDownvoted      = userDownvoted,
                UserCreatedArticle = userCreatedArticle,
                ImageDataUrl       = imageDataUrl,
                DisplayText        = displayText,
                BaseUrl            = _baseUrl
            };

            return(View(viewModel));
        }
예제 #3
0
        public async Task ShouldGetPublicUser()
        {
            // Arrange
            using var scope = Factory.Services.CreateScope();

            var sut = new GetPublicUserHandler(deletedUserValidatorMock.Object, unitOfWork, mediator, mapper, currentUserServiceMock.Object);

            // Act
            GetPublicUserModel sutResult = await sut.Handle(new GetPublicUserQuery(user.Id), new CancellationToken(false));

            // Assert
            Assert.NotNull(sutResult);

            Assert.Equal(user.Id, sutResult.Id);
        }
        public async Task ShouldGetValidUser()
        {
            using var scope = Factory.Services.CreateScope();

            deletedUserValidatorMock.Setup(pv => pv.ValidateEntity(It.IsAny <User>(), It.IsAny <DeletedEntityPolicy>())).Returns(user);

            var sut = new GetUserByUsernameHandler(deletedUserValidatorMock.Object, unitOfWork, mediator, mapper, currentUserServiceMock.Object);

            // Act
            GetPublicUserModel sutResult = await sut.Handle(
                new GetUserByUsernameQuery(user.UserName), new System.Threading.CancellationToken(false));

            // Assert
            Assert.NotNull(sutResult);

            Assert.Equal(user.UserName, sutResult.Username);
            Assert.Equal(user.Id, sutResult.Id);
        }
예제 #5
0
        public async Task <ViewResult> Details(int id, PagingParams pagingParams)
        {
            var articleModel = await Mediator.Send(new GetArticleQuery(id));

            var getArticleCommentsQuery = new GetCommentsByIdsQuery(articleModel.CommentIds, pagingParams);
            var articleComments         = await getArticleCommentsQuery.DefaultIfExceptionAsync(Mediator);

            var           getBoardQuery = new GetBoardQuery(articleModel.BoardId);
            GetBoardModel board         = await getBoardQuery.DefaultIfExceptionAsync(Mediator);

            var getUserQuery        = new GetPublicUserQuery(articleModel.UserId);
            GetPublicUserModel user = await getUserQuery.DefaultIfExceptionAsync(Mediator);

            var privateUser = await new GetAuthenticatedUserQuery().DefaultIfExceptionAsync(Mediator);

            var loggedIn = privateUser != null && privateUser.Id != 0;

            var savedArticle = loggedIn && privateUser.SavedArticles.Contains(id);

            var wroteArticle = loggedIn && privateUser.ArticleIds.Contains(id);

            string imageDataURL = "";

            if (articleModel.AssociatedImageId > 0)
            {
                GetImageModel img = await Mediator.Send(new GetImageByIdQuery(articleModel.AssociatedImageId));

                imageDataURL = _imageDataHelper.ConvertImageDataToDataUrl(img.ImageData, img.ContentType);
            }

            var viewModel = new ArticleDetailsViewModel
            {
                Article                = articleModel,
                Board                  = board,
                CommentPage            = new FrontendPage <GetCommentModel>(articleComments),
                LoggedIn               = loggedIn,
                User                   = user,
                UserSavedArticle       = savedArticle,
                UserWroteArticle       = wroteArticle,
                AssociatedImageDataUrl = imageDataURL
            };

            return(View(viewModel));
        }
        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));
        }