Ejemplo n.º 1
0
        private static void SeedSales(CarDealerDbContext context, IMapper mapper)
        {
            if (!context.Customers.Any())
            {
                ImportCustomers(context, mapper);
            }
            if (!context.Cars.Any())
            {
                ImportCars(context, mapper);
            }
            var customers = context.Customers.Select(c => new { c.Id, c.IsYoungDriver }).ToArray();

            int[]          customerIds = customers.Select(c => c.Id).ToArray();
            int[]          carIds      = context.Cars.Select(c => c.Id).ToArray();
            decimal[]      discounts   = new decimal[] { 0M, 0.05M, 0.1M, 0.15M, 0.2M, 0.3M, 0.4M, 0.5M };
            Random         rng         = new Random();
            HashSet <Sale> sales       = context.Sales.ToHashSet();
            int            carsForSale = carIds.Length * 100 / 110;

            while (sales.Count < carsForSale)
            {
                int     customerId    = rng.Next(customerIds.Min(), customerIds.Max());
                var     customer      = customers.First(c => c.Id == customerId);
                int     carId         = rng.Next(carIds.Min(), carIds.Max());
                int     discountIndex = rng.Next(0, discounts.Length);
                decimal discount      = discounts[discountIndex];
                if (customer.IsYoungDriver)
                {
                    discount += 0.05M;
                }
                Sale sale = new Sale()
                {
                    Car_Id      = carId,
                    Customer_Id = customerId,
                    Discount    = discount
                };
                if (!sales.Any(s => s.Car_Id == sale.Car_Id))
                {
                    context.Sales.Add(sale);
                    sales.Add(sale);
                }
            }
            context.SaveChanges();
        }
Ejemplo n.º 2
0
        public static void ImportSuppliers(CarDealerDbContext context, string xmlSuplyers)
        {
            var serializer           = new XmlSerializer(typeof(SupplierDto[]), new XmlRootAttribute("suppliers"));
            var deserializedSupliers = (SupplierDto[])serializer.Deserialize(new StringReader(xmlSuplyers));
            var validSuppliers       = new List <Supplier>();

            foreach (var dto in deserializedSupliers)
            {
                if (!isValid(dto))
                {
                    continue;
                }

                var suplier = Mapper.Map <Supplier>(dto);
                validSuppliers.Add(suplier);
            }
            context.Suppliers.AddRange(validSuppliers);
            context.SaveChanges();
        }
Ejemplo n.º 3
0
        public static void ImportCustomers(CarDealerDbContext context, string xmlCustomer)
        {
            var serializer        = new XmlSerializer(typeof(CustomerDto[]), new XmlRootAttribute("customers"));
            var deserializedParts = (CustomerDto[])serializer.Deserialize(new StringReader(xmlCustomer));
            var validCustomers    = new List <Customer>();

            foreach (var dto in deserializedParts)
            {
                if (!isValid(dto))
                {
                    continue;
                }

                var customer = Mapper.Map <Customer>(dto);
                validCustomers.Add(customer);
            }
            context.Customers.AddRange(validCustomers);
            context.SaveChanges();
        }
Ejemplo n.º 4
0
        private static void SeedSales(CarDealerDbContext context)
        {
            if (!context.Customers.Any())
            {
                ImportCustomers(context);
            }
            if (!context.Cars.Any())
            {
                ImportCars(context);
            }
            var            customers   = context.Customers.Select(c => new { c.Id, c.IsYoungDriver }).ToHashSet();
            List <int>     customerIds = customers.Select(c => c.Id).ToList();
            List <int>     carIds      = context.Cars.Select(c => c.Id).ToList();
            List <decimal> discounts   = new List <decimal>()
            {
                0M, 0.05M, 0.1M, 0.15M, 0.2M, 0.3M, 0.4M, 0.5M
            };
            HashSet <Sale> sales            = context.Sales.ToHashSet();
            int            carsForSaleCount = carIds.Count * 100 / 110;
            Random         rng = new Random();

            while (sales.Count < carsForSaleCount)
            {
                int     carId      = carIds[rng.Next(0, carIds.Count)];
                int     customerId = customerIds[rng.Next(0, customerIds.Count)];
                var     customer   = customers.First(c => c.Id == customerId);
                decimal discount   = discounts[rng.Next(0, discounts.Count)];
                if (customer.IsYoungDriver)
                {
                    discount += 0.05M;
                }
                Sale sale = new Sale()
                {
                    Car_Id      = carId,
                    Customer_Id = customerId,
                    Discount    = discount
                };
                context.Sales.Add(sale);
                sales.Add(sale);
                carIds.Remove(carId);
            }
            context.SaveChanges();
        }
