public static string ImportCustomers(CarDealerContext context, string inputXml)
        {
            var xmlSerializer = XmlConverter.Deserializer <ImportCustomerDTO>(inputXml, "Customers");

            var customersDto = xmlSerializer.Select(c => new Customer
            {
                Name          = c.Name,
                BirthDate     = c.BirthDate,
                IsYoungDriver = c.IsYoungDriver
            }).ToList();

            context.Customers.AddRange(customersDto);
            context.SaveChanges();

            return($"Successfully imported {customersDto.Count}");
        }
示例#2
0
        //Problem 1
        public static string ImportSuppliers(CarDealerContext context, string inputXml)
        {
            var serializer   = new XmlSerializer(typeof(List <SupplierDto>), new XmlRootAttribute("Suppliers"));
            var suppliersDto = (List <SupplierDto>)serializer.Deserialize(new StringReader(inputXml));

            foreach (var supplierDto in suppliersDto)
            {
                var supplier = Mapper.Map <Supplier>(supplierDto);

                context.Suppliers.Add(supplier);
            }

            int count = context.SaveChanges();

            return($"Successfully imported {count}");
        }
示例#3
0
        public static string ImportParts(CarDealerContext context, string inputXml)
        {
            InitializeAutomaper();

            var rootName = "Parts";

            var partsDTO = XMLCustomSerializer.Deserialize <PartDTO[]>(inputXml, rootName);

            var parts = mapper.Map <Part[]>(partsDTO).Where(p => context.Suppliers.Any(s => s.Id == p.SupplierId)
                                                            ).ToArray();

            context.Parts.AddRange(parts);
            context.SaveChanges();

            return($"Successfully imported {parts.Length}");
        }
示例#4
0
        //Task 10
        public static string ImportParts(CarDealerContext context, string inputJson)
        {
            var validSupplierIds = context
                                   .Suppliers
                                   .Select(s => s.Id)
                                   .ToList();

            var parts = JsonConvert.DeserializeObject <List <Part> >(inputJson)
                        .Where(p => validSupplierIds.Contains(p.SupplierId))
                        .ToList();

            context.Parts.AddRange(parts);
            context.SaveChanges();

            return(String.Format(importResult, parts.Count));
        }
        public static string ImportCustomers(CarDealerContext context, string inputJson)
        {
            var customers = JsonConvert.DeserializeObject <List <Customer> >(inputJson, new JsonSerializerSettings
            {
                DateFormatString = "yyyy-MM-dd hh:mm:ss"
            });

            context.Customers.AddRange(customers);

            var addedEntries = context.SaveChanges();


            var result = $"Successfully imported {addedEntries}.";

            return(result);
        }
示例#6
0
        public static string ImportSales(CarDealerContext context, string inputJson)
        {
            InitializeMapper();

            var salesInputDTO = JsonConvert.DeserializeObject <IEnumerable <SaleInputDTO> >(inputJson);

            var salesToImport = mapper.Map <IEnumerable <Sale> >(salesInputDTO);

            ;

            context.Sales.AddRange(salesToImport);

            context.SaveChanges();

            return($"Successfully imported {salesToImport.Count()}.");
        }
示例#7
0
        //Query 12. Import Customers
        public static string ImportCustomers(CarDealerContext context, string inputXml)
        {
            var xmlSerializer =
                new XmlSerializer(typeof(ImportCustomerDto[]), new XmlRootAttribute("Customers"));


            var customerDto = ((ImportCustomerDto[])xmlSerializer.Deserialize(new StringReader(inputXml)));

            var customers = Mapper.Map <Customer[]>(customerDto);

            context.Customers.AddRange(customers);

            context.SaveChanges();

            return($"Successfully imported {customers.Length}");
        }
示例#8
0
        private static void ImportSuppliers(CarDealerContext ctx)
        {
            var xDocument = XDocument.Load(@"C:\Users\Bogdan Alov\documents\visual studio 2015\Projects\XML Processing\CarDealer\XML\suppliers.xml");
            var suppliers = xDocument.Root.Elements();

            foreach (var supplier in suppliers)
            {
                var newSupplier = new Supplier()
                {
                    Name       = supplier.Attribute("name").Value,
                    IsImported = Convert.ToBoolean(supplier.Attribute("is-importer").Value)
                };
                ctx.Suppliers.Add(newSupplier);
                ctx.SaveChanges();
            }
        }
