// GET: Home public ActionResult Index() { DogsDbContext context = new DogsDbContext(); List <Dog> items = context.Dogs.ToList(); return(View(items)); }
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); }
public DogsProvider(DogsDbContext dbContext, ILogger <DogsProvider> logger, IMapper mapper) { this.dbContext = dbContext; this.logger = logger; this.mapper = mapper; SeedData(); }
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")); }
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(); }
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)); }
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); }
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")); }