Exemplo n.º 1
0
        // GET: Home
        public ActionResult Index()
        {
            DogsDbContext context = new DogsDbContext();
            List <Dog>    items   = context.Dogs.ToList();

            return(View(items));
        }
Exemplo n.º 2
0
        public async Task GetDogReturnsDogsUsingValidId()
        {
            // Arrange
            var options = new DbContextOptionsBuilder <DogsDbContext>().UseInMemoryDatabase(nameof(GetDogReturnsDogsUsingValidId))
                          .Options;
            var dbContext = new DogsDbContext(options);

            CreateDogs(dbContext);

            var dogProfile    = new DogProfile();
            var configuration = new MapperConfiguration(config => config.AddProfile(dogProfile));
            var mapper        = new Mapper(configuration);

            var  sut   = new DogsProvider(dbContext, null, mapper);
            Guid dogId = Guid.Parse("005c68b6-a5e9-4fff-b7d9-c88ca27c39f1");

            // Act
            var dog = await sut.GetDogAsync(dogId);

            // Assert
            Assert.True(dog.IsSuccess);
            Assert.NotNull(dog.Dog);
            Assert.True(dog.Dog.Id == dogId);
            Assert.Null(dog.ErrorMessage);
        }
Exemplo n.º 3
0
        public DogsProvider(DogsDbContext dbContext, ILogger <DogsProvider> logger, IMapper mapper)
        {
            this.dbContext = dbContext;
            this.logger    = logger;
            this.mapper    = mapper;

            SeedData();
        }
Exemplo n.º 4
0
        public ActionResult Delete(int id)
        {
            DogsDbContext context = new DogsDbContext();
            Dog           item    = context.Dogs.Where(d => d.Id == id).FirstOrDefault();

            context.Dogs.Remove(item);
            context.SaveChanges();
            return(RedirectToAction("Index"));
        }
Exemplo n.º 5
0
        private void CreateDogs(DogsDbContext dbContext)
        {
            for (int i = 0; i < 10; i++)
            {
                dbContext.Dogs.Add(new Dog()
                {
                    Id   = i == 0 ? Guid.Parse("005c68b6-a5e9-4fff-b7d9-c88ca27c39f1") : Guid.NewGuid(),
                    Name = Guid.NewGuid().ToString()
                });
            }

            dbContext.SaveChanges();
        }
Exemplo n.º 6
0
        public ActionResult Edit(int?id)
        {
            DogsDbContext context = new DogsDbContext();
            Dog           item    = null;

            if (id == null)
            {
                item = new Dog();
            }
            else
            {
                item = context.Dogs.Where(d => d.Id == id.Value).FirstOrDefault();
            }
            return(View(item));
        }
Exemplo n.º 7
0
        public async Task GetDogsReturnsAllDogsAsync()
        {
            // Arrange
            var options = new DbContextOptionsBuilder <DogsDbContext>().UseInMemoryDatabase(nameof(GetDogsReturnsAllDogsAsync))
                          .Options;
            var dbContext = new DogsDbContext(options);

            CreateDogs(dbContext);

            var dogProfile    = new DogProfile();
            var configuration = new MapperConfiguration(config => config.AddProfile(dogProfile));
            var mapper        = new Mapper(configuration);

            var sut = new DogsProvider(dbContext, null, mapper);

            // Act
            var result = await sut.GetDogsAsync();

            // Assert
            Assert.True(result.IsSuccess);
            Assert.True(result.Dogs.Any());
            Assert.Null(result.ErrorMessage);
        }
Exemplo n.º 8
0
        public ActionResult Edit(Dog item)
        {
            DogsDbContext context = new DogsDbContext();

            if (item.Age > 100)
            {
                return(View(item));
            }
            int count = context.Dogs
                        .Where(d => d.Type == item.Type && d.Id != item.Id)
                        .Count();

            if (item.Id <= 0)
            {
                context.Dogs.Add(item);
            }
            else
            {
                context.Entry(item).State = System.Data.Entity.EntityState.Modified;
            }

            context.SaveChanges();
            return(RedirectToAction("Index"));
        }