Exemplo n.º 1
0
        public async Task <Dto.TodoDto> Create(CreateTodoDto data)
        {
            var request  = new Modules.Todo.Application.CQRS.Commands.CreateTodoCommand(data.Description);
            var response = await m_mediator.Send(request);

            return(m_mapper.Map <Dto.TodoDto>(response));
        }
        public void TestCreateTodoOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();

            using (var context = new ExampleDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabase();
                var controller = new ToDoController();

                var noCachingConfig = new GenericBizRunnerConfig {
                    TurnOffCaching = true
                };
                var utData      = new NonDiBizSetup(noCachingConfig);
                var bizInstance = new CreateTodoBizLogic(context);
                var service     = new ActionService <ICreateTodoBizLogic>(context, bizInstance, utData.WrappedConfig);

                //ATTEMPT
                var dto = new CreateTodoDto()
                {
                    Name       = "Test",
                    Difficulty = 3,
                };
                var response = controller.Post(dto, service);

                //VERIFY
                response.GetStatusCode().ShouldEqual(201);
                var rStatus = response.CheckCreateResponse("GetSingleTodo", new { id = 7 }, context.TodoItems.Find(7));
                rStatus.IsValid.ShouldBeTrue(rStatus.GetAllErrors());
                rStatus.Message.ShouldEqual("Success");
            }
        }
        public void TestCreateTodoOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();

            using (var context = new ExampleDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabase();
                var controller = new ToDoController();

                var mapper      = BizRunnerHelpers.CreateEmptyMapper();
                var bizInstance = new CreateTodoBizLogic(context);
                var service     = new ActionService <ICreateTodoBizLogic>(context, bizInstance, mapper);

                //ATTEMPT
                var dto = new CreateTodoDto()
                {
                    Name       = "Test",
                    Difficulty = 3,
                };
                var response = controller.Post(dto, service);

                //VERIFY
                response.GetStatusCode().ShouldEqual(201);
                var rStatus = response.CheckCreateResponse("GetSingleTodo", new { id = 7 }, dto);
                rStatus.IsValid.ShouldBeTrue(rStatus.GetAllErrors());
                rStatus.Message.ShouldEqual("Success");
            }
        }
Exemplo n.º 4
0
 public static Todo ToTodo(this CreateTodoDto dto)
 {
     return(new()
     {
         UserId = dto.UserId,
         Description = dto.Description,
         Title = dto.Title
     });
 }
Exemplo n.º 5
0
        public ActionResult <TodoItem> Post(CreateTodoDto item, [FromServices] IActionService <ICreateTodoBizLogic> service)
        {
            var result = service.RunBizAction <TodoItem>(item);

            //NOTE: to get this to work you MUST set the name of the HttpGet, e.g. [HttpGet("{id}", Name= "GetSingleTodo")],
            //on the Get you want to call, then then use the Name value in the Response.
            //Otherwise you get a "No route matches the supplied values" error.
            //see https://stackoverflow.com/questions/36560239/asp-net-core-createdatroute-failure for more on this
            return(service.Status.Response(this, "GetSingleTodo", new { id = result?.Id }, result));
        }
Exemplo n.º 6
0
        public async Task <ActionResult> CreateTodoAsync(CreateTodoDto createTodoDto)
        {
            var response = await _mediator.Send(new CreateTodoCommand(createTodoDto));

            if (!response.Success)
            {
                return(StatusCode(StatusCodes.Status422UnprocessableEntity, response));
            }

            return(Ok(response));
        }
Exemplo n.º 7
0
        public Todo CreateTodo(CreateTodoDto todoCreateDto)
        {
            var todo = new Todo {
                DueDate     = DateTime.Now.AddDays(7),
                IsCompleted = todoCreateDto.IsCompleted.HasValue ? todoCreateDto.IsCompleted.Value : false,
                Name        = todoCreateDto.Name
            };

            _todoContext.Todos.Add(todo);
            return(todo);
        }
