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"); } }
public static Todo ToTodo(this CreateTodoDto dto) { return(new() { UserId = dto.UserId, Description = dto.Description, Title = dto.Title }); }
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)); }
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)); }
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); }
/// <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); }
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); }
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()); }
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>()); }
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; }
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"); }
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 !"); } }
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"); } }
public bool AddTodoList([FromServices] ITodoListService todoListService, [FromBody] CreateTodoDto todo) { return(todoListService.AddTodoList(todo)); }
public CreateTodoAction(CreateTodoDto todo) =>
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")); } }
public async Task <ToDo> CreateTask([FromBody] CreateTodoDto todoDto) { return(await _repository.CreateTodo(todoDto)); }