Ejemplo n.º 5
0
        public void EditUser(UserVM userVM)
        {
            CarDealerDbContext context = new CarDealerDbContext();
            var edit = context.Users.FirstOrDefault(u => u.Id == userVM.Id);

            if (edit != null)
            {
                edit.LastName  = userVM.LastName;
                edit.FirstName = userVM.FirstName;
                edit.Email     = userVM.Email;
                edit.Password  = userVM.Password;
                edit.Role      = userVM.Role;
                edit.UserName  = userVM.Email;
            }
            var userMgr = new UserManager <AppUser>(new UserStore <AppUser>(context));

            userMgr.AddToRole(edit.Id, edit.Role);
            context.SaveChanges();
        }
Ejemplo n.º 6
0
        private static List <Customer> ImportCustomers()
        {
            var jsonString = File.ReadAllText("../../../JSON/Inner/customers.json");

            var deserializedCustomers = JsonConvert.DeserializeObject <Customer[]>(jsonString);

            List <Customer> customers = new List <Customer>();

            foreach (var customer in deserializedCustomers)
            {
                customers.Add(customer);
            }

            var ctx = new CarDealerDbContext();

            ctx.Customers.AddRange(customers);
            ctx.SaveChanges();

            return(customers);
        }
Ejemplo n.º 7
0
        private static void ImportParts()
        {
            var context      = new CarDealerDbContext();
            var mapperConfig = new MapperConfiguration(cfg => { cfg.AddProfile <CarDealerProfile>(); });
            var mapper       = mapperConfig.CreateMapper();
            var xmlString    = File.ReadAllText("ImportXmls/parts.xml");
            var serializer   = new XmlSerializer(typeof(PartDto[]),
                                                 new XmlRootAttribute("parts"));

            var deserializeParts = (PartDto[])serializer.Deserialize(new StringReader(xmlString));


            var parts = new List <Part>();

            foreach (var deserializePart in deserializeParts)
            {
                if (!IsValid(deserializePart))
                {
                    continue;
                }
                //take random supplier
                var suppliers  = context.Suppliers.ToList();
                var supplierId = new Random().Next(suppliers.Count - 1);

                var supplier = suppliers.FirstOrDefault(id => id.Id == supplierId);

                suppliers.Remove(supplier);

                var partDto = new PartDto()
                {
                    Name       = deserializePart.Name,
                    Price      = deserializePart.Price,
                    Quantity   = deserializePart.Quantity,
                    SupplierId = supplierId
                };
                var part = mapper.Map <Part>(partDto);
                parts.Add(part);
            }
            context.AddRange(parts);
            context.SaveChanges();
        }
