コード例 #1
0
        public async Task ShouldUpdateTodoList()
        {
            var userId = await RunAsDefaultUserAsync();

            var listId = await SendAsync(new CreateTodoListCommand
            {
                Title = "New List"
            });

            var command = new UpdateTodoListCommand
            {
                Id    = listId,
                Title = "Updated List Title"
            };

            await SendAsync(command);

            var list = await FindAsync <TodoList>(listId);

            list.Title.Should().Be(command.Title);
            list.LastModifiedBy.Should().NotBeNull();
            list.LastModifiedBy.Should().Be(userId);
            list.LastModified.Should().NotBeNull();
            list.LastModified.Should().BeCloseTo(DateTime.Now, 1000);
        }
コード例 #2
0
        public async Task ShouldRequireUniqueTitle()
        {
            var response = await SendAsync(new CreateTodoListCommand
            {
                Title = "New List"
            });

            var listId = response.Data;

            await SendAsync(new CreateTodoListCommand
            {
                Title = "Other List"
            });

            var command = new UpdateTodoListCommand
            {
                Id    = listId,
                Title = "Other List"
            };

            FluentActions.Invoking(() =>
                                   SendAsync(command))
            .Should().Throw <ValidationException>().Where(ex => ex.Errors.ContainsKey("Title"))
            .And.Errors["Title"].Should().Contain("The specified title already exists.");
        }
コード例 #3
0
        public async Task <IActionResult> PutTodoList(int id, UpdateTodoListCommand command)
        {
            if (id != command.Id)
            {
                return(BadRequest());
            }

            await Sender.Send(command);

            return(NoContent());
        }
コード例 #4
0
        public async Task <ActionResult> Update(long id, UpdateTodoListCommand command)
        {
            if (id != command.Id)
            {
                return(BadRequest());
            }

            await Mediator.Send(command);

            return(NoContent());
        }
コード例 #5
0
        public void ShouldRequireValidTodoListId()
        {
            var command = new UpdateTodoListCommand
            {
                Id    = 99,
                Title = "New Title"
            };

            FluentActions.Invoking(() =>
                                   SendAsync(command)).Should().Throw <NotFoundException>();
        }
コード例 #6
0
        public void Handle_GivenInvalidId_ThrowsException()
        {
            var command = new UpdateTodoListCommand
            {
                Id    = 99,
                Title = "Bucket List",
            };

            var handler = new UpdateTodoListCommand.UpdateTodoListCommandHandler(Context);

            Should.ThrowAsync <NotFoundException>(() =>
                                                  handler.Handle(command, CancellationToken.None));
        }
コード例 #7
0
        public void IsValid_ShouldBeTrue_WhenListTitleIsUnique()
        {
            var command = new UpdateTodoListCommand
            {
                Id    = 1,
                Title = "Shopping"
            };

            var validator = new UpdateTodoListCommandValidator(Context);

            var result = validator.Validate(command);

            result.IsValid.ShouldBe(true);
        }
コード例 #8
0
        public async Task <IActionResult> Update(
            [FromBody] UpdateTodoListCommand command)
        {
            try
            {
                await Mediator.Send(command);
            }
            catch (ValidationException ve)
            {
                return(BadRequest(ve.Message));
            }

            return(Ok());
        }
コード例 #9
0
 public TodoListsController(
     GetTodoListsQuery getTodoListsQuery,
     GetTodoListByIdQuery getTodoListByIdQuery,
     GetTodoListItemsQuery getTodoListItemsQuery,
     CreateTodoListCommand createTodoListCommand,
     UpdateTodoListCommand updateTodoListCommand,
     RemoveTodoListCommand removeTodoListCommand
     )
 {
     this.getTodoListsQuery     = getTodoListsQuery;
     this.getTodoListByIdQuery  = getTodoListByIdQuery;
     this.getTodoListItemsQuery = getTodoListItemsQuery;
     this.createTodoListCommand = createTodoListCommand;
     this.updateTodoListCommand = updateTodoListCommand;
     this.removeTodoListCommand = removeTodoListCommand;
 }
