Ejemplo n.º 1
0
        private static void ResetDatabase(ArtilleryContext context, bool shouldDropDatabase = false)
        {
            if (shouldDropDatabase)
            {
                context.Database.EnsureDeleted();
            }

            if (context.Database.EnsureCreated())
            {
                return;
            }

            var disableIntegrityChecksQuery = "EXEC sp_MSforeachtable @command1='ALTER TABLE ? NOCHECK CONSTRAINT ALL'";

            context.Database.ExecuteSqlCommand(disableIntegrityChecksQuery);

            var deleteRowsQuery = "EXEC sp_MSforeachtable @command1='SET QUOTED_IDENTIFIER ON;DELETE FROM ?'";

            context.Database.ExecuteSqlCommand(deleteRowsQuery);

            var enableIntegrityChecksQuery =
                "EXEC sp_MSforeachtable @command1='ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL'";

            context.Database.ExecuteSqlCommand(enableIntegrityChecksQuery);

            var reseedQuery =
                "EXEC sp_MSforeachtable @command1='IF OBJECT_ID(''?'') IN (SELECT OBJECT_ID FROM SYS.IDENTITY_COLUMNS) DBCC CHECKIDENT(''?'', RESEED, 0)'";

            context.Database.ExecuteSqlCommand(reseedQuery);
        }
Ejemplo n.º 2
0
        public static string ExportShells(ArtilleryContext context, double shellWeight)
        {
            var shells = context.Shells.ToArray()
                         .Where(s => s.ShellWeight > shellWeight)
                         .OrderBy(s => s.ShellWeight)
                         .Select(s => new
            {
                ShellWeight = s.ShellWeight,
                Caliber     = s.Caliber,
                Guns        = s.Guns
                              .Where(g => (int)g.GunType == 3)
                              .OrderByDescending(g => g.GunWeight)
                              .Select(g => new
                {
                    GunType      = g.GunType.ToString(),
                    GunWeight    = g.GunWeight,
                    BarrelLength = g.BarrelLength,
                    Range        = g.Range > 3000 ? "Long-range" : "Regular range"
                })
            })
                         .ToArray();

            string shellsJson = JsonConvert.SerializeObject(shells, Formatting.Indented);

            return(shellsJson);
        }
Ejemplo n.º 3
0
        public static string ExportShells(ArtilleryContext context, double shellWeight)
        {
            var shells = context
                         .Shells
                         .ToList()
                         .Where(s => s.ShellWeight > shellWeight)
                         .Select(s => new
            {
                s.ShellWeight,
                s.Caliber,
                Guns = s.Guns
                       .ToList()
                       .Where(g => g.GunType.ToString() == GunType.AntiAircraftGun.ToString())
                       .Select(g => new
                {
                    GunType = g.GunType.ToString(),
                    g.GunWeight,
                    g.BarrelLength,
                    Range = g.Range > 3000 ?
                            "Long-range" :
                            "Regular range",
                })
                       .ToList()
                       .OrderByDescending(g => g.GunWeight)
                       .ToList(),
            })
                         .ToList()
                         .OrderBy(s => s.ShellWeight)
                         .ToList();

            return(JsonConvert.SerializeObject(shells, Formatting.Indented));
        }
Ejemplo n.º 4
0
        public static string ImportCountries(ArtilleryContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(CountryImportDto[]), new XmlRootAttribute("Countries"));

            using StringReader stringReader = new StringReader(xmlString);

            CountryImportDto[] countryDtos = (CountryImportDto[])xmlSerializer.Deserialize(stringReader);

            List <Country> countries = new List <Country>();

            foreach (var countryDto in countryDtos)
            {
                if (!IsValid(countryDto))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                Country country = new Country()
                {
                    CountryName = countryDto.CountryName,
                    ArmySize    = countryDto.ArmySize
                };

                countries.Add(country);
                sb.AppendLine(string.Format(SuccessfulImportCountry, country.CountryName, country.ArmySize));
            }

            context.Countries.AddRange(countries);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Ejemplo n.º 5