示例#9
0
        // Problem 02 - Import Parts
        public static string ImportParts(CarDealerContext context, string inputJson)
        {
            var parts = JsonConvert.DeserializeObject <List <Part> >(inputJson)
                        .Where(p => Enumerable.Range(context.Suppliers
                                                     .Min(s => s.Id),
                                                     context.Suppliers
                                                     .Max(s => s.Id))
                               .Contains(p.SupplierId))
                        .ToList();

            context.AddRange(parts);

            context.SaveChanges();

            return($"Successfully imported {parts.Count}.");
        }
示例#10
0
        public static string ImportParts(CarDealerContext context, string inputJson) // Query 9. Import Parts
        {
            var suppliersId = context.Suppliers
                              .Select(x => x.Id)
                              .ToArray();

            var parts = JsonConvert
                        .DeserializeObject <IEnumerable <Part> >(inputJson)
                        .Where(s => suppliersId.Contains(s.SupplierId))
                        .ToList();

            context.Parts.AddRange(parts);
            context.SaveChanges();

            return($"Successfully imported {parts.Count}.");
        }
        public static string ImportSuppliers(CarDealerContext context, string inputJson)
        {
            var supplier = JsonConvert.DeserializeObject <IEnumerable <ImportSupplierInputModel> >(inputJson);

            var suppliers = supplier.Select(x => new Supplier
            {
                Name       = x.Name,
                IsImporter = x.IsImporter,
            })
                            .ToList();

            context.Suppliers.AddRange(suppliers);
            context.SaveChanges();

            return($"Successfully imported {suppliers.Count}.");
        }
示例#12
0
文件: StartUp.cs 项目: nkrastev/MSSQL
        public static string ImportSuppliers(CarDealerContext context, string inputXml)
        {
            var serializer            = new XmlSerializer(typeof(ImportSupplierDto[]), new XmlRootAttribute("Suppliers"));
            var textRead              = new StringReader(inputXml);
            var importSupplierDtoData = serializer.Deserialize(textRead) as ImportSupplierDto[];

            var suppliers = importSupplierDtoData.Select(x => new Supplier
            {
                Name       = x.Name,
                IsImporter = x.IsImporter,
            }).ToArray();

            context.Suppliers.AddRange(suppliers);
            context.SaveChanges();
            return($"Successfully imported {suppliers.Count()}");
        }
示例#13
0
        //10
        public static string ImportParts(CarDealerContext context, string inputJson)
        {
            int[] suppliersIds = context.Suppliers
                                 .Select(s => s.Id)
                                 .ToArray();

            Part[] parts = JsonConvert.DeserializeObject <Part[]>(inputJson)
                           .Where(p => suppliersIds.Contains(p.SupplierId))
                           .ToArray();

            context.Parts.AddRange(parts);

            var importedParts = context.SaveChanges();

            return(string.Format(successMessage, importedParts));
        }
示例#14
0
        public static string ImportSales(CarDealerContext context, string inputXml)
        {
            HashSet <int> carsIds = context.Cars.Select(x => x.Id).ToHashSet();

            var salesDto = XmlApplier
                           .Deserialize <DTOs.Import.SaleDTO>(inputXml, "Sales")
                           .Where(x => carsIds.Contains(x.CarId));

            var sales = MapperApplier
                        .MapCollection <DTOs.Import.SaleDTO, Sale>(salesDto);

            context.Sales.AddRange(sales);
            context.SaveChanges();

            return($"Successfully imported {sales.Length}");
        }
示例#15
0
        //12

        public static string ImportCustomers(CarDealerContext context, string inputJson)
        {
            var customersDto = JsonConvert.DeserializeObject <ICollection <CustomersImportDto> >(inputJson);

            var customers = customersDto.Select(c => new Customer
            {
                Name          = c.name,
                BirthDate     = c.birthDate,
                IsYoungDriver = c.isYoungDriver
            }).ToList();

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

            return($"Successfully imported {customers.Count}.");
        }
