예제 #1
0
        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);
        }
예제 #2
0
        public void Edit(CarManufactureViewModel carManufactureView)
        {
            CarManufacturer a = db.CarManufacturers.Find(carManufactureView.ID);

            a.Title = carManufactureView.Title;
            db.SaveChanges();
        }
예제 #3
0
        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));
 }
예제 #5
0
 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;
 }
예제 #6
0
        public long Insert(CarManufacturer entity)
        {
            entity.date_created = DateTime.Now;

            db.CarManufacturers.Add(entity);
            db.SaveChanges();

            return(entity.id);
        }
예제 #7
0
        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);
 }
예제 #10
0
        public async Task <bool> Delete(CarManufacturer entity)
        {
            try
            {
                _dbContext.CarManufacturers.Remove(entity);
                await _dbContext.SaveChangesAsync();

                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
예제 #11
0
        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);
        }
예제 #12
0
        public async Task <bool> Insert(CarManufacturer entity)
        {
            try
            {
                await _dbContext.CarManufacturers.AddAsync(entity);

                await _dbContext.SaveChangesAsync();

                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
예제 #13
0
        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);
        }
예제 #14
0
        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);
        }
예제 #15
0
        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));
        }
예제 #17
0
        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"));
        }
예제 #18
0
        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());
        }
예제 #19
0
        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));
 }
예제 #21
0
 public SuperCarModel(CarManufacturer carManufacturer,
                      CarName carName, CarColor carColor, int carConstructionYear)
     : base(carManufacturer, carName, carColor, carConstructionYear)
 {
     base.carType = CarType.SportAuto;
 }
예제 #22
0
        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();
            }
        }