Ejemplo n.º 8
0
        public static void ImportRandomSales(CarDealerDbContext context)
        {
            var discounts     = new float[] { 0, 0.05f, 0.1f, 0.15f, 0.2f, 0.3f, 0.4f, 0.5f };
            var validSales    = new List <Sale>();
            var rnd           = new Random();
            var numberOfSales = rnd.Next(100, 242);

            var customers = context.Customers.ToArray();
            var cars      = context.Cars.ToArray();

            for (int i = 0; i <= numberOfSales; i++)
            {
                var discount   = discounts[rnd.Next(0, 8)];
                var carId      = rnd.Next(1, 243);
                var customerId = rnd.Next(1, 31);

                if (validSales.Any(x => x.CarId == carId))
                {
                    continue;
                }

                var isYoungDriver = customers
                                    .FirstOrDefault(x => x.Id == customerId)
                                    .IsYoungDriver == true;
                if (isYoungDriver)
                {
                    discount += 0.05f;
                }

                var sale = new Sale
                {
                    Discount   = discount,
                    CustomerId = customerId,
                    CarId      = carId
                };
                validSales.Add(sale);
            }

            context.Sales.AddRange(validSales);
            context.SaveChanges();
        }
        private static void ImportSuppliers(IMapper mapper)
        {
            var xmlString = File.ReadAllText("../../../XML/Inner/suppliers.xml");

            var serializer            = new XmlSerializer(typeof(SupplierDto[]), new XmlRootAttribute("suppliers"));
            var deserializedSuppliers = (SupplierDto[])serializer.Deserialize(new StringReader(xmlString));

            List <Supplier> suppliers = new List <Supplier>();

            foreach (var supplierDto in deserializedSuppliers)
            {
                var supplier = mapper.Map <Supplier>(supplierDto);

                suppliers.Add(supplier);
            }

            var ctx = new CarDealerDbContext();

            ctx.Suppliers.AddRange(suppliers);
            ctx.SaveChanges();
        }
Ejemplo n.º 10
0
 private static void ImportCars(CarDealerDbContext context)
 {
     if (!context.Parts.Any())
     {
         ImportParts(context);
     }
     using (StreamReader carsJSON = File.OpenText(@"..\..\..\Resources\cars.json"))
     {
         Car[]         cars         = JsonConvert.DeserializeObject <Car[]>(carsJSON.ReadToEnd());
         HashSet <Car> existingCars = context.Cars.ToHashSet();
         List <int>    partIds      = context.Parts.Select(p => p.Id).ToList();
         Random        rng          = new Random();
         foreach (Car car in cars)
         {
             if (IsObjectValid(car) && !existingCars.Any(c
                                                         => c.Make == car.Make && c.Model == car.Model &&
                                                         c.TravelledDistance == car.TravelledDistance))
             {
                 int carPartsCount = rng.Next(10, 21);
                 for (int i = 1; i <= carPartsCount; i++)
                 {
                     int partId = partIds[rng.Next(0, partIds.Count)];
                     while (car.CarParts.Any(p => p.Part_Id == partId))
                     {
                         partId = partIds[rng.Next(0, partIds.Count)];
                     }
                     PartCar carPart = new PartCar()
                     {
                         Part_Id = partId,
                         Car_Id  = car.Id
                     };
                     car.CarParts.Add(carPart);
                 }
                 context.Cars.Add(car);
                 existingCars.Add(car);
             }
         }
         context.SaveChanges();
     }
 }
Ejemplo n.º 11
0
        public static void ImportCars(CarDealerDbContext context, string xmlCars)
        {
            var serializer       = new XmlSerializer(typeof(CarDto[]), new XmlRootAttribute("cars"));
            var deserializedCars = (CarDto[])serializer.Deserialize(new StringReader(xmlCars));
            var validCars        = new List <Car>();
            var rnd = new Random();

            foreach (var dto in deserializedCars)
            {
                bool isValidInt = int.TryParse(dto.TravelledDistance, out int travelled);

                if (!isValid(dto) || !isValidInt)
                {
                    continue;
                }

                var car        = Mapper.Map <Car>(dto);
                var partsCount = rnd.Next(10, 21);
                for (int i = 0; i <= partsCount; i++)
                {
                    var partId  = rnd.Next(1, 132);
                    var partCar = new PartCar
                    {
                        Car    = car,
                        PartId = partId
                    };

                    if (car.PartCars.Any(x => x.PartId == partId))
                    {
                        continue;
                    }

                    car.PartCars.Add(partCar);
                }

                validCars.Add(car);
            }
            context.Cars.AddRange(validCars);
            context.SaveChanges();
        }