示例#16
0
        public static string DeserializeAddEntities <T>(
            CarDealerContext context,
            string inputJson,
            Predicate <T> predicate
            )
            where T : class, new()
        {
            var entities = JsonConvert
                           .DeserializeObject <T[]>(inputJson)
                           .Where(x => predicate(x))
                           .ToArray();

            context.AddRange(entities);
            context.SaveChanges();
            return($"Successfully imported {entities.Count()}.");
        }
示例#17
0
        //10

        public static string ImportParts(CarDealerContext context, string inputJson)
        {
            var mappConfig = new MapperConfiguration(cfg => cfg.AddProfile <CarDealerProfile>());
            var mapper     = mappConfig.CreateMapper();

            var partsDto = JsonConvert.DeserializeObject <ICollection <PartsImportDto> >(inputJson);
            var partsAll = mapper.Map <ICollection <Part> >(partsDto);

            var suppliersId = context.Suppliers.Select(s => s.Id).ToList();
            var parts       = partsAll.Where(p => suppliersId.Contains(p.SupplierId)).ToList();

            context.Parts.AddRange(parts);
            context.SaveChanges();

            return($"Successfully imported {parts.Count}.");
        }
示例#18
0
        public static string ImportSuppliers(CarDealerContext context, string inputXml)
        {
            var suppliersDto = XmlConverter.Deserializer <SupplierInputModel>(inputXml, "Suppliers");

            var suppliers = suppliersDto.Select(x => new Supplier
            {
                Name       = x.Name,
                IsImporter = x.IsImporter
            })
                            .ToList();

            context.AddRange(suppliers);
            context.SaveChanges();

            return($"Successfully imported {suppliers.Count}");
        }
示例#19
0
        public static string ImportCustomers(CarDealerContext context, string inputJson)
        {
            InitializeMapper();

            var customersDTO = JsonConvert.DeserializeObject <IEnumerable <CustomerInputDTO> >(inputJson);

            var customerToImport = mapper.Map <IEnumerable <Customer> >(customersDTO);



            context.Customers.AddRange(customerToImport);

            context.SaveChanges();

            return($"Successfully imported {customerToImport.Count()}.");
        }
示例#20
0
        public static string ImportSuppliers(CarDealerContext context, string inputXml)
        {
            var serializer = new XmlSerializer(typeof(List <ImportSupplierDto>), new XmlRootAttribute("Suppliers"));

            var suppliersDtos = (List <ImportSupplierDto>)serializer.Deserialize(new StringReader(inputXml));

            var suppliers = new List <Supplier>();

            suppliersDtos.ForEach(x => suppliers.Add(Mapper.Map <Supplier>(x)));

            context.AddRange(suppliers);

            var count = context.SaveChanges();

            return(string.Format(Result, count));
        }
示例#21
0
        //09. Import Suppliers
        public static string ImportSuppliers(CarDealerContext context, string inputXml)
        {
            var suppliersDtos = XMLConverter.Deserializer <ImportSupplierDto>(inputXml, "Suppliers");

            var result = suppliersDtos.Select(s => new Supplier
            {
                Name       = s.Name,
                IsImporter = s.IsImporter
            })
                         .ToArray();

            context.Suppliers.AddRange(result);
            context.SaveChanges();

            return($"Successfully imported {suppliersDtos.Length}");
        }
示例#22
0
文件: StartUp.cs 项目: Burckz/EF
        public static string ImportSales(CarDealerContext context, string inputXml)
        {
            var serializer = new XmlSerializer(typeof(Sale[]), new XmlRootAttribute("Sales"));

            var sales = (Sale[])serializer.Deserialize(new StringReader(inputXml));

            foreach (var sale in sales)
            {
                if (context.Cars.Any(c => c.Id == sale.CarId))
                {
                    context.Sales.Add(sale);
                }
            }

            return($"Successfully imported {context.SaveChanges()}");
        }
示例#23
0
        //Problem 9
        public static string ImportParts(CarDealerContext context, string inputJson)
        {
            Part[] parts = JsonConvert.DeserializeObject <Part[]>(inputJson)
                           .ToArray();

            int[] suppliers = context.Suppliers.Select(s => s.Id).ToArray();

            parts = parts.Where(part => suppliers.Any(supplier => supplier == part.SupplierId))
                    .ToArray();

            context.Parts.AddRange(parts);

            context.SaveChanges();

            return($"Successfully imported {parts.Length}.");
        }
