示例#1
0
        public async Task Task_Create()
        {
            var connection = TestHelper.GetConnection();
            var options    = TestHelper.GetMockDBOptions(connection);

            try
            {
                using (var context = new AppcentTodoContext(options))
                {
                    var service = new AddTodoCommandHandler(context, TestHelper.GetMapperInstance());
                    var command = new AddTodoCommand();
                    command.Data = new AddTodoRequest
                    {
                        UserName     = "******",
                        Name         = "Task test",
                        CategoryId   = 1,
                        TaskPriority = TaskPriority.P1,
                        TaskStatus   = Models.EntityModels.TaskStatus.Todo
                    };
                    var result = await service.Execute(command);

                    Assert.True(result.Result.IsSuccess);
                }

                using (var context = new AppcentTodoContext(options))
                {
                    var count = context.AcTasks.Where(e => e.Name == "Task test");
                    Assert.Equal(1, count.Count());
                }
            }
            finally
            {
                connection.Close();
            }
        }
        public async Task <IActionResult> AddTodo([FromBody] AddTodoCommand command)
        {
            var todo = await _mediator.Send(command);

            if (todo == null)
            {
                return(BadRequest("Failed saving"));
            }

            return(Ok(todo));
        }
示例#3
0
        public async Task <ActionResult> AddTodo([FromBody] AddTodoDto viewRequest)
        {
            if (!TryValidateModel(viewRequest))
            {
                return(BadRequest(ValidationHelper.GetModelErrors(ModelState)));
            }

            var request = this._mapper.Map <AddTodoRequest>(viewRequest);

            request.UserName = HttpContext.User.Identity.Name;
            var command = new AddTodoCommand {
                Data = request
            };

            return(await Go(command));
        }
示例#4
0
        public async Task WhenAddingTodoWithNoTitle_ThenTheRequestShouldFail()
        {
            fixture.Reset();

            // Arrange
            var command = new AddTodoCommand
            {
                Title = null
            };

            // Act
            Func <Task> act = () => fixture.Mediator.Send(command);

            // Assert
            await act.Should().ThrowAsync <ValidationException>().WithMessage("*'Title' must not be empty.*");
        }
示例#5
0
        public async Task WhenAddingAValidTodo_ThenTheCommandShouldSucceed()
        {
            fixture.Reset();

            // Arrange
            var command = new AddTodoCommand
            {
                Title = "My test todo"
            };

            // Act
            var response = await fixture.Mediator.Send(command);

            // Assert
            response.Id.Should().BeGreaterThan(0);

            fixture.UnitOfWorkMock.Verify(uow => uow.CompleteAsync(It.IsAny <CancellationToken>()), Times.Once());
        }
示例#6
0
        public async Task <IActionResult> AddTodo(AddTodoCommand request)
        {
            var response = await _mediator.Send(request);

            return(Created(Url.Action(nameof(GetTodo), new { id = response.Id }), response));
        }
示例#7
0
        public async Task <IActionResult> Add([FromBody] AddTodoCommand command)
        {
            await mediator.Send(command);

            return(Ok());
        }
示例#8
0
 public Task <IActionResult> Add(AddTodoCommand command) => _mediator.Send(command).ToActionResult();
示例#9
0
 private static EitherAsync <TodoFailure, TodoItem> Validate(AddTodoCommand request) =>
 TodoItem.New(Guid.NewGuid(), request.IsDone, request.Content)
 .ToEither().ToAsync()
 .MapLeft(TodoFailureCon.Validation);
示例#10
0
 public async Task <Either <TodoFailure, Unit> > Handle(AddTodoCommand request, CancellationToken cancellationToken) =>
 await
     (from item in Validate(request)
     from _ in _todoItemRepository.Add(item)
     select _).ToEither();