Exemplo n.º 8
0
 /// <summary>
 /// 添加代办事项
 /// </summary>
 /// <param name="todo"></param>
 /// <returns></returns>
 public bool AddTodoList(CreateTodoDto todo)
 {
     todoListDbContext.Todos.Add(new Todo
     {
         Content    = todo.Content,
         Priority   = todo.Priority,
         UserId     = todo.UserId,
         StartDate  = todo.StartDate,
         FinishDate = todo.FinishDate
     });
     return(todoListDbContext.SaveChanges() > 0);
 }
Exemplo n.º 9
0
        public TodoItem BizAction(CreateTodoDto inputData)
        {
            if (inputData.Name.EndsWith("!"))
            {
                AddError("Business logic says the name cannot end with !", nameof(inputData.Name));
            }

            var item = new TodoItem(inputData.Name, inputData.Difficulty);

            _context.Add(item);

            Message = $"Successfully saved the todo item '{inputData.Name}'.";
            return(item);
        }
Exemplo n.º 10
0
        public ActionResult <ReadTodoDto> CreateTodoItem(CreateTodoDto todoCreateDto)
        {
            var createdTodo = _todoRepository.CreateTodo(todoCreateDto);

            if (_todoRepository.SaveChanges())
            {
                return(CreatedAtAction(nameof(GetTodoById),
                                       new {
                    Id = createdTodo.Id
                },
                                       MapTodoToReadTodoDto(createdTodo)));
            }
            return(BadRequest());
        }
Exemplo n.º 11
0
        public async Task <TodoDto> AddTodoAsync(CreateTodoDto createTodoDto, string userId)
        {
            await _todoRepository.InsertAsync(createTodoDto.ToTodo());

            await _todoRepository.Save();

            var todo = await _todoRepository.GetAsync(t =>
                                                      (t.Title == createTodoDto.Title) &&
                                                      (t.Description == createTodoDto.Description) &&
                                                      (t.UserId == userId)
                                                      );

            return(todo?.Adapt <TodoDto>());
        }
