Beispiel #1
0
        public async Task User_Can_Edit_His_Own_Posts()
        {
            Result editResult;
            var    picture = (Picture)Convert.FromBase64String(DatabaseFixture.GetTestPictureBase64());
            var    author  = new User((Nickname)"authoruser1", (FullName)"author1 user1", Password.Create("password").Value, (Email)"*****@*****.**", "my bio");
            var    post    = new Post(author, picture, (PostText)"test post 1");

            using (var session = _testFixture.OpenSession(_output))
            {
                await session.SaveAsync(author);

                await session.SaveAsync(post);
            }

            var command = new EditPostCommand(author.ID, post.ID, "edited text on post 1", picture);

            using (var session = _testFixture.OpenSession(_output))
            {
                var sut = new EditPostCommandHandler(session, Log.Logger);
                editResult = await sut.Handle(command, default);
            }

            using (var session = _testFixture.OpenSession(_output))
            {
                using (new AssertionScope())
                {
                    session.Load <Post>(post.ID).Text.Value.Should().Be("edited text on post 1");
                    editResult.IsSuccess.Should().BeTrue();
                }
            }
        }
Beispiel #2
0
        public async Task User_Cannot_Edit_Post_Published_By_Another_User()
        {
            Result editResult;
            var    picture = (Picture)Convert.FromBase64String(DatabaseFixture.GetTestPictureBase64());
            var    author  = new User((Nickname)"authoruser1", (FullName)"author1 user1", Password.Create("password").Value, (Email)"*****@*****.**", "my bio");
            var    reader  = new User((Nickname)"readeruser", (FullName)"reader user", Password.Create("password").Value, (Email)"*****@*****.**", "my bio");
            var    post    = new Post(author, picture, (PostText)"test post 1");

            using (var session = _testFixture.OpenSession(_output))
            {
                await session.SaveAsync(author);

                await session.SaveAsync(reader);

                await session.SaveAsync(post);
            }

            var command = new EditPostCommand(reader.ID, post.ID, "edited text on post 1", picture);

            using (var session = _testFixture.OpenSession(_output))
            {
                var sut = new EditPostCommandHandler(session, Log.Logger);
                editResult = await sut.Handle(command, default);
            }

            editResult.IsSuccess.Should().BeFalse($"You're not allowed to edit post {post.ID}.");
        }
Beispiel #3
0
    public void EditPost(Post post)
    {
        bool authorIsCaller = IsPostAuthorMatch <bool> .Execute(new { post.Author });

        if (authorIsCaller)
        {
            EditPostCommand.Execute(new { post.Content });
        }
    }
Beispiel #4
0
 public virtual async Task <IActionResult> Put(int id, [FromBody] EditPostCommand editPostCommand)
 {
     return(await Runsafely(async() =>
     {
         editPostCommand.SetId(id);
         var commandResult = await _mediator.Send(editPostCommand);
         return commandResult ? Ok() : BadRequest();
     }));
 }
        public async Task <ActionResult> Edit(Guid id, [FromBody] PostEditDto editedPost)
        {
            var command = new EditPostCommand(editedPost)
            {
                Id = id
            };
            await Mediator.Send(command);

            return(NoContent());
        }
Beispiel #6
0
        public async Task <ActionResult> Update(Guid id, EditPostCommand command)
        {
            if (id != command.Id)
            {
                return(BadRequest());
            }

            await Mediator.Send(command);

            return(NoContent());
        }
        public void ShouldNotCallHandleIfPostNotExist()
        {
            dbSetPost.Setup(x => x.FindAsync(id)).Returns(null);
            context.Setup(x => x.Posts).Returns(dbSetPost.Object);

            EditPostCommandHandler editPostCommandHandler = new EditPostCommandHandler(context.Object, stringLocalizer.Object);
            EditPostCommand        editPostCommand        = new EditPostCommand(postDto);

            Func <Task> act = async() => await editPostCommandHandler.Handle(editPostCommand, new CancellationToken());

            act.Should().ThrowAsync <NotFoundException>();
        }
        private Negotiator EditPost(EditPostCommand command)
        {
            var commandResult = _commandInvokerFactory.Handle <EditPostCommand, CommandResult>(command);

            if (commandResult.Success)
            {
                AddMessage("文章更新成功", "success");

                return(ShowPostEdit(command.PostId));
            }

            return(View["Edit", commandResult.GetErrors()]);
        }
        private Negotiator EditPost(EditPostCommand command)
        {
            var commandResult = _commandInvokerFactory.Handle<EditPostCommand, CommandResult>(command);

            if (commandResult.Success)
            {
                AddMessage("文章更新成功", "success");

                return ShowPostEdit(command.PostId);
            }

            return View["Edit", commandResult.GetErrors()];
        }
        public void ShouldNotCallHandleIfNotSavedChanges()
        {
            dbSetPost.Setup(x => x.FindAsync(id)).Returns(new ValueTask <Post>(Task.FromResult(post)));
            context.Setup(x => x.Posts).Returns(dbSetPost.Object);
            context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(0));

            EditPostCommandHandler editPostCommandHandler = new EditPostCommandHandler(context.Object, stringLocalizer.Object);
            EditPostCommand        editPostCommand        = new EditPostCommand(postDto);

            Func <Task> act = async() => await editPostCommandHandler.Handle(editPostCommand, new CancellationToken());

            act.Should().ThrowAsync <RestException>();
        }
        public async Task ShouldCallHandle()
        {
            dbSetPost.Setup(x => x.FindAsync(id)).Returns(new ValueTask <Post>(Task.FromResult(post)));
            context.Setup(x => x.Posts).Returns(dbSetPost.Object);
            context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(1));

            EditPostCommandHandler editPostCommandHandler = new EditPostCommandHandler(context.Object, stringLocalizer.Object);
            EditPostCommand        editPostCommand        = new EditPostCommand(postDto);

            var result = await editPostCommandHandler.Handle(editPostCommand, new CancellationToken());

            result.Should().Be(Unit.Value);
        }
