public ActionResult Index(IndexVM model) { PetsRepository repo = new PetsRepository(); model.Items = repo.GetAllItems(); return(View(model)); }
static void Main(string[] args) { PetValidator petValidator = new PetValidator(); string petsFilename = "..\\..\\..\\data\\pets.txt"; PetsRepository petsRepository = new PetsRepository(petValidator, petsFilename); CustomerValidator customerValidator = new CustomerValidator(); string customersFilename = "..\\..\\..\\data\\customers.txt"; CustomersRepository customersRepository = new CustomersRepository(customerValidator, customersFilename); ServiceValidator serviceValidator = new ServiceValidator(); string servicesFilename = "..\\..\\..\\data\\services.txt"; ServicesRepository servicesRepository = new ServicesRepository(serviceValidator, servicesFilename); VetValidator vetValidator = new VetValidator(); string vetsFilename = "..\\..\\..\\data\\vets.txt"; VetsRepository vetsRepository = new VetsRepository(vetValidator, vetsFilename); AppointmentValidator appointmentValidator = new AppointmentValidator(); string appointmentsFilename = "..\\..\\..\\data\\appointments.txt"; AppointmentsRepository appointmentsRepository = new AppointmentsRepository(appointmentValidator, appointmentsFilename); Controller controller = new Controller(petsRepository, customersRepository, servicesRepository, vetsRepository,appointmentsRepository); runApp(controller); }
public async Task NullReturnedWhenApiResponseIsOkAndDataIsArbitrary() { var repo = new PetsRepository(new HttpClient(new MockHttpHandler("null", HttpStatusCode.OK)), _mockApplicationSettings.Object); var results = await repo.GetAllOwnersAndPets().ConfigureAwait(false); results.Should().BeNull(); }
public PetsController(PetsRepository repository, IOptions <AppSettings> settings, ILogger <PetsController> logger) { _appSettings = settings.Value; _repository = repository; _logger = logger; _logger.LogInformation("PetsController"); }
public async void AddAsync_ShouldAddEmployee() { // Arrange var petToAdd = new Pet { Id = 1, Name = "Dick", Type = "duck", OwnerId = 23 }; var options = new DbContextOptionsBuilder <AppContext>() .UseInMemoryDatabase(databaseName: "Pets3") .Options; using (var appContext = new AppContext(options)) { var repo = new PetsRepository(appContext); // Act await repo.AddAsync(petToAdd); // Assert var pet = appContext.Pets.Find(petToAdd.Id); Assert.Equal(petToAdd.Id, pet.Id); Assert.Equal(petToAdd.Name, pet.Name); Assert.Equal(petToAdd.Type, pet.Type); Assert.Equal(petToAdd.OwnerId, pet.OwnerId); } }
public async Task NullReturnedWhenApiIsDown() { var repo = new PetsRepository(new HttpClient(new MockHttpHandler(string.Empty, HttpStatusCode.BadRequest)), _mockApplicationSettings.Object); var results = await repo.GetAllOwnersAndPets().ConfigureAwait(false); results.Should().BeNull(); }
public async void DeleteAsync_ShouldDeleteEmployee() { // Arrange var petToDelete = new Pet { Id = 1, Name = "Dick", Type = "duck", OwnerId = 23 }; var options = new DbContextOptionsBuilder <AppContext>() .UseInMemoryDatabase(databaseName: "Pets5") .Options; using (var appContext = new AppContext(options)) { await appContext.Pets.AddAsync(petToDelete); await appContext.SaveChangesAsync(); } using (var appContext = new AppContext(options)) { var repo = new PetsRepository(appContext); // Act await repo.DeleteAsync(petToDelete.Id); // Assert var pet = appContext.Employees.Find(petToDelete.Id); Assert.Null(pet); } }
public Controller(PetsRepository petsRepository, CustomersRepository customersRepository, ServicesRepository servicesRepository, VetsRepository vetRepository, AppointmentsRepository appointmentsRepository) { this.petsRepository = petsRepository; this.customersRepository = customersRepository; this.servicesRepository = servicesRepository; this.vetRepository = vetRepository; this.appointmentsRepository = appointmentsRepository; }
public void SetUp() { petsRepository = new PetsRepository(petValidator, petsFilename); customersRepository = new CustomersRepository(customerValidator, customersFilename); servicesRepository = new ServicesRepository(serviceValidator, servicesFilename); vetsRepository = new VetsRepository(vetValidator, vetsFilename); appointmentsRepository = new AppointmentsRepository(appointmentValidator, appointmentsFilename); }
public void GetPet_WithInMemoryData_CorrectPrice() { var petsRepository = new PetsRepository(); var petId = petsRepository.AddPet(new PetDto("Test", "Test description", 1000)); var pet = petsRepository.GetPet(petId); Assert.AreEqual(1000, pet.Price); }
public async Task ThrowExceptionWhenApiUrlInvalid() { var sut = new PetsRepository(); await Assert.ThrowsAsync <ArgumentNullException>(() => sut.GetPetOwnerAsync(null, null)); await Assert.ThrowsAsync <ArgumentNullException>(() => sut.GetPetOwnerAsync(null, "people.json")); await Assert.ThrowsAsync <UriFormatException>(() => sut.GetPetOwnerAsync(string.Empty, "people.json")); }
public virtual ActionResult Edit(EditVM model) { if (!ModelState.IsValid) { return(View(model)); } UnitOfWork uow = new UnitOfWork(); PetsRepository Repo = new PetsRepository(uow); VaccinesRepository V_repo = new VaccinesRepository(uow); Pet item = new Pet(); Vaccine vac = new Vaccine(); item.Vaccines = new List <Vaccine>(); model.PopulateEntity(item); item.Vaccines.Clear(); foreach (var v in model.Selected) { vac = V_repo.GetById(v.ID); if (v.IsChecked) { vac.Pet.Remove(item); item.Vaccines.Add(vac); vac.Pet.Add(item); } } //List<Vaccine> vacs = new List<Vaccine>(); /* foreach (var v in model.Selected) * { * if (v.IsChecked) * { * Vaccine sel = new Vaccine(); * sel = v_repo.GetById(v.ID); * vacs.Add(sel); * } * } * foreach (var v in vacs) * { * v_repo.Save(v); * }*/ Repo.Save(item); uow.Commit(); //return View(model); return(RedirectToAction("Index", "Pet")); }
public ActionResult Delete(int id) { PetsRepository repo = new PetsRepository(); Pet item = repo.GetById(id); if (AuthenticationManager.LoggedUser.Id != 1 && AuthenticationManager.LoggedUser.Id != item.UserId) { RedirectToAction("Login", "Home"); } else { repo.Delete(item); } return(RedirectToAction("Index", "Pet")); }
public async Task ThrowExceptionWhenApiUrlInvalid() { var client = new HttpClient() { BaseAddress = new Uri("http://agl-developer-test.azurewebsites.net/") }; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var sut = new PetsRepository(client); //sut.RequestUri = "people.json"; var petOwnersViewModel = await sut.GetPetOwnerAsync(); Assert.True(petOwnersViewModel.Exception == null); Assert.True(petOwnersViewModel.StatusCode != HttpStatusCode.OK); }
public async Task ValidatePetsFromOwnersInternalServerError() { var fakeHttpMessageHandler = new FakeHttpMessageHandler(new HttpResponseMessage() { StatusCode = HttpStatusCode.InternalServerError, Content = new StringContent(string.Empty) }); var client = new HttpClient(fakeHttpMessageHandler) { BaseAddress = new Uri("http://agl-developer-test.azurewebsites.net/") }; var sut = new PetsRepository(client); var petOwnersViewModel = await sut.GetPetOwnerAsync(); Assert.True(petOwnersViewModel.Exception == null); Assert.True(petOwnersViewModel.StatusCode == HttpStatusCode.InternalServerError); Assert.True(petOwnersViewModel.PetOwners != null && petOwnersViewModel.PetOwners.Count == 0); }
public async void GetByIdAsync_ShouldReturnEmployee() { // Arrange var pets = new List <Pet> { new Pet { Id = 121, Name = "Dick", Type = "duck", OwnerId = 23, Owner = new Employee { Id = 134 } }, new Pet { Id = 212, Name = "Mat", Type = "cat", OwnerId = 48, Owner = new Employee { Id = 12 } } }; var options = new DbContextOptionsBuilder <AppContext>() .UseInMemoryDatabase(databaseName: "Pets2") .Options; using (var appContext = new AppContext(options)) { await appContext.Pets.AddRangeAsync(pets); await appContext.SaveChangesAsync(); } using (var appContext = new AppContext(options)) { var repo = new PetsRepository(appContext); // Act var result = await repo.GetByIdAsync(pets.First().Id); // Assert Assert.IsAssignableFrom <Pet>(result); Assert.Equal(pets.First().Id, result.Id); } }
public async Task ResultsReturnedWhenApiIsUp() { var mockDataForGrouping = new List <Owner> { new Owner { Name = "Selina", Gender = "Female", Pets = new List <Pet> { new Pet { Type = "Cat", Name = "Garfield" }, new Pet { Type = "Cat", Name = "Abe" } } }, new Owner { Name = "Bruce", Gender = "Male", Pets = new List <Pet> { new Pet { Type = "Cat", Name = "Jasper" }, new Pet { Type = "Dog", Name = "Scooby" }, new Pet { Type = "Cat", Name = "Garry" } } } }; var mockJson = JsonConvert.SerializeObject(mockDataForGrouping); var repo = new PetsRepository(new HttpClient(new MockHttpHandler(mockJson)), _mockApplicationSettings.Object); var results = await repo.GetAllOwnersAndPets().ConfigureAwait(false); results.Should().NotBeNull(); }
public async void UpdateAsync_ShouldUpdateEmployee() { // Arrange var petBeforeUpdate = new Pet { Id = 1, Name = "Dick", Type = "duck", OwnerId = 23 }; var petAfterUpdate = new Pet { Id = 1, Name = "Mat", Type = "cat", OwnerId = 48 }; var options = new DbContextOptionsBuilder <AppContext>() .UseInMemoryDatabase(databaseName: "Pets4") .Options; using (var appContext = new AppContext(options)) { await appContext.Pets.AddAsync(petBeforeUpdate); await appContext.SaveChangesAsync(); } using (var appContext = new AppContext(options)) { var repo = new PetsRepository(appContext); // Act await repo.UpdateAsync(petAfterUpdate); // Assert var pet = appContext.Pets.Find(petAfterUpdate.Id); Assert.Equal(petAfterUpdate.Id, pet.Id); Assert.Equal(petAfterUpdate.Name, pet.Name); Assert.Equal(petAfterUpdate.Type, pet.Type); Assert.Equal(petAfterUpdate.OwnerId, pet.OwnerId); } }
public ActionResult Edit(int?id) { Pet item = null; PetsRepository repo = new PetsRepository(); VaccinesRepository v_repo = new VaccinesRepository(); item = id == null ? new Pet() : repo.GetById(id.Value); EditVM model = new EditVM(item); List <Vaccine> available = new List <Vaccine>(); List <CheckBoxItem> selected = new List <CheckBoxItem>(); available = v_repo.GetAllItems(); foreach (var v in available) { selected.Add(new CheckBoxItem() { ID = v.Id, Display = v.Name, IsChecked = item.ContainsV(v) }); } foreach (var v in selected) { model.Selected.Add(v); } if (AuthenticationManager.LoggedUser.Id != 1 && AuthenticationManager.LoggedUser.Id != model.User_Id) { return(RedirectToAction("Login", "Home")); } return(View(model)); }
public async void GetAllAsync_ShouldReturnListOfEmployees() { // Arrange var employees = new List <Pet> { new Pet { Id = 21, Name = "Dick", Type = "duck", OwnerId = 23 }, new Pet { Id = 12, Name = "Mat", Type = "cat", OwnerId = 48 } }; var options = new DbContextOptionsBuilder <AppContext>() .UseInMemoryDatabase(databaseName: "Pets1") .Options; using (var appContext = new AppContext(options)) { await appContext.Pets.AddRangeAsync(employees); await appContext.SaveChangesAsync(); } using (var appContext = new AppContext(options)) { var repo = new PetsRepository(appContext); // Act var result = await repo.GetAllAsync(); // Assert Assert.IsAssignableFrom <IEnumerable <Pet> >(result); Assert.Equal(employees.Count, result.Count()); } }
public PetsController(PetsRepository repository) { _repository = repository; }
/// <summary> /// /// </summary> /// <param name="repository"></param> public PetsController(PetsRepository repository)//构造函数依赖注入 { _repository = repository; }
public HomeController(PersonRepository personRepository, PetsRepository petsRepository) { _personRepository = personRepository; _petsRepository = petsRepository; }