コード例 #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));
        }