Exemplo n.º 12
0
        public CreateTodoCommand(CreateTodoDto message)
        {
            if (message is null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            Category    = message.Category;
            City        = message.City;
            Description = message.Description;
            EndDate     = message.EndDate;
            StartDate   = message.StartDate;
            Title       = message.Title;
            Venue       = message.Venue;
        }
Exemplo n.º 13
0
        public async Task <ToDo> CreateTodo(CreateTodoDto todoDto)
        {
            var newTodo = new ToDo
            {
                Title       = todoDto.Title,
                Description = todoDto.Description,
                IsCompleted = false
            };

            await _context.ToDos.AddAsync(newTodo);

            await _context.SaveChangesAsync();

            return(newTodo);
        }
        public void TestCreateStatusRouteValueValueNotMatchOk()
        {
            //SETUP
            var status       = new StatusGenericHandler();
            var controller   = new ToDoController();
            var dto          = new CreateTodoDto();
            var actionResult = status.Response(controller, "Get", new { id = 999 }, dto);

            //ATTEMPT
            var statusCode = actionResult.GetStatusCode();
            var rStatus    = actionResult.CheckCreateResponse("Get", new { id = 7 }, dto);

            //VERIFY
            statusCode.ShouldEqual(CreateResponse.CreatedStatusCode);
            rStatus.IsValid.ShouldBeFalse(rStatus.GetAllErrors());
            rStatus.GetAllErrors().ShouldEqual("RouteValues->id, different values: expected = 7, found = 999");
        }
        public void TestCreateStatusBadDtoOk()
        {
            //SETUP
            var status       = new StatusGenericHandler();
            var controller   = new ToDoController();
            var dto          = new CreateTodoDto();
            var actionResult = status.Response(controller, "Get", new { id = 7 }, dto);

            //ATTEMPT
            var statusCode = actionResult.GetStatusCode();
            var rStatus    = actionResult.CheckCreateResponse("Get", new { id = 7 }, new CreateTodoDto());

            //VERIFY
            statusCode.ShouldEqual(CreateResponse.CreatedStatusCode);
            rStatus.IsValid.ShouldBeFalse(rStatus.GetAllErrors());
            rStatus.GetAllErrors().ShouldEqual("DTO: the returned DTO instance does not match the test DTO: expected CreateTodoDto, found: CreateTodoDto");
        }
        public void TestCreateStatusBadRouteNameOk()
        {
            //SETUP
            var status       = new StatusGenericHandler();
            var controller   = new ToDoController();
            var dto          = new CreateTodoDto();
            var actionResult = status.Response(controller, "Bad", new { id = 7 }, dto);

            //ATTEMPT
            var statusCode = actionResult.GetStatusCode();
            var rStatus    = actionResult.CheckCreateResponse("Get", new { id = 7 }, dto);

            //VERIFY
            statusCode.ShouldEqual(CreateResponse.CreatedStatusCode);
            rStatus.IsValid.ShouldBeFalse(rStatus.GetAllErrors());
            rStatus.GetAllErrors().ShouldEqual("RouteName: expected Get, found: Bad");
        }
        public void TestCreateStatusOk()
        {
            //SETUP
            var status       = new StatusGenericHandler();
            var controller   = new ToDoController();
            var dto          = new CreateTodoDto();
            var actionResult = status.Response(controller, "Get", new { id = 7 }, dto);

            //ATTEMPT
            var statusCode = actionResult.GetStatusCode();
            var rStatus    = actionResult.CheckCreateResponse("Get", new { id = 7 }, dto);

            //VERIFY
            statusCode.ShouldEqual(CreateResponse.CreatedStatusCode);
            rStatus.IsValid.ShouldBeTrue(rStatus.GetAllErrors());
            rStatus.Message.ShouldEqual("Success");
        }
Exemplo n.º 18
0
        public void TestCreateTodoViaBizLogicWithErrorOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();

            using (var context = new ExampleDbContext(options))
            {
                context.Database.EnsureCreated();
                var service = new CreateTodoBizLogic(context);

                //ATTEMPT
                var dto = new CreateTodoDto
                {
                    Name       = "Test!",
                    Difficulty = 3
                };
                var result = service.BizAction(dto);

                //VERIFY
                service.HasErrors.ShouldBeTrue(service.GetAllErrors());
                service.GetAllErrors().ShouldEqual("Business logic says the name canot end with !");
            }
        }
Exemplo n.º 19
0
        public void TestCreateTodoViaBizLogicOk()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExampleDbContext>();

            using (var context = new ExampleDbContext(options))
            {
                context.Database.EnsureCreated();
                var service = new CreateTodoBizLogic(context);

                //ATTEMPT
                var dto = new CreateTodoDto
                {
                    Name       = "Test",
                    Difficulty = 3
                };
                var result = service.BizAction(dto);
                context.SaveChanges();

                //VERIFY
                service.HasErrors.ShouldBeFalse(service.GetAllErrors());
                context.TodoItems.Single().Name.ShouldEqual("Test");
            }
        }
Exemplo n.º 20
0
 public bool AddTodoList([FromServices] ITodoListService todoListService, [FromBody] CreateTodoDto todo)
 {
     return(todoListService.AddTodoList(todo));
 }
Exemplo n.º 21
0
 public CreateTodoAction(CreateTodoDto todo) =>
Exemplo n.º 22
0
        public async Task <ActionResult <IEnumerable <TodoDto> > > CreateTodo(string userId, [FromBody] CreateTodoDto createTodoDto)
        {
            _logger.LogInformation($"Entered {nameof(CreateTodo)}");
            try
            {
                var todos = await _userService.AddTodoAsync(createTodoDto, userId);

                return(Ok(todos));
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Error in {nameof(CreateTodo)}");
                return(StatusCode(500, "Server internal Error"));
            }
        }
Exemplo n.º 23
0
 public async Task <ToDo> CreateTask([FromBody] CreateTodoDto todoDto)
 {
     return(await _repository.CreateTodo(todoDto));
 }