public void ShouldHaveErrorWhenTitleIsGreaterThan200Characters()
        {
            // Arrange
            var itemUnderTest = new CreateTodoItemCommandValidator();
            var command       = new CreateTodoItemCommand
            {
                Title = "ABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFG" +
                        "ABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFG" +
                        "ABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFG" +
                        "ABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFG"
            };

            // Act
            var result = itemUnderTest.Validate(command);

            // Assert
            var boolResult = result.IsValid;

            boolResult.ShouldBeFalse();

            var errors = result.Errors;

            errors.ShouldNotBeNull();
            errors.Count.ShouldBe(1);

            var error = errors.First();

            error.ErrorMessage.ShouldBe("The length of 'Title' must be 200 characters or fewer. You entered 203 characters.");
            error.PropertyName.ShouldBe("Title");
        }
Пример #2
0
        public TodoItemDto Handle(CreateTodoItemCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("CreateTodoItemCommand can not be null");
            }

            var todoItem = new TodoItem()
            {
                Id        = Guid.NewGuid().ToString(),
                Title     = command.Title,
                CreatedAt = DateTime.Now,
                Completed = false
            };

            _todoRepository.Insert(todoItem);

            var todoItemDto = new TodoItemDto()
            {
                Id        = todoItem.Id,
                Completed = todoItem.Completed,
                Title     = todoItem.Title,
                CreatedAt = todoItem.CreatedAt
            };

            return(todoItemDto);
        }
Пример #3
0
        public void ShouldRequireMinimumFields()
        {
            var command = new CreateTodoItemCommand();

            FluentActions.Invoking(() =>
                                   SendAsync(command)).Should().Throw <ValidationException>();
        }
Пример #4
0
        public async Task ShouldCreateTodoItem()
        {
            var userId = await RunAsDefaultUserAsync();

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

            var command = new CreateTodoItemCommand
            {
                ListId = listId,
                Title  = "Tasks"
            };

            var itemId = await SendAsync(command);

            var item = await FindAsync <TodoItem>(itemId);

            item.Should().NotBeNull();
            item.ListId.Should().Be(command.ListId);
            item.Title.Should().Be(command.Title);
            item.CreatedBy.Should().Be(userId);
            item.Created.Should().BeCloseTo(DateTime.Now, 10000);
            item.LastModifiedBy.Should().BeNull();
            item.LastModified.Should().BeNull();
        }
Пример #5
0
        public async Task CreateTodoItem(TodoItem todoItem)
        {
            var command  = new CreateTodoItemCommand(todoItem);
            var response = await _mediator.Send(command);

            await Clients.Group(response.TodoListId.ToString()).SendAsync("ReceiveTodoItem", response);
        }
        public async Task <ActionResult <TodoItem> > PostTodoItem(TodoItem todoItem)
        {
            var command  = new CreateTodoItemCommand(todoItem);
            var response = _mediator.Send(command);

            return(CreatedAtAction("GetTodoItem", new { id = response.Id }, todoItem));
        }
Пример #7
0
        public Task <SimpleResponseDto <TodoItemDto> > Post([FromBody] CreateTodoItemCommand command)
        {
            command.Id = Guid.NewGuid();
            Task task = commandBus.Send(command);

            task.Wait();
            return(Get(command.Id));
        }
Пример #8
0
        public TodoItemDto Handle(CreateTodoItemCommand itemCommand)
        {
            var todoItem = new TodoItem(itemCommand.Title, itemCommand.Order);

            this._todoRepository.SaveState(todoItem);

            return(this.mapper.Map(todoItem));
        }
Пример #9
0
        public async Task <ActionResult <TodoItemDto> > CreateTodoItem(
            [FromBody] CreateTodoItemCommand command,
            CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var response = await _mediator.Send(request : command, cancellationToken : cancellationToken);

            return(CreatedAtAction(actionName: nameof(GetTodoItem), routeValues: new { id = response.Id }, value: response));
        }
Пример #10
0
        public IViewComponentResult Invoke(int listId)
        {
            var command = new CreateTodoItemCommand()
            {
                ListId = listId
            };

            return(View(command));
        }