0
        public static string ImportShells(ArtilleryContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ShellImportDto[]), new XmlRootAttribute("Shells"));

            using StringReader stringReader = new StringReader(xmlString);

            ShellImportDto[] shellDtos = (ShellImportDto[])xmlSerializer.Deserialize(stringReader);

            List <Shell> shells = new List <Shell>();

            foreach (var shellDto in shellDtos)
            {
                if (!IsValid(shellDto))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                Shell shell = new Shell()
                {
                    ShellWeight = shellDto.ShellWeight,
                    Caliber     = shellDto.Caliber
                };

                shells.Add(shell);
                sb.AppendLine(string.Format(SuccessfulImportShell, shell.Caliber, shell.ShellWeight));
            }

            context.Shells.AddRange(shells);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Ejemplo n.º 6
0
        private static void ExportEntities(ArtilleryContext context, string exportDir)
        {
            var exportShells = DataProcessor.Serializer.ExportShells(context, 100);

            Console.WriteLine(exportShells);
            File.WriteAllText(exportDir + "Actual Result - ExportShells.json", exportShells);

            var exportActors = DataProcessor.Serializer.ExportGuns(context, "Krupp");

            Console.WriteLine(exportActors);
            File.WriteAllText(exportDir + "Actual Result - ExportGuns.xml", exportActors);
        }
Ejemplo n.º 7
0
        public static string ImportGuns(ArtilleryContext context, string jsonString)
        {
            StringBuilder sb = new StringBuilder();

            GunImportDto[] gunDtos = JsonConvert.DeserializeObject <GunImportDto[]>(jsonString);

            List <Gun> guns = new List <Gun>();

            foreach (var gunDto in gunDtos)
            {
                if (!IsValid(gunDto))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                bool isValidGunType = Enum.TryParse(gunDto.GunType, out GunType gunType);

                if (!isValidGunType)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                Gun gun = new Gun()
                {
                    ManufacturerId = gunDto.ManufacturerId,
                    GunWeight      = gunDto.GunWeight,
                    BarrelLength   = gunDto.BarrelLength,
                    NumberBuild    = gunDto.NumberBuild,
                    Range          = gunDto.Range,
                    GunType        = gunType,
                    ShellId        = gunDto.ShellId,
                    CountriesGuns  = gunDto.Countries.Select(g => new CountryGun()
                    {
                        CountryId = g.Id
                    })
                                     .ToList()
                };

                guns.Add(gun);
                sb.AppendLine(string.Format(SuccessfulImportGun, gun.GunType.ToString(), gun.GunWeight, gun.BarrelLength));
            }

            context.AttachRange(guns);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Ejemplo n.º 8
0
        public static string ImportManufacturers(ArtilleryContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var serializer = new XmlSerializer(typeof(List <ImportManufacturersDto>), new XmlRootAttribute("Manufacturers"));

            var namespaces = new XmlSerializerNamespaces();

            namespaces.Add(string.Empty, string.Empty);

            var reader = new StringReader(xmlString);

            using (reader)
            {
                var manufacturersDtos = (List <ImportManufacturersDto>)serializer.Deserialize(reader);

                var manufacturersToAdd = new List <Manufacturer>();

                foreach (var manufacturerDto in manufacturersDtos)
                {
                    if (!IsValid(manufacturerDto) ||
                        manufacturersToAdd.Any(m => m.ManufacturerName == manufacturerDto.ManufacturerName))
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }

                    var founded = manufacturerDto
                                  .Founded
                                  .Split(", ")
                                  .TakeLast(2);

                    var manufacturer = new Manufacturer()
                    {
                        Founded          = string.Join(", ", founded),
                        ManufacturerName = manufacturerDto.ManufacturerName,
                    };

                    manufacturersToAdd.Add(manufacturer);
                    sb.AppendLine(string.Format(SuccessfulImportManufacturer, manufacturer.ManufacturerName, manufacturer.Founded));
                }

                context.AddRange(manufacturersToAdd);
                context.SaveChanges();

                return(sb.ToString().Trim());
            }
        }
