public async Task <ActionResult> EditArticle(long id, [FromForm] EditArticleCommand command) { command.SetArticleId(id); await Handle(command); return(NoContent()); }
public void ShouldNotCallHandleIfArticleNotExist() { dbSetArticle.Setup(x => x.FindAsync(id)).Returns(null); context.Setup(x => x.Articles).Returns(dbSetArticle.Object); EditArticleCommandHandler editArticleCommandHandler = new EditArticleCommandHandler(context.Object, stringLocalizer.Object, mapper.Object); EditArticleCommand editArticleCommand = new EditArticleCommand(articleDto); Func <Task> act = async() => await editArticleCommandHandler.Handle(editArticleCommand, new CancellationToken()); act.Should().Throw <NotFoundException>(); }
public EditArticleCommandHandlerTests() { _articleManagementService = Mock.Of <IArticleManagementService>(); _articleCommandHandler = new EditArticleCommandHandler(_articleManagementService); _editArticleCommand = new EditArticleCommand { AuthorId = Guid.NewGuid(), AuthorName = "Author Name", Content = "Some content", Id = 123, Topic = "Some topic" }; }
public async Task ShouldCallHandle() { dbSetArticle.Setup(x => x.FindAsync(id)).Returns(new ValueTask <Article>(Task.FromResult(article))); context.Setup(x => x.Articles).Returns(dbSetArticle.Object); context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(1)); EditArticleCommandHandler editArticleCommandHandler = new EditArticleCommandHandler(context.Object, stringLocalizer.Object, mapper.Object); EditArticleCommand editArticleCommand = new EditArticleCommand(articleDto); var result = await editArticleCommandHandler.Handle(editArticleCommand, new CancellationToken()); mapper.Verify(x => x.Map(editArticleCommand, article), Times.Once); result.Should().Be(Unit.Value); }
public void ShouldNotCallHandleIfNotSavedChanges() { dbSetArticle.Setup(x => x.FindAsync(id)).Returns(new ValueTask <Article>(Task.FromResult(new Article { Id = id }))); context.Setup(x => x.Articles).Returns(dbSetArticle.Object); context.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(0)); EditArticleCommandHandler editArticleCommandHandler = new EditArticleCommandHandler(context.Object, stringLocalizer.Object, mapper.Object); EditArticleCommand editArticleCommand = new EditArticleCommand(articleDto); Func <Task> act = async() => await editArticleCommandHandler.Handle(editArticleCommand, new CancellationToken()); act.Should().Throw <RestException>(); }
public void ShouldRequireFieldAndThrowValidationExceptionDuringArticleEditing(string testTitle, string testAbstract, string testContent) { //arrange var command = new EditArticleCommand() { Id = Guid.NewGuid(), Title = testTitle, Abstract = testAbstract, Image = null, Content = testContent }; //act //assert FluentActions.Invoking(() => SendAsync(command)).Should().Throw <Common.Exceptions.ValidationException>(); }
public async Task <ApiResult> EditArticleAsync([FromBody] EditArticleCommand command) { if (!ModelState.IsValid) { return(InValidModelsError()); } var user = _authorizationService.GetUser(); if (user == null) { return(AuthorizeError()); } _log.LogInformation("执行EditArticleAsync...控制器方法"); return(await _mediator.Send(command)); }
public async Task Handle_ArticleManagementServiceThrows_UnsuccessfulWithException() { var exception = new Exception(); var article = new Article { Topic = "test topic" }; this.mockArticleRepository .Setup(s => s.UpdateAsync(It.IsAny <Article>())) .Throws(exception); this.editArticleCommand = new EditArticleCommand(article); var result = await this.editArticleCommandHandler .Handle(this.editArticleCommand, CancellationToken.None); Assert.False(result.Successful); Assert.Same(exception, result.Exception); }
private async Task PrepareArticleToEdit(EditArticleCommand command) { var articleCategories = await PrepareArticleCategories(command.ArticleCategories); var tags = PrepareTags(command.Tags); var lastModifiedAt = DateTimeHelpers.GetCurrenTime(); var publishAt = PreparePublishAtDate(command.PublishAt, lastModifiedAt); var slug = PrepareSlug(command.Title, publishAt); var photoName = await fileService.UploadImage(command.TitlePhotoFile); article.EditArticle( command.Title, photoName, slug, command.ShortText, command.ArticleText, command.ReadTime, lastModifiedAt, publishAt, tags, articleCategories); }
public async Task ShouldUpdateArticle() { //arrange var loggedUser = await RunAsUserAsync("scott101@localhost", "Pa$$w0rd!"); var articleToCreate = new Faker <ArticleCreateOrEditDto>("en") .RuleFor(a => a.Id, f => f.Random.Guid()) .RuleFor(a => a.Title, f => f.Lorem.Sentence()) .RuleFor(a => a.Abstract, f => f.Lorem.Paragraph()) .RuleFor(a => a.Image, f => null) .RuleFor(a => a.Content, f => f.Lorem.Paragraphs(5)) .Generate(); var articleId = await SendAsync(new CreateArticleCommand(articleToCreate)); var articleToModify = new Faker <ArticleCreateOrEditDto>("en") .RuleFor(a => a.Id, f => articleToCreate.Id) .RuleFor(a => a.Title, f => f.Lorem.Sentence()) .RuleFor(a => a.Abstract, f => f.Lorem.Paragraph()) .RuleFor(a => a.Image, f => null) .RuleFor(a => a.Content, f => f.Lorem.Paragraphs(5)) .Generate(); //act var command = new EditArticleCommand(articleToModify); await SendAsync(command); var modifiedArticle = await FindByGuidAsync <Article>(articleId); //assert modifiedArticle.Id.Should().Be(articleToModify.Id); modifiedArticle.Title.Should().Be(articleToModify.Title); modifiedArticle.Abstract.Should().Be(articleToModify.Abstract); modifiedArticle.Image.Should().BeNull(); modifiedArticle.Content.Should().Be(articleToModify.Content); modifiedArticle.LastModifiedBy.Should().Be(loggedUser); modifiedArticle.LastModified.Should().BeCloseTo(DateTime.UtcNow, 1000); }
public async Task EditArticle(EditArticleCommand command) { await CheckIfUserCanEditorDeleteArticle(command.UserId, command.ArticleId); await PrepareArticleToEdit(command); await UpdateArticle(); }
public async Task <ActionResult> Edit( int id, EditArticleCommand command) => await this.Send(command.SetId(id));