コード例 #10
0
        public async Task Handle_GivenValidId_ShouldUpdatePersistedTodoList()
        {
            var command = new UpdateTodoListCommand
            {
                Id    = 1,
                Title = "Shopping",
            };

            var handler = new UpdateTodoListCommand.UpdateTodoListCommandHandler(Context);

            await handler.Handle(command, CancellationToken.None);

            var entity = Context.TodoLists.Find(command.Id);

            entity.ShouldNotBeNull();
            entity.Title.ShouldBe(command.Title);
        }
コード例 #11
0
        public void IsValid_ShouldBeFalse_WhenListTitleIsNotUnique()
        {
            Context.TodoLists.Add(new TodoList {
                Title = "Shopping"
            });
            Context.SaveChanges();

            var command = new UpdateTodoListCommand
            {
                Id    = 2,
                Title = "Shopping"
            };

            var validator = new UpdateTodoListCommandValidator(Context);

            var result = validator.Validate(command);

            result.IsValid.ShouldBe(false);
        }
コード例 #12
0
        public async void ShouldThrowNotFoundException_WhenIdDoesNotExistInDatabase()
        {
            // Arrange
            var command = new UpdateTodoListCommand
            {
                Id    = 99,
                Title = "New Title 31/10/2020"
            };

            // Act
            var exception = await Record.ExceptionAsync(async() =>
            {
                await _fixture.SendAsync(command);
            });

            // Assert
            exception.ShouldBeOfType <NotFoundException>();
            exception.Message.ShouldContain("Entity \"TodoList\" (99) was not found.");
        }
コード例 #13
0
        public async Task <HttpResponseData> UpdateTodosList([HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "todolists/{id}")]
                                                             HttpRequestData req,
                                                             int id,
                                                             FunctionContext functionContext)
        {
            logger.LogInformation("Called UpdateTodosList");

            var todoList = await req.ReadFromJsonAsync <TodoList>();

            var request = new UpdateTodoListCommand
            {
                Id    = id,
                Title = todoList.Title
            };

            return(await this.processor.ExecuteAsync <UpdateTodoListCommand, Unit>(functionContext,
                                                                                   req,
                                                                                   request,
                                                                                   (r) => req.CreateResponseAsync()));
        }
コード例 #14
0
        public async void ShouldUpdateTodoList_WhenValidListIsSentIn()
        {
            var userId = await _fixture.RunAsDefaultUserAsync();

            var listId = await _fixture.SendAsync(new CreateTodoListCommand
            {
                Title = "New List 17:25"
            });

            var command = new UpdateTodoListCommand
            {
                Id    = listId,
                Title = "Updated List Title 17:25"
            };

            await _fixture.SendAsync(command);

            var list = await _fixture.FindAsync <TodoList>(listId);

            list.Title.ShouldBe(command.Title);
            list.LastModified.ShouldNotBeNull();
        }
コード例 #15
0
        public async void ShouldThrowException_WhenTitleAlreadyExists()
        {
            // Arrange
            var listId = await _fixture.SendAsync(new CreateTodoListCommand
            {
                Title = "New List 17:10"
            });

            await _fixture.SendAsync(new CreateTodoListCommand
            {
                Title = "Other List 17:12"
            });

            var command = new UpdateTodoListCommand
            {
                Id    = listId,
                Title = "Other List 17:12"
            };

            // Act
            var exception = (ValidationException)await Record.ExceptionAsync(async() =>
            {
                await _fixture.SendAsync(command);
            });

            // Assert
            exception.ShouldBeOfType <ValidationException>();
            exception.Message.ShouldContain("One or more validation failures have occurred.");

            var errors = exception.Errors;

            errors.ShouldNotBeNull();

            errors.TryGetValue("Title", out string[] errorText);
            errorText.ShouldNotBeNull();
            errorText.Count().ShouldBe(1);
            errorText[0].ShouldBe("The specified title already exists.");
        }
コード例 #16
0
        public async Task HandleAsync(UpdateTodoListCommand command, CancellationToken cancellationToken)
        {
            var todoList = await repository.GetAsync <TodoList>(command.Id);

            todoList.Rename(command.Name);
        }
コード例 #17
0
        public async Task <ActionResult> Update(UpdateTodoListCommand command)
        {
            await Mediator.Send(command);

            return(NoContent());
        }
コード例 #18
0
 public Task UpdateTodoList(UpdateTodoListCommand command) =>
 this.PutJson("api/TodoLists", command);