Ejemplo n.º 12
0
        public void Create(string make, string model, long travelledDistance, IEnumerable <int> partIds)
        {
            var car = new Car
            {
                Make              = make,
                Model             = model,
                TravelledDistance = travelledDistance
            };

            db.Cars.Add(car);

            foreach (var partId in partIds)
            {
                var partCar = new PartCar
                {
                    CarId  = car.Id,
                    PartId = partId
                };
                db.PartCars.Add(partCar);
            }
            db.SaveChanges();
        }
Ejemplo n.º 13
0
        public static void ImportParts(CarDealerDbContext context, string xmlParts)
        {
            var serializer        = new XmlSerializer(typeof(PartDto[]), new XmlRootAttribute("parts"));
            var deserializedParts = (PartDto[])serializer.Deserialize(new StringReader(xmlParts));
            var validParts        = new List <Part>();
            var rnd = new Random();

            foreach (var dto in deserializedParts)
            {
                if (!isValid(dto))
                {
                    continue;
                }

                var part       = Mapper.Map <Part>(dto);
                var supplierId = rnd.Next(1, 32);
                part.SupplierId = supplierId;
                validParts.Add(part);
            }
            context.Parts.AddRange(validParts);
            context.SaveChanges();
        }
Ejemplo n.º 14
0
        public void Create(string name, bool isImporter, IEnumerable <int> parts)
        {
            var existingParts = this.db.Parts
                                .Where(p => parts.Contains(p.Id))
                                .Select(p => p.Id)
                                .ToList();

            var supplier = new Supplier
            {
                Name       = name,
                IsImporter = isImporter
            };

            foreach (var partId in existingParts)
            {
                var part = this.db.Parts.Find(partId);
                supplier.Parts.Add(part);
            }

            db.Suppliers.Add(supplier);
            db.SaveChanges();
        }
Ejemplo n.º 15
0
        private static void ImportParts()
        {
            var jsonString = File.ReadAllText("../../../JSON/Inner/parts.json");

            var deserializedParts = JsonConvert.DeserializeObject <Part[]>(jsonString);

            List <Part> parts = new List <Part>();

            foreach (var part in deserializedParts)
            {
                int supplierId = new Random().Next(1, 32);

                part.SupplierId = supplierId;

                parts.Add(part);
            }

            var ctx = new CarDealerDbContext();

            ctx.Parts.AddRange(parts);
            ctx.SaveChanges();
        }
Ejemplo n.º 16
0
        private static void ImportCars(CarDealerDbContext db, IMapper mapper)
        {
            String        xmlString  = File.ReadAllText("cars.xml");
            XmlSerializer serializer = new XmlSerializer(typeof(ImportCarsDto[]), new XmlRootAttribute("cars"));

            ImportCarsDto[] deserializedParts = (ImportCarsDto[])serializer.Deserialize(new StringReader(xmlString));

            List <Car> listCars = new List <Car>();

            foreach (ImportCarsDto carsDto in deserializedParts)
            {
                if (!IsValid(carsDto))
                {
                    continue;
                }

                Car car = mapper.Map <Car>(carsDto);
                listCars.Add(car);
            }
            db.Cars.AddRange(listCars);
            db.SaveChanges();
        }
        private static List <Customer> ImportCustomers(IMapper mapper)
        {
            var xmlString = File.ReadAllText("../../../XML/Inner/customers.xml");

            var serializer            = new XmlSerializer(typeof(CustomerDto[]), new XmlRootAttribute("customers"));
            var deserializedCustomers = (CustomerDto[])serializer.Deserialize(new StringReader(xmlString));

            List <Customer> customers = new List <Customer>();

            foreach (var customerDto in deserializedCustomers)
            {
                var customer = mapper.Map <Customer>(customerDto);

                customers.Add(customer);
            }

            var ctx = new CarDealerDbContext();

            ctx.Customers.AddRange(customers);
            ctx.SaveChanges();

            return(customers);
        }
Ejemplo n.º 18
0
        public IActionResult Edit(Part p)
        {
            using (this.context)
            {
                Part part = this.context.Parts.Find(p.Id);

                part.Price    = p.Price;
                part.Quantity = p.Quantity;

                Logger logger = new Logger
                {
                    ModifiedTable = ModiefiedTable,
                    User          = User.Identity.Name,
                    Operation     = "Edit",
                    Time          = DateTime.Now
                };

                this.context.Loggers.Add(logger);
                context.SaveChanges();

                return(RedirectToAction(nameof(All)));
            }
        }
