public async Task <AddResult> Add(string title, string description) { //TODO: Add security input validation for title and description strings try { var item = new TodoItem { CreatedTimeStamp = DateTime.UtcNow, Description = description, IsCompleted = false, Title = title, Id = Guid.NewGuid() }; var success = await _todoRepository.AddItem(item); return(new AddResult { IsSuccess = success, NewItem = success ? item : null }); } catch (Exception e) { Logger.LogException(e, $"Exception: unable to add new item with title: {title} description: {description}"); return(new AddResult { IsSuccess = false }); } }
public async Task CanAddAndGetAllItems() { //Arange ITodoRepository repo = TodoRepository.Create(); var item1 = new TodoItem() { Name = "Item1", Description = "Hello World" }; var item2 = new TodoItem() { Name = "Item2", Description = "World Hello" }; //Act await repo.AddItem(item1); await repo.AddItem(item2); var todos = await repo.GetAllItems(); var todoArray = todos.ToArray(); var todo1 = todoArray.First(i => i.Name == item1.Name); var todo2 = todoArray.First(i => i.Name == item2.Name); await repo.Clear(); //Assert Assert.Equal(todoArray.Length, 2); Assert.Equal(todo1, item1); Assert.Equal(todo2, item2); }
public async Task CanAddThenUpdateAnItem() { //Arange ITodoRepository repo = TodoRepository.Create(); var item = new TodoItem() { Name = "My Item", Description = "Hello World" }; //Act item = await repo.AddItem(item); item.Description = "World Hello"; var retItem = await repo.UpdateItem(item); var todos = await repo.GetAllItems(); var todoArray = todos.ToArray(); await repo.Clear(); //Assert Assert.NotNull(todoArray); Assert.Equal(todoArray.Length, 1); Assert.Equal(todoArray[0], item); }
// POST api/items public IHttpActionResult Post([FromBody] TodoItemDto value) { if (!ValidatePost(value)) { return(BadRequest("Ensure item has listId, text and optionally done, notes.")); } return(Ok(ToDto(_todoRepository.AddItem(value.listId, value.text, value.done, value.notes)))); }
public TodoController(ITodoRepository serviceRepo) { _serviceRepo = serviceRepo; if (_serviceRepo.GetTodoList().Count() == 0) { _serviceRepo.AddItem(new TodoItem { Name = "Item1" }); } }
public async Task CanAddThenRemoveItems() { //Arange ITodoRepository repo = TodoRepository.Create(); var item1 = new TodoItem() { Name = "Item1", Description = "Hello World" }; var item2 = new TodoItem() { Name = "Item2", Description = "World Hello" }; //Act item1 = await repo.AddItem(item1); item2 = await repo.AddItem(item2); var todos = await repo.GetAllItems(); var todoArray = todos.ToArray(); var originalCount = todoArray.Length; await repo.RemoveItem(item1.ID); todos = await repo.GetAllItems(); todoArray = todos.ToArray(); var todo = await repo.GetItem(item2.ID); await repo.Clear(); //Assert Assert.NotNull(todoArray); Assert.Equal(originalCount, 2); Assert.Equal(todoArray.Length, 1); Assert.NotNull(todo); Assert.Equal(todo, item2); }
public IActionResult AddItem(long todoId, [FromBody] TodoItem item) { var todo = _todoRepository.Find(todoId); if (todo == null) { return(NotFound()); } item.TodoId = todoId; _todoRepository.AddItem(todoId, item); return(CreatedAtRoute("GetTodoItem", new { id = item.Id }, item)); }
public void Add_NewItem_ReturnsResultObject() { // Arrange A.CallTo(() => _repository.AddItem(A <TodoItem> ._)).Returns(true); // Act var result = _todoService.Add(_todoItem.Title, _todoItem.Description); // Assert result.IsSuccess.Should().BeTrue(); result.NewItem.Should().NotBeNull(); result.NewItem.Title.Should().Be(_todoItem.Title); result.NewItem.Description.Should().Be(_todoItem.Description); result.NewItem.CreatedTimeStamp.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromMilliseconds(100)); result.NewItem.Id.Should().NotBeEmpty(); result.NewItem.IsCompleted.Should().BeFalse(); }
public IActionResult AddItem(long todoId, [FromBody] ToDoItem item) { if (item == null || !item.CheckValidName()) { return(BadRequest()); } var todo = _todoRepository.Find(todoId); if (todo == null) { return(NotFound()); } item.TodoId = todoId; _todoRepository.AddItem(todoId, item); return(CreatedAtRoute("GetTodoItem", new { id = item.Id }, item)); }
public async Task CanGetItem() { //Arange ITodoRepository repo = TodoRepository.Create(); var item = new TodoItem() { Name = "Item1", Description = "Hello World" }; //Act item = await repo.AddItem(item); var todo = await repo.GetItem(item.ID); await repo.Clear(); //Assert Assert.NotNull(todo); Assert.Equal(todo, item); }
public IActionResult Post(TodoItem item) { _todoRepository.AddItem(item); return(NoContent()); }