Beispiel #1
0
        public void SeedDb(CarShopContext context)
        {
            context.Database.EnsureCreated();

            //User
            string password = "******";

            byte[] passwordHashAdmin, passwordSaltAdmin;
            _authHelper.CreatePasswordHash(password, out passwordHashAdmin, out passwordSaltAdmin);

            var user = context.Users.Add(new User
            {
                Username     = "******",
                PasswordHash = passwordHashAdmin,
                PasswordSalt = passwordSaltAdmin,
                IsAdmin      = true
            });

            //Car accessories
            var ca1 = new CarAccessory
            {
                AbsBrakes = true
            };

            var ca2 = new CarAccessory
            {
                AbsBrakes = false
            };

            var ca3 = new CarAccessory
            {
                AbsBrakes = true
            };

            //Car details
            var cd1 = new CarDetail
            {
                Doors = 5
            };

            var cd2 = new CarDetail
            {
                Doors = 3
            };

            var cd3 = new CarDetail
            {
                Doors = 5
            };

            //Car specs
            var cs1 = new CarSpec
            {
                Gear = 6
            };

            var cs2 = new CarSpec
            {
                Gear = 5
            };

            var cs3 = new CarSpec
            {
                Gear = 6
            };

            //Cars
            var c1 = new Car
            {
                Price          = 10,
                CarAccessories = ca1,
                CarDetails     = cd1,
                CarSpecs       = cs1,
                Sold           = false,
                Description    = "Pæne bil med meget plads. 2.4 i. 162 Hk. " +
                                 "Synsfri træk 1700 kg.Årg 4 / 5.2006. Km 231000." +
                                 "Synet 4 / 5.2018. Se reg nr XXC 52558.Tre nøgler. " +
                                 "Undervogns behandlet.Flot stue. Kører bare godt.AC / Klima.CD. " +
                                 "4 x El - ruder.Fart pilot.Sæde varme. 16 t alu med m / s dæk. " +
                                 "Med nr plader, Plus 600 kr til omg."
            };

            var c2 = new Car
            {
                Price          = 25,
                CarAccessories = ca2,
                CarDetails     = cd2,
                CarSpecs       = cs2,
                Sold           = false,
                Description    = "Kører bare godt. 1.6 HDI 1069 Hk. 6 Gear. " +
                                 "Nysynet 22 / 11.2019," +
                                 "Årg 24 / 9. 2009." +
                                 "Km 329000.Se reg nr BG 48737." +
                                 "Årlig vægt afgift 4000 kr.To nøgler." +
                                 "Synsfri træk 1000 kg.CD.AC.El - ruder." +
                                 "17 t alu med gode dæk." +
                                 "Service er ok." +
                                 "Med nr plader," +
                                 "Plus 600 kr til omg."
            };

            var c3 = new Car
            {
                Price          = 15,
                CarAccessories = ca3,
                CarDetails     = cd3,
                CarSpecs       = cs3,
                Sold           = false,
                Description    = "Pæn St.Car 2.0 i 115 Hk. Synet 1/10.2018." +
                                 "Km 355000.Se reg nr CA 31456." +
                                 "Årg 4 / 7.2000.En nøgle." +
                                 "Kører bare godt." +
                                 "4 x El - ruder.CD." +
                                 "Tandrem er skiftet ved 307000 km i 2017." +
                                 "Med nr plader," +
                                 "Plus 600 kr til omg."
            };

            context.Cars.AddRange(c1, c2, c3);
            context.CarDetails.AddRange(cd1, cd2, cd3);
            context.CarAccessories.AddRange(ca1, ca2, ca3);
            context.CarSpecs.AddRange(cs1, cs2, cs3);
            context.SaveChanges();
        }
        public void ValidateSpecValues(CarSpec carSpecs)
        {
            if (carSpecs == null)
            {
                throw new InvalidDataException("CarSpecs can't be null when trying to create a car");
            }

            if (carSpecs.Tonnage < 0)
            {
                throw new InvalidDataException("Tonnage has to be higher than or equal to zero");
            }

            if (carSpecs.Tank < 0)
            {
                throw new InvalidDataException("Tank has to be higher than or equal to zero");
            }

            if (carSpecs.NewPrice < 0)
            {
                throw new InvalidDataException("New price has to be higher than or equal to zero");
            }

            if (carSpecs.Width < 0)
            {
                throw new InvalidDataException("Width has to be higher than or equal to zero");
            }

            if (carSpecs.Valves < 0)
            {
                throw new InvalidDataException("Valves has to be higher than or equal to zero");
            }

            if (carSpecs.MaxWeight < 0)
            {
                throw new InvalidDataException("Max weight has to be higher than or equal to zero");
            }

            if (carSpecs.Length < 0)
            {
                throw new InvalidDataException("Length has to be higher than or equal to zero");
            }

            if (carSpecs.Cylinder < 0)
            {
                throw new InvalidDataException("Cylinder has to be higher than or equal to zero");
            }

            if (carSpecs.CostPrSixMonths < 0)
            {
                throw new InvalidDataException("Cost per six months has to be higher than or equal to zero");
            }

            if (carSpecs.Weight < 0)
            {
                throw new InvalidDataException("Weight has to be higher than or equal to zero");
            }

            if (carSpecs.MaxTrailerWeight < 0)
            {
                throw new InvalidDataException("Nax trailer weight has to be higher than or equal to zero");
            }
        }