Ejemplo n.º 9
0
        public static string ImportGuns(ArtilleryContext context, string jsonString)
        {
            var sb = new StringBuilder();

            var serializer = JsonConvert.DeserializeObject <List <ImportGunsDto> >(jsonString);

            var gunsToAdd = new List <Gun>();

            foreach (var gunDto in serializer)
            {
                if (!IsValid(gunDto) ||
                    !Enum.IsDefined(typeof(GunType), gunDto.GunType))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var gun = new Gun()
                {
                    Range          = gunDto.Range,
                    GunType        = Enum.Parse <GunType>(gunDto.GunType),
                    BarrelLength   = gunDto.BarrelLength,
                    GunWeight      = gunDto.GunWeight,
                    ManufacturerId = gunDto.ManufacturerId,
                    NumberBuild    = gunDto.NumberBuild,
                    ShellId        = gunDto.ShellId,
                };

                foreach (var countryGun in gunDto.Countries)
                {
                    gun.CountriesGuns.Add(new CountryGun()
                    {
                        CountryId = countryGun.Id,
                        GunId     = gun.Id,
                    });
                }

                gunsToAdd.Add(gun);
                sb.AppendLine(string.Format(SuccessfulImportGun, gun.GunType.ToString(), gun.GunWeight, gun.BarrelLength));
            }

            context.AddRange(gunsToAdd);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
Ejemplo n.º 10
0
        public static string ImportManufacturers(ArtilleryContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ManufacturerImportDto[]), new XmlRootAttribute("Manufacturers"));

            using StringReader stringReader = new StringReader(xmlString);

            ManufacturerImportDto[] manufacturerDtos = (ManufacturerImportDto[])xmlSerializer.Deserialize(stringReader);

            List <Manufacturer> manufacturers = new List <Manufacturer>();

            foreach (var manufacturerDto in manufacturerDtos.Distinct())
            {
                if (!IsValid(manufacturerDto))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                if (manufacturers.Any(m => m.ManufacturerName == manufacturerDto.ManufacturerName))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                Manufacturer manufacturer = new Manufacturer()
                {
                    ManufacturerName = manufacturerDto.ManufacturerName,
                    Founded          = manufacturerDto.Founded
                };

                manufacturers.Add(manufacturer);
                string[] manufacturerFounded = manufacturer.Founded.Split(", ", StringSplitOptions.RemoveEmptyEntries).ToArray();
                string[] manufacturerCountry = manufacturerFounded.TakeLast(2).ToArray();

                sb.AppendLine(string.Format(SuccessfulImportManufacturer, manufacturer.ManufacturerName,
                                            string.Join(", ", manufacturerCountry)));
            }

            context.Manufacturers.AddRange(manufacturers);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Ejemplo n.º 11
0
        public static void Main(string[] args)
        {
            var context = new ArtilleryContext();

            Mapper.Initialize(config => config.AddProfile <ArtilleryProfile>());

            ResetDatabase(context, shouldDropDatabase: true);

            var projectDir = GetProjectDirectory();

            ImportEntities(context, projectDir + @"Datasets/", projectDir + @"ImportResults/");

            ExportEntities(context, projectDir + @"ExportResults/");

            using (var transaction = context.Database.BeginTransaction())
            {
                transaction.Rollback();
            }
        }
Ejemplo n.º 12
0
        public static string ImportCountries(ArtilleryContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var serializer = new XmlSerializer(typeof(List <ImportCountriesDto>), new XmlRootAttribute("Countries"));

            var namespaces = new XmlSerializerNamespaces();

            namespaces.Add(string.Empty, string.Empty);

            var reader = new StringReader(xmlString);

            using (reader)
            {
                var countriesDtos = (List <ImportCountriesDto>)serializer.Deserialize(reader);

                var countriesToAdd = new List <Country>();

                foreach (var countryDto in countriesDtos)
                {
                    if (!IsValid(countryDto))
                    {
                        sb.AppendLine(ErrorMessage);

                        continue;
                    }

                    var country = new Country()
                    {
                        ArmySize    = countryDto.ArmySize,
                        CountryName = countryDto.CountryName,
                    };

                    countriesToAdd.Add(country);
                    sb.AppendLine(string.Format(SuccessfulImportCountry, country.CountryName, country.ArmySize));
                }

                context.AddRange(countriesToAdd);
                context.SaveChanges();

                return(sb.ToString().Trim());
            }
        }
Ejemplo n.º 13
0
        public static string ImportShells(ArtilleryContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var serializer = new XmlSerializer(typeof(List <ImportShellsDto>), new XmlRootAttribute("Shells"));

            var namespaces = new XmlSerializerNamespaces();

            namespaces.Add(string.Empty, string.Empty);

            var reader = new StringReader(xmlString);

            using (reader)
            {
                var shellsDtos = (List <ImportShellsDto>)serializer.Deserialize(reader);

                var shellsToAdd = new List <Shell>();

                foreach (var shellDto in shellsDtos)
                {
                    if (!IsValid(shellDto))
                    {
                        sb.AppendLine(ErrorMessage);
                        continue;
                    }

                    var shell = new Shell()
                    {
                        Caliber     = shellDto.Caliber,
                        ShellWeight = shellDto.ShellWeight,
                    };

                    shellsToAdd.Add(shell);
                    sb.AppendLine(string.Format(SuccessfulImportShell, shell.Caliber, shell.ShellWeight));
                }

                context.AddRange(shellsToAdd);
                context.SaveChanges();

                return(sb.ToString().Trim());
            }
        }
Ejemplo n.º 14
0
 public static string ExportGuns(ArtilleryContext context, string manufacturer)
 {
     var guns = context
                .Guns
                .Where(g => g.Manufacturer.ManufacturerName == manufacturer)
                .Select(g => new ExportGunsDto()
     {
         BarrelLength = g.BarrelLength,
         Manufacturer = g.Manufacturer.ManufacturerName,
         GunType      = g.GunType.ToString(),
         GunWeight    = g.GunWeight,
         Range        = g.Range,
         Countries    = g.CountriesGuns
                        .Where(cg => cg.Country.ArmySize > 4_500_000)
                        .Select(cg => new ExportGunsCountriesDto
         {
             ArmySize = cg.Country.ArmySize,
             Country  = cg.Country.CountryName,
         })
                        .OrderBy(c => c.ArmySize)
                        .ToList()
     })
Ejemplo n.º 15
0
        private static void ImportEntities(ArtilleryContext context, string baseDir, string exportDir)
        {
            var importCountries =
                DataProcessor.Deserializer.ImportCountries(context,
                                                           File.ReadAllText(baseDir + "countries.xml"));

            PrintAndExportEntityToFile(importCountries, exportDir + "Actual Result - ImportCountries.txt");

            var importManufacturers = DataProcessor.Deserializer.ImportManufacturers(context,
                                                                                     File.ReadAllText(baseDir + "manufacturers.xml"));

            PrintAndExportEntityToFile(importManufacturers, exportDir + "Actual Result - ImportMnufacturers.txt");

            var importShells = DataProcessor.Deserializer.ImportShells(context,
                                                                       File.ReadAllText(baseDir + "shells.xml"));

            PrintAndExportEntityToFile(importShells, exportDir + "Actual Result - ImportShells.txt");

            var importGuns =
                DataProcessor.Deserializer.ImportGuns(context,
                                                      File.ReadAllText(baseDir + "guns.json"));

            PrintAndExportEntityToFile(importGuns, exportDir + "Actual Result - ImportGuns.txt");
        }
Ejemplo n.º 16
0
        public static string ExportGuns(ArtilleryContext context, string manufacturer)
        {
            StringBuilder sb = new StringBuilder();

            using StringWriter stringWriter = new StringWriter(sb);

            XmlSerializer           xmlSerializer = new XmlSerializer(typeof(GunExportDto[]), new XmlRootAttribute("Guns"));
            XmlSerializerNamespaces namespaces    = new XmlSerializerNamespaces();

            namespaces.Add(string.Empty, string.Empty);

            var guns = context.Guns
                       .Where(g => g.Manufacturer.ManufacturerName == manufacturer)
                       .Select(g => new GunExportDto()
            {
                Manufacturer = g.Manufacturer.ManufacturerName,
                GunType      = g.GunType.ToString(),
                BarrelLength = g.BarrelLength,
                GunWeight    = g.GunWeight,
                Range        = g.Range,
                Countries    = g.CountriesGuns.Where(c => c.Country.ArmySize > 4500000)
                               .Select(c => new CountryDto()
                {
                    Country  = c.Country.CountryName,
                    ArmySize = c.Country.ArmySize
                })
                               .OrderBy(c => c.ArmySize)
                               .ToArray()
            })
                       .OrderBy(g => g.BarrelLength)
                       .ToArray();

            xmlSerializer.Serialize(stringWriter, guns, namespaces);

            return(sb.ToString().TrimEnd());
        }
Ejemplo n.º 17
0
 public PersonRepository(IConfiguration configuration)
 {
     _context = new ArtilleryContext(configuration);
 }