public async Task <IActionResult> Post([FromBody] ToDo toDo) { if (toDo is null || !ModelState.IsValid) { return(BadRequest("Empty or invalid ToDo item body provided")); } // Ensure the specified todo list actually exists if (toDo.ListId.HasValue && !(await _context.ToDoList.AnyAsync(l => l.Id == toDo.ListId))) { return(StatusCode(StatusCodes.Status409Conflict, "Could not find a todo list with an Id that matches provided ListId")); } await _context.ToDo.AddAsync(toDo); try { await _context.SaveChangesAsync(); } catch { // TODO: Insert logging here return(StatusCode(StatusCodes.Status500InternalServerError, "Could not commit new ToDo entity to database")); } return(CreatedAtAction("GetToDo", new { toDo.Id }, toDo)); }
public async void CanGetLists() { DbContextOptions <ToDoNgDbContext> options = new DbContextOptionsBuilder <ToDoNgDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; using (ToDoNgDbContext context = new ToDoNgDbContext(options)) { // Arrange await context.ToDoList.AddRangeAsync( new ToDoList() { Name = "Hello", }, new ToDoList() { Name = "Goodbye", } ); await context.SaveChangesAsync(); ToDoListController controller = new ToDoListController(context); // Act OkObjectResult result = controller.GetAll() as OkObjectResult; DbSet <ToDoList> lists = result.Value as DbSet <ToDoList>; // Assert Assert.Equal(2, await lists.CountAsync()); } }
public async void CanGetItems() { DbContextOptions <ToDoNgDbContext> options = new DbContextOptionsBuilder <ToDoNgDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; using (ToDoNgDbContext context = new ToDoNgDbContext(options)) { // Arrange await context.ToDo.AddRangeAsync( new ToDo() { Message = "Hello", IsDone = true }, new ToDo() { Message = "Goodbye", IsDone = false } ); await context.SaveChangesAsync(); ToDoController controller = new ToDoController(context); // Act OkObjectResult result = controller.GetAll() as OkObjectResult; DbSet <ToDo> items = result.Value as DbSet <ToDo>; // Assert Assert.Equal(2, await items.CountAsync()); } }
public async Task <IActionResult> Post([FromBody] ToDoList list) { if (list is null || !ModelState.IsValid) { return(BadRequest("Empty or invalid ToDoList body provided")); } await _context.ToDoList.AddAsync(list); try { await _context.SaveChangesAsync(); } catch { // TODO: Insert logging here return(StatusCode(StatusCodes.Status500InternalServerError, "Could not commit new ToDoList entity to database")); } return(CreatedAtAction("GetToDoList", new { list.Id }, list)); }