示例#24
0
文件: StartUp.cs 项目: Burckz/EF
        public static string ImportParts(CarDealerContext context, string inputXml)
        {
            var serializer = new XmlSerializer(typeof(Part[]), new XmlRootAttribute("Parts"));

            var parts = (Part[])serializer.Deserialize(new StringReader(inputXml));

            foreach (var part in parts)
            {
                if (context.Suppliers.Any(s => s.Id == part.SupplierId))
                {
                    context.Parts.Add(part);
                }
            }

            return($"Successfully imported {context.SaveChanges()}");
        }
        public static string ImportParts(CarDealerContext context, string inputJson)
        {
            var parts = JsonConvert.DeserializeObject <List <Part> >(inputJson);

            var suppliersIds = context.Suppliers.Select(x => x.Id).ToList();

            var partsToAdd = parts.Where(x => suppliersIds.Contains(x.SupplierId));

            context.AddRange(partsToAdd);

            var partsCount = context.SaveChanges();

            var result = $"Successfully imported {partsCount}.";

            return(result);
        }
        //Problem 10 - Import Parts
        public static string ImportParts(CarDealerContext context, string inputXml)
        {
            var xmlSerializer = new XmlSerializer(typeof(PartDto[]),
                                                  new XmlRootAttribute("Parts"));

            var partDtos = (PartDto[])(xmlSerializer.Deserialize(new StringReader(inputXml)));
            var parts    = Mapper.Map <IEnumerable <Part> >(partDtos);

            var supplierIds = context.Suppliers.Select(x => x.Id).ToHashSet();
            var validParts  = parts.Where(p => supplierIds.Contains(p.SupplierId));

            context.Parts.AddRange(validParts);
            int count = context.SaveChanges();

            return($"Successfully imported {count}");
        }
示例#27
0
        public static string ImportParts(CarDealerContext context, string inputXml)
        {
            var xmlSerializer = new XmlSerializer(typeof(ImportPartDto[]),
                                                  new XmlRootAttribute("Parts"));

            var partsDto = ((ImportPartDto[])xmlSerializer.Deserialize(new StringReader(inputXml)))
                           .Where(p => context.Suppliers.Any(s => s.Id == p.SupplierId))
                           .ToArray();

            var parts = Mapper.Map <Part[]>(partsDto);

            context.Parts.AddRange(parts);
            context.SaveChanges();

            return($"Successfully imported {parts.Length}");
        }
示例#28
0
        //13

        public static string ImportSales(CarDealerContext context, string inputJson)
        {
            var salesDto = JsonConvert.DeserializeObject <ICollection <SalesImportDto> >(inputJson);

            var sales = salesDto.Select(s => new Sale
            {
                CarId      = s.carId,
                CustomerId = s.customerId,
                Discount   = s.discount
            }).ToList();

            context.Sales.AddRange(sales);
            context.SaveChanges();

            return($"Successfully imported {sales.Count}.");
        }
示例#29
0
        // 10. Import Parts
        public static string ImportParts(CarDealerContext context, string inputJson)
        {
            var partsDto = JsonConvert.DeserializeObject <IEnumerable <PartsImportModel> >(inputJson);

            var suppliersIds = context.Suppliers.Select(s => s.Id).ToList();

            var parts = partsDto
                        .Where(x => suppliersIds.Contains(x.SupplierId))
                        .Select(x => new Part())
                        .ToList();

            context.Parts.AddRange(parts);
            context.SaveChanges();

            return($"Successfully imported {parts.Count}.");
        }
示例#30
0
        public static string ImportCustomers(CarDealerContext context, string inputJson)
        {
            var customersDto = JsonConvert.DeserializeObject <IEnumerable <ImportCustomersModel> >(inputJson);
            var customers    = customersDto.Select(x => new Customer
            {
                Name          = x.Name,
                BirthDate     = x.BirthDate,
                IsYoungDriver = x.IsYoungerDriver
            })
                               .ToList();

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

            return($"Successfully imported {customers.Count}.");
        }