예제 #1
0
        public static async Task <int> PopulateDatabaseAsync(IRepository todoRepository)
        {
            if ((await todoRepository.CountAsync(ToDoItemSpecs.All())) >= 5)
            {
                return(0);
            }

            todoRepository.Add(new ToDoItem
            {
                Title       = "Get Sample Working",
                Description = "Try to get the sample to build."
            });
            todoRepository.Add(new ToDoItem
            {
                Title       = "Review Solution",
                Description = "Review the different projects in the solution and how they relate to one another."
            });
            todoRepository.Add(new ToDoItem
            {
                Title       = "Run and Review Tests",
                Description = "Make sure all the tests run and review what they are doing."
            });

            return(await todoRepository.CountAsync(ToDoItemSpecs.All()));
        }
예제 #2
0
        public async Task UpdatesItemAfterAddingIt()
        {
            // add an item
            var repository   = GetRepository();
            var initialTitle = Guid.NewGuid().ToString();
            var item         = new ToDoItemBuilder().Title(initialTitle).Build();

            repository.Add(item);
            _dbContext.SaveChanges();

            // detach the item so we get a different instance
            _dbContext.Entry(item).State = EntityState.Detached;

            // fetch the item and update its title
            var newItem = await repository.FirstOrDefaultAsync(ToDoItemSpecs.ByTitle(initialTitle));

            Assert.NotNull(newItem);
            Assert.NotSame(item, newItem);
            var newTitle = Guid.NewGuid().ToString();

            newItem.Title = newTitle;

            // Update the item
            repository.Update(newItem);
            var updatedItem = await repository.FirstOrDefaultAsync(ToDoItemSpecs.ByTitle(initialTitle));

            Assert.NotNull(updatedItem);
            Assert.NotEqual(item.Title, updatedItem.Title);
            Assert.Equal(newItem.Id, updatedItem.Id);
        }
예제 #3
0
        public IActionResult List()
        {
            var items = _repository.List(ToDoItemSpecs.All())
                        .Select(ToDoItemDTO.FromToDoItem);

            return(Ok(items));
        }
예제 #4
0
        public async Task <IActionResult> Complete(int id)
        {
            var toDoItem = await _repository.FirstOrDefaultAsync(ToDoItemSpecs.ById(id));

            toDoItem.MarkComplete();
            _repository.Update(toDoItem);

            return(Ok(ToDoItemDTO.FromToDoItem(toDoItem)));
        }
예제 #5
0
        public async Task AddsItemAndSetsId()
        {
            var repository = GetRepository();
            var item       = new ToDoItemBuilder().Build();

            repository.Add(item);
            _dbContext.SaveChanges();

            var newItem = await repository.FirstOrDefaultAsync(ToDoItemSpecs.All());

            Assert.Equal(item, newItem);
            Assert.True(newItem?.Id > 0);
        }
예제 #6
0
        public void DeletesItemAfterAddingIt()
        {
            // add an item
            var repository   = GetRepository();
            var initialTitle = Guid.NewGuid().ToString();
            var item         = new ToDoItemBuilder().Title(initialTitle).Build();

            repository.Add(item);

            // delete the item
            repository.Delete(item);

            // verify it's no longer there
            Assert.DoesNotContain(repository.List(ToDoItemSpecs.ByTitle(initialTitle)),
                                  i => i.Title == initialTitle);
        }
예제 #7
0
 public void OnGet()
 {
     ToDoItems = _repository.List(ToDoItemSpecs.All()).ToList();
 }
예제 #8
0
        public async Task <IActionResult> GetById(int id)
        {
            var item = ToDoItemDTO.FromToDoItem(await _repository.FirstOrDefaultAsync(ToDoItemSpecs.ById(id)));

            return(Ok(item));
        }