Пример #11
0
        public void Handle_WithNonExistentTodoItemId_ShouldTrowException()
        {
            // Arrange
            CreateTodoItemCommand command = null;

            _mockTodoRepository.Setup(s => s.Insert(It.IsAny <TodoItem>()));

            // Assert
            Assert.Throws <ArgumentNullException>(() => _todoItemsCommandHandler.Handle(command));
        }
Пример #12
0
        public async Task WhenTheUserCreatesANewItemInTheList(string itemTitle)
        {
            _createTodoItemCommand = new CreateTodoItemCommand
            {
                ListId = _listId,
                Title  = itemTitle
            };

            _itemId = await AppHooks.SendAsync(_createTodoItemCommand);
        }
Пример #13
0
        public void Handle(CreateTodoItemCommand message)
        {
            var list = FindList(message.ListId);

            if (list == null)
            {
                return;
            }

            list.AddItem(message.ItemId, message.Text, message.IsCompleted);
            _repository.Save(list);
        }
Пример #14
0
        public async Task GivenValidCreateTodoItemCommand_ReturnsSuccessCode()
        {
            var client = await _factory.GetAuthenticatedClientAsync();

            var command = new CreateTodoItemCommand
            {
                Title = "Do yet another thing."
            };

            var content = IntegrationTestHelper.GetRequestContent(command);

            var response = await client.PostAsync($"/api/todoitems", content);

            response.EnsureSuccessStatusCode();
        }
Пример #15
0
        public async Task GivenInvalidCreateTodoItemCommand_ReturnsBadRequest()
        {
            var client = await _factory.GetAuthenticatedClientAsync();

            var command = new CreateTodoItemCommand
            {
                Title = "This description of this thing will exceed the maximum length. This description of this thing will exceed the maximum length. This description of this thing will exceed the maximum length. This description of this thing will exceed the maximum length."
            };

            var content = IntegrationTestHelper.GetRequestContent(command);

            var response = await client.PostAsync($"/api/todoitems", content);

            response.StatusCode.ShouldBe(HttpStatusCode.BadRequest);
        }
Пример #16
0
        public async Task Handle_ShouldPersistTodoItem()
        {
            var command = new CreateTodoItemCommand
            {
                Title = "Do yet another thing."
            };

            var handler = new CreateTodoItemCommand.CreateTodoItemCommandHandler(Context);

            var result = await handler.Handle(command, CancellationToken.None);

            var entity = Context.TodoItems.Find(result);

            entity.ShouldNotBeNull();
            entity.Title.ShouldBe(command.Title);
        }
Пример #17
0
        public void CreateTodoItemCommand_MapsTo_DomainTodoItem()
        {
            // Arrange
            var command = new CreateTodoItemCommand();

            // Act
            var result = Sut.Map <Domain.Entities.TodoItem>(command);

            // Assert
            Assert.NotNull(result);

            // Business Validations
            Assert.NotNull(result.Id);
            Assert.IsType <string>(result.Id);
            Assert.NotEqual(DateTimeOffset.MinValue, result.CreatedOn);
        }
Пример #18
0
        public async Task CreateTodoItemHandler_CallsDependencies()
        {
            // Arrange
            var request    = new CreateTodoItemCommand();
            var dataAccess = new Mock <ITodoItemCommandDataAccess>();
            var mapper     = new Mock <IMapper>();

            mapper.Setup(x => x.Map <Domain.Entities.TodoItem>(request)).Returns(new Domain.Entities.TodoItem());
            var sut = new CreateTodoItemHandler(dataAccess.Object, mapper.Object);

            // Act
            var result = await sut.Handle(request, CancellationToken.None);

            // Assert
            dataAccess.Verify(x => x.Create(It.IsAny <Domain.Entities.TodoItem>(), CancellationToken.None), Times.Once);
            mapper.Verify(x => x.Map <Domain.Entities.TodoItem>(request), Times.Once);
        }
