public async Task Can_Create() { // Arrange await using var context = await GetInMemContext(); var entity = new Domain.Entities.TodoItem { Id = "1", Description = "", CreatedOn = DateTimeOffset.Now, CompletedOn = DateTimeOffset.Now, DueOn = DateTimeOffset.Now }; var sut = new TodoItemCommandDataAccess(context); // Act await sut.Create(entity, CancellationToken.None); await context.SaveChangesAsync(); // Assert var total = await context.TodoItems.Select(x => x.Id).CountAsync(); Assert.Equal(1, total); Assert.Equal(entity.Id, context.TodoItems.Single().Id); }
public async Task <IActionResult> Index(NewTodoModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } else { var todo = new Domain.Entities.TodoItem() { CreateTime = DateTimeOffset.Now, ModifyTime = DateTimeOffset.Now, IsComplete = false, Name = model.Name }; var step1 = new Domain.Entities.TodoItemStep() { Index = 1, Name = "步骤1" }; todo.AddStep(step1); await _todoItemRepository.AddAsync(todo); } return(RedirectToAction(actionName: nameof(Index))); }
/// <summary> /// Create a new todo item. /// </summary> /// <param name="entity">The todo item to create.</param> /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param> public async Task Create(Domain.Entities.TodoItem entity, CancellationToken cancellationToken) { await _dbContext.TodoItems.AddAsync(entity, cancellationToken); await _dbContext.SaveChangesAsync(cancellationToken); }