public async Task GetAsync_WithCorrectId_WorksCorrectly() { // Arrange const string testName = "TestName"; var manufacturer = new CarManufacturer { Name = testName }; var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options); context.CarManufacturers.AddRange( manufacturer, new CarManufacturer { Name = "OtherManufacturer1" }, new CarManufacturer { Name = "OtherManufacturer2" }); context.SaveChanges(); var manufacturersService = new ManufacturersService(new EfRepository <CarManufacturer>(context)); // Act var actualResult = (await manufacturersService.GetAsync(manufacturer.Id)).Name; // Assert Assert.Equal(testName, actualResult); }
public void Edit(CarManufactureViewModel carManufactureView) { CarManufacturer a = db.CarManufacturers.Find(carManufactureView.ID); a.Title = carManufactureView.Title; db.SaveChanges(); }
public async Task SeedAsync(ApplicationDbContext dbContext, IServiceProvider serviceProvider) { if (dbContext.CarModels.Any()) { return; } var audi = new CarManufacturer { Name = "Audi" }; var bmw = new CarManufacturer { Name = "BMW" }; var mercedes = new CarManufacturer { Name = "Mercedes" }; for (int i = 3; i <= 8; i++) { await dbContext.CarModels.AddAsync(new CarModel { Name = $"A{i}", Manufacturer = audi, }); await dbContext.CarModels.AddAsync(new CarModel { Name = $"{i} Series", Manufacturer = bmw, }); } await dbContext.CarModels.AddAsync(new CarModel { Name = $"A-Class", Manufacturer = mercedes, }); await dbContext.CarModels.AddAsync(new CarModel { Name = $"C-Class", Manufacturer = mercedes, }); await dbContext.CarModels.AddAsync(new CarModel { Name = $"E-Class", Manufacturer = mercedes, }); await dbContext.CarModels.AddAsync(new CarModel { Name = $"S-Class", Manufacturer = mercedes, }); }
public static bool ValidateCarModel(CarManufacturer manufacturer, string model) { if (!CarModels.TryGetValue(manufacturer, out var models)) { return(false); } return(models.Contains(model)); }
public CarModel(CarManufacturer carManufacturer, CarName carName, CarColor carColor, int carConstructionYear) { this.carType = CarType.Auto; this.carManufacturer = carManufacturer; this.carName = carName; this.carColor = carColor; this.CarConstructionYear = carConstructionYear; }
public long Insert(CarManufacturer entity) { entity.date_created = DateTime.Now; db.CarManufacturers.Add(entity); db.SaveChanges(); return(entity.id); }
public void Create(CarManufactureViewModel model) { var item = new CarManufacturer { Title = model.Title }; db.CarManufacturers.Add(item); db.SaveChanges(); }
//you need to call the parameterized base class constructor public Car(CarManufacturer manufacturer, string model) : base(Terrain.Land) { Manufacturer = manufacturer; if (!ValidateCarModel(manufacturer, model)) { throw new ArgumentException($"Manufacturer {manufacturer} does not make model: {model}"); } Model = model; }
public static IEnumerable <string> ModelsForManufacturer(CarManufacturer manufacturer) { if (!CarModels.TryGetValue(manufacturer, out var models)) { //maybe this should through, but this also makes sense return(new List <string>()); } //otherwise return(models); }
public async Task <bool> Delete(CarManufacturer entity) { try { _dbContext.CarManufacturers.Remove(entity); await _dbContext.SaveChangesAsync(); return(true); } catch (Exception e) { return(false); } }
public async Task DeleteAsync_WithCarModels_DoesNotDelete() { // Arrange const string testName = "TestName"; var manufacturer = new CarManufacturer { Name = testName, Models = new List <CarModel> { new CarModel { Model = "TestModel" } } }; var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options); context.CarManufacturers.AddRange( manufacturer, new CarManufacturer { Name = "OtherManufacturer1" }, new CarManufacturer { Name = "OtherManufacturer2" }); context.SaveChanges(); var manufacturersService = new ManufacturersService(new EfRepository <CarManufacturer>(context)); // Act var result = await manufacturersService.DeleteAsync(manufacturer.Id); // Assert Assert.False(result); var existsInDb = await context.CarManufacturers.AnyAsync(m => m.Name == testName); Assert.True(existsInDb); var dbCount = await context.CarManufacturers.CountAsync(); Assert.Equal(3, dbCount); }
public async Task <bool> Insert(CarManufacturer entity) { try { await _dbContext.CarManufacturers.AddAsync(entity); await _dbContext.SaveChangesAsync(); return(true); } catch (Exception e) { return(false); } }
public async Task GetAllByManufacturerAsync_WithData_WorksCorrectly() { // Arrange var expectedResults = new[] { "Model1", "Model2", "Model3" } .OrderBy(m => m) .ToArray(); var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options); var manufacturer = new CarManufacturer { Name = "TestManufacturer" }; foreach (var model in expectedResults) { context.CarModels.Add(new CarModel { Model = model, Manufacturer = manufacturer }); } context.CarModels.Add(new CarModel { Model = "OtherModel", Manufacturer = new CarManufacturer { Name = "OtherManufacturer" } }); context.SaveChanges(); var modelsService = new ModelsService(new EfRepository <CarModel>(context)); // Act var actualResults = (await modelsService.GetAllByManufacturerAsync(manufacturer.Id)) .Select(m => m.Model) .OrderBy(m => m) .ToArray(); // Assert Assert.Equal(expectedResults, actualResults); }
public async Task ExistsAsync_WithDifferentManufacturer_ReturnsFalse() { // Arrange const string testName = "TestName"; var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options); var manufacturer = new CarManufacturer { Name = "TestManufacturer" }; context.CarModels.AddRange( new CarModel { Model = testName, Manufacturer = new CarManufacturer { Name = "OtherManufacturer" } }, new CarModel { Model = "OtherModel", Manufacturer = manufacturer }); context.SaveChanges(); var modelsService = new ModelsService(new EfRepository <CarModel>(context)); var serviceModel = new CarModelServiceModel { Model = testName, ManufacturerId = manufacturer.Id }; // Act var result = await modelsService.ExistsAsync(serviceModel); // Assert Assert.False(result); }
public async Task UpdateAsync_WithInvalidModel_DoesNotChange() { // Arrange const string expectedResult = "InitialName"; var manufacturer = new CarManufacturer { Name = expectedResult }; var context = new PoolItDbContext(new DbContextOptionsBuilder <PoolItDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options); context.CarManufacturers.AddRange( manufacturer, new CarManufacturer { Name = "OtherManufacturer1" }, new CarManufacturer { Name = "OtherManufacturer2" }); context.SaveChanges(); var manufacturersService = new ManufacturersService(new EfRepository <CarManufacturer>(context)); var serviceModel = new CarManufacturerServiceModel { Id = manufacturer.Id, Name = "a" }; // Act var result = await manufacturersService.UpdateAsync(serviceModel); // Assert Assert.False(result); var actualResult = (await context.CarManufacturers.SingleAsync(m => m.Id == manufacturer.Id)).Name; Assert.Equal(expectedResult, actualResult); }
public async Task <CarManufacturerDTO> CreateManufacturerAsync(CarManufacturerCreate data) { var manufacturer = new CarManufacturer { Name = data.Name, Website = data.Website }; _db.CarManufacturers.Add(manufacturer); var saved = await _db.SaveChangesAsync(); if (saved <= 0) { throw new Exception("Problem saving data."); } return(MapToDTO(manufacturer)); }
public ActionResult Edit(CarManufacturer carManufacturer) { if (ModelState.IsValid) { var dao = new CarManufacturerDAO(); var result = dao.Update(carManufacturer); if (result) { return(RedirectToAction("Index", "CarManufacturerAdmin")); } else { ModelState.AddModelError("", "Cập nhật thất bại"); } } return(View("Index")); }
public ActionResult Create(CarManufacturer cm) { if (ModelState.IsValid) { var dao = new CarManufacturerDAO(); long id = dao.Insert(cm); if (id > 0) { return(RedirectToAction("Index", "CarManufacturerAdmin")); } else { ModelState.AddModelError("", "Thêm mới thất bại"); } } return(View()); }
public bool Update(CarManufacturer entity) { try { var cm = db.CarManufacturers.Find(entity.id); cm.name = entity.name; cm.thunbar = entity.thunbar; cm.date_update = DateTime.Now; db.SaveChanges(); return(true); } catch { return(false); } }
private CarManufacturerDTO MapToDTO(CarManufacturer manufacturer) { return(iMapper.Map <CarManufacturer, CarManufacturerDTO>(manufacturer)); }
public SuperCarModel(CarManufacturer carManufacturer, CarName carName, CarColor carColor, int carConstructionYear) : base(carManufacturer, carName, carColor, carConstructionYear) { base.carType = CarType.SportAuto; }
public async Task Seed() { if (!_context.CarModels.Any()) { var country = new Country { Id = new Guid(), Name = "Country" }; _context.Countries.Add(country); await _context.SaveChangesAsync(); var carManufacturer = new CarManufacturer { Id = new Guid(), Name = "Manufacturer", CountryId = country.Id }; _context.CarManufacturers.Add(carManufacturer); _context.SaveChanges(); var engine = new Engine { Id = new Guid(), Name = "Engine", PistonsCount = 4, Volume = 1.6, Power = 70, Torque = 120 }; _context.Engines.Add(engine); await _context.SaveChangesAsync(); var carComplectation = new CarComplectation { Id = new Guid(), Name = "Complectation", Price = 1140, DriveType = DriveType.FWD, Interior = "interior", PassengerCount = 4, LoadCapacity = 1200, EngineId = engine.Id }; _context.CarComplectations.Add(carComplectation); await _context.SaveChangesAsync(); var bodyType = new BodyType { Id = new Guid(), Name = "Body" }; _context.BodyTypes.Add(bodyType); await _context.SaveChangesAsync(); var carModel = new CarModel { Id = new Guid(), Name = "Car", CarManufacturerId = carManufacturer.Id, BodyTypeId = bodyType.Id, CarComplectationId = carComplectation.Id }; _context.CarModels.Add(carModel); await _context.SaveChangesAsync(); } }