Ejemplo n.º 19
0
 private static void ImportSuppliers(CarDealerDbContext context, IMapper mapper)
 {
     using (var suppliersXml = new StreamReader(@"..\..\..\Resources\suppliers.xml"))
     {
         var serializer               = new XmlSerializer(typeof(SupplierDto[]), new XmlRootAttribute("suppliers"));
         var supplierDtos             = (SupplierDto[])serializer.Deserialize(suppliersXml);
         HashSet <Supplier> suppliers = context.Suppliers.ToHashSet();
         for (int i = 0; i < supplierDtos.Length; i++)
         {
             SupplierDto supplierDto = supplierDtos[i];
             if (IsObjectValid(supplierDto))
             {
                 Supplier supplier = mapper.Map <Supplier>(supplierDto);
                 if (!suppliers.Any(s => s.Name == supplier.Name))
                 {
                     context.Suppliers.Add(supplier);
                     suppliers.Add(supplier);
                 }
             }
         }
         context.SaveChanges();
     }
 }
Ejemplo n.º 20
0
 private static void ImportCustomers(CarDealerDbContext context, IMapper mapper)
 {
     using (var customersXml = new StreamReader(@"..\..\..\Resources\customers.xml"))
     {
         var serializer               = new XmlSerializer(typeof(CustomerDto[]), new XmlRootAttribute("customers"));
         var customerDtos             = (CustomerDto[])serializer.Deserialize(customersXml);
         HashSet <Customer> customers = context.Customers.ToHashSet();
         for (int i = 0; i < customerDtos.Length; i++)
         {
             CustomerDto customerDto = customerDtos[i];
             if (IsObjectValid(customerDto))
             {
                 Customer customer = mapper.Map <Customer>(customerDto);
                 if (!customers.Any(c => c.Name == customer.Name && c.BirthDate == customer.BirthDate))
                 {
                     context.Customers.Add(customer);
                     customers.Add(customer);
                 }
             }
         }
         context.SaveChanges();
     }
 }
Ejemplo n.º 21
0
 private static void ImportParts(CarDealerDbContext context)
 {
     if (!context.Suppliers.Any())
     {
         ImportSuppliers(context);
     }
     using (StreamReader partsJSON = File.OpenText(@"..\..\..\Resources\parts.json"))
     {
         Part[]         parts         = JsonConvert.DeserializeObject <Part[]>(partsJSON.ReadToEnd());
         HashSet <Part> existingParts = context.Parts.ToHashSet();
         List <int>     supplierIds   = context.Suppliers.Select(s => s.Id).ToList();
         Random         rng           = new Random();
         foreach (Part part in parts)
         {
             if (IsObjectValid(part) && !existingParts.Any(p => p.Name == part.Name))
             {
                 part.Supplier_Id = supplierIds[rng.Next(0, supplierIds.Count)];
                 context.Parts.Add(part);
                 existingParts.Add(part);
             }
         }
         context.SaveChanges();
     }
 }
Ejemplo n.º 22
0
        public static void ImportSuppliers(CarDealerDbContext context, IMapper mapper)
        {
            String        xmlString  = File.ReadAllText("suppliers.xml");
            XmlSerializer serializer = new XmlSerializer(typeof(SupplierImportDto[]), new XmlRootAttribute("suppliers"));

            SupplierImportDto[] deserializedCategories = (SupplierImportDto[])serializer.Deserialize(new StringReader(xmlString));

            List <Supplier> suppliersList = new List <Supplier>();

            foreach (SupplierImportDto entry in deserializedCategories)
            {
                if (!IsValid(entry))
                {
                    continue;
                }

                Supplier supplier = mapper.Map <Supplier>(entry);
                suppliersList.Add(supplier);
            }

            context.Supplier.AddRange(suppliersList);
            context.SaveChanges();
            Console.WriteLine();
        }