Пример #19
0
        public async Task Handle_ShouldPersistTodoItem()
        {
            var command = new CreateTodoItemCommand
            {
                Name = "Do yet another thing."
            };

            var sut = new CreateTodoItemCommand.CreateTodoItemCommandHandler(Context);

            var result = await sut.Handle(command, CancellationToken.None);

            var entity = Context.TodoItems.Find(result);

            entity.ShouldNotBeNull();
            entity.Name.ShouldBe(command.Name);
            entity.IsComplete.ShouldBeFalse();
        }
Пример #20
0
        public async Task <HttpResponseData> CreateTodoItem([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "todolists/{id}/items")]
                                                            HttpRequestData req,
                                                            int id,
                                                            FunctionContext functionContext)
        {
            logger.LogInformation("Called CreateTodoItems");

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

            var command = new CreateTodoItemCommand
            {
                ListId = id,
                Title  = todoList.Title
            };

            return(await this.processor.ExecuteAsync <CreateTodoItemCommand, int>(functionContext,
                                                                                  req,
                                                                                  command,
                                                                                  (r) => req.CreateObjectCreatedResponseAsync($"todolists/{id}/items", r)));
        }
        public async Task ShouldCreateTodoItem()
        {
            var categoryId = await SendAsync(new CreateTodoCategoryCommand
            {
                CategoryTitle = "Belajar"
            });

            var command = new CreateTodoItemCommand
            {
                CategoryId    = categoryId,
                ActivityTitle = "Belajar Membaca"
            };

            var itemId = await SendAsync(command);

            var item = await FindAsync <TodoItem>(itemId);

            item.Should().NotBeNull();
            item.CategoryId.Should().Be(command.CategoryId);
            item.ActivityTitle.Should().Be(command.ActivityTitle);
        }
        public IActionResult Post([FromBody] TodoItemDto todoItemDto)
        {
            try
            {
                var command = new CreateTodoItemCommand(todoItemDto.Description, todoItemDto.IsCompleted);
                command.Validate();

                if (command.Invalid)
                {
                    return(Ok(command));
                }

                _todoItemCommandHandler.Handle(command);

                return(Ok());
            }
            catch (Exception e)
            {
                return(BadRequest(e));
            }
        }
        public async Task Handle_ShouldPersistTodoItem()
        {
            //arrange
            var todoItemEntities = new List <TodoItem>();

            var mockedDbContext = new Mock <IApplicationDbContext>();

            mockedDbContext.Setup(db => db.TodoItems.Add(It.IsAny <TodoItem>())).Callback((TodoItem entity) => todoItemEntities.Add(entity));

            //act
            var command = new CreateTodoItemCommand
            {
                Title = "Do yet another thing."
            };

            var handler = new CreateTodoItemCommand.CreateTodoItemCommandHandler(mockedDbContext.Object);

            var result = await handler.Handle(command, CancellationToken.None);

            //assert
            todoItemEntities.Count().ShouldBe(1);
            todoItemEntities[0].Title.ShouldBe(command.Title);
        }
 public async Task <ActionResult <int> > Create(CreateTodoItemCommand command)
 {
     return(await Mediator.Send(command));
 }
 public async Task <ActionResult <int> > Create(CreateTodoItemCommand command)
 {
     //envia el comando a la aplicacion a a traves del mediator
     return(await Mediator.Send(command));
 }
Пример #26
0
        public IActionResult Create([FromBody] CreateTodoItemCommand cmd)
        {
            this._createTodoItemItemCommandHandler.Handle(cmd);

            return(this.Ok());
        }
Пример #27
0
        public async Task <ActionResult <TodoItem> > Handler(CreateTodoItemCommand command)
        {
            TodoItem result = await _dispatcher.DispatchAsync(command);

            return(result);
        }
 public async Task <ActionResult <long> > PostTodoItem(
     CreateTodoItemCommand command)
 {
     return(await _mediator.Send(command));
 }
 public Task <ApiResponse <int> > CreateTodoItem(CreateTodoItemCommand command) =>
 this.PostJson <CreateTodoItemCommand, int>("api/TodoItems", command);
Пример #30
0
        public async Task <ActionResult <int> > Create(CreateTodoItemCommand command)
        {
            await Mediator.Send(command);

            return(this.Redirect("/Todo"));
        }