Beispiel #12
0
        private Negotiator EditPost(EditPostCommand command)
        {
            var commandResult = _commandInvokerFactory.Handle <EditPostCommand, CommandResult>(command);

            if (commandResult.Success)
            {
                AddMessage("Post was successfully updated", "info");

                return(ShowPostDetails(command.PostId));
            }

            return(View["Details", commandResult.GetErrors()]);
        }
Beispiel #13
0
    public void EditPost(Post post)
    {
        bool authorIsCaller = IsPostAuthorMatch <bool> .Execute(new { post.Author });

        if (authorIsCaller)
        {
            EditPostCommand.Execute(new { post.Content });

            var hash = HashObject(post).ToHexString();

            Connector connector = new Connector(credentialManager.PublicKey, credentialManager.PrivateKey);
            connector.Call("editPostHash", post.Id, hash);
        }
    }
Beispiel #14
0
        public async Task <Result <int> > Handle(EditPostCommand request, CancellationToken cancellationToken)
        {
            using (var tx = _session.BeginTransaction())
            {
                var editor = await _session.LoadAsync <User>(request.UserID, cancellationToken);

                var postToEdit = await _session.QueryOver <Post>()
                                 .Fetch(SelectMode.ChildFetch, p => p.Author)
                                 .Where(p => p.ID == request.PostID)
                                 .SingleOrDefaultAsync(cancellationToken);

                if (!postToEdit.CanBeEditedBy(editor))
                {
                    _logger.Warning("User [{NickName}({UserID})] tried to edit post {PostID} but he wasn't allowed to.",
                                    editor.Nickname,
                                    request.UserID,
                                    request.PostID);
                    return(Result.Failure <int>($"You're not allowed to edit post {postToEdit.ID}."));
                }

                postToEdit.UpdateText(PostText.Create(request.Text).Value);
                postToEdit.UpdatePicture(Picture.Create(request.PictureRawBytes, postToEdit.Picture.Identifier).Value);
                try
                {
                    await _session.SaveAsync(postToEdit, cancellationToken);

                    await tx.CommitAsync(cancellationToken);

                    _logger.Information("User [{Nickname}({UserID})] edited post {PostID}.",
                                        editor.Nickname,
                                        request.UserID,
                                        request.PostID);

                    return(Result.Success(postToEdit.ID));
                }
                catch (ADOException ex)
                {
                    await tx.RollbackAsync(cancellationToken);

                    _logger.Error("Failed to edit post {PostID} for user [{Nickname}({UserID})]. Error message: {ErrorMessage}",
                                  request.PostID,
                                  editor.Nickname,
                                  request.UserID,
                                  ex.Message);

                    return(Result.Failure <int>(ex.Message));
                }
            }
        }
Beispiel #15
0
        public IActionResult EditPost(EditPostCommand command)
        {
            // 设置当前应用静态文件所在目录并执行提交编辑
            command.WebRootPath = this._hostingEnvironment.WebRootPath;
            var commandResultA = this._commandInvokerFactory.Handle <EditPostCommand, CommandResult>(command);

            // 判断是否保存成功
            if (!commandResultA.IsSuccess)
            {
                return(Json(new { code = -1, msg = $"Error:{commandResultA.GetErrors()[0]}", url = string.Empty }));
            }



            this.ClearCache();
            return(Json(new { code = 1, msg = $"Success:提交成功", url = $"/AdminPost/Index/{command.ReturnPageNum}" }));
        }
Beispiel #16
0
        public Posts EditPost(EditPostCommand post)
        {
            using (var db = _paintStoreContext)
            {
                var postToUptade = db.Posts.First(x => x.Id == post.Id);

                if (post.Title != null)
                {
                    postToUptade.Title = post.Title;
                }
                if (post.Description != null)
                {
                    postToUptade.Description = post.Description;
                }
                postToUptade.Edited = true;
                db.SaveChanges();
                return(postToUptade);
            }
        }
Beispiel #17
0
        public async Task <IActionResult> Edit(EditPostModel editedPost, IFormFile pictureFile)
        {
            if (ModelState.IsValid)
            {
                var command       = new EditPostCommand(User.GetIdentifier(), editedPost.PostID, editedPost.Text, await pictureFile.ToByteArrayAsync());
                var commandResult = await _dispatcher.Send(command);

                if (commandResult.IsSuccess)
                {
                    return(RedirectToAction("Index", "Home"));
                }

                ModelState.AddModelError("", commandResult.Error);
            }
            else
            {
                ModelState.AddModelError("Picture", "Please select a picture to share together with your post.");
            }

            return(View(editedPost));
        }
Beispiel #18
0
 public async Task <ActionResult> Edit(int id,
                                       EditPostCommand command)
 => await this.Send(command.SetId(id));
Beispiel #19
0
        public async Task <IActionResult> EditPost([FromBody] Post post, [FromServices] EditPostCommand command)
        {
            await command.ExecuteAsync(post);

            return(Ok());
        }
Beispiel #20
0
 public IActionResult EditPost([FromBody] EditPostCommand post)
 {
     return(Ok(_postsService.EditPost(post)));
 }