Пример #1
0
        public static string ImportOfficersPrisoners(SoftJailDbContext context, string xmlString)
        {
            var sb          = new StringBuilder();
            var xmlOfficers = XmlConverter.Deserializer <OfficersPrisonersXmlInputModel>(xmlString, "Officers");

            foreach (var xmlOfficer in xmlOfficers)
            {
                if (!IsValid(xmlOfficer))
                {
                    sb.AppendLine("Invalid Data");
                    continue;
                }

                var officer = new Officer
                {
                    FullName         = xmlOfficer.FullName,
                    Salary           = xmlOfficer.Money,
                    Position         = Enum.Parse <Position>(xmlOfficer.Position),
                    Weapon           = Enum.Parse <Weapon>(xmlOfficer.Weapon),
                    DepartmentId     = xmlOfficer.DepartmentId,
                    OfficerPrisoners = xmlOfficer.Prisoners.Select(x => new OfficerPrisoner
                    {
                        PrisonerId = x.Id
                    }).ToArray()
                };
                context.Officers.Add(officer);
                context.SaveChanges();
                sb.AppendLine($"Imported {officer.FullName} ({officer.OfficerPrisoners.Count} prisoners)");
            }

            return(sb.ToString().TrimEnd());
        }
Пример #2
0
        public static string ImportOfficersPrisoners(SoftJailDbContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            List <Officer> officers = new List <Officer>();

            var officersDtos = XmlConverter.Deserializer <OfficerPrisonerDTO>(xmlString, "Officers");

            foreach (var officerPrisonerDto in officersDtos)
            {
                if (!IsValid(officerPrisonerDto))
                {
                    sb.AppendLine("Invalid Data");
                    continue;
                }

                Officer officer = new Officer()
                {
                    FullName         = officerPrisonerDto.Name,
                    Salary           = officerPrisonerDto.Money,
                    DepartmentId     = officerPrisonerDto.DepartmentId,
                    Position         = Enum.Parse <Position>(officerPrisonerDto.Position),
                    Weapon           = Enum.Parse <Weapon>(officerPrisonerDto.Weapon),
                    OfficerPrisoners = officerPrisonerDto.Prisoners.Select(x => new OfficerPrisoner()
                    {
                        PrisonerId = x.Id
                    })
                                       .ToList()
                };

                //officers.Add(officer);

                context.Officers.Add(officer);
                context.SaveChanges();

                sb.AppendLine($"Imported {officer.FullName} ({officer.OfficerPrisoners.Count} prisoners)");
            }

            //context.Officers.AddRange(officers);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Пример #3
0
        public static string ImportOfficersPrisoners(SoftJailDbContext context, string xmlString)
        {
            var officers = XmlConverter.Deserializer <XMLOfficerPrisoner>(xmlString, "Officers");
            var sb       = new StringBuilder();

            foreach (var officerPrisonerXml in officers)
            {
                var enumPositionIsValid =
                    Enum.TryParse(typeof(Position), officerPrisonerXml.Position, out var position);

                var enumWeaponIsValid =
                    Enum.TryParse(typeof(Position), officerPrisonerXml.Position, out var weapon);

                if (IsValid(officerPrisonerXml) == false || enumPositionIsValid == false || position == null || enumWeaponIsValid == false || weapon == null)
                {
                    sb.AppendLine("Invalid Data");
                    continue;
                }

                var officer = new Officer
                {
                    FullName         = officerPrisonerXml.Name,
                    Salary           = officerPrisonerXml.Money,
                    Position         = (Position)position,
                    Weapon           = (Weapon)weapon,
                    DepartmentId     = officerPrisonerXml.DepartmentId,
                    OfficerPrisoners = officerPrisonerXml.Prisoners.Select(x => new OfficerPrisoner
                    {
                        PrisonerId = x.Id
                    }).ToList()
                };

                context.Officers.Add(officer);
                context.SaveChanges();

                sb.AppendLine($"Imported {officer.FullName} ({officer.OfficerPrisoners.Count} prisoners)");
            }

            return(sb.ToString().TrimEnd());
        }
Пример #4
0
        public static string ImportOfficersPrisoners(SoftJailDbContext context, string xmlString)
        {
            StringBuilder sb       = new StringBuilder();
            var           officers = new List <Officer>();

            var officersDto = XmlConverter.Deserializer <OfficerPrisonerImportModel>(xmlString, "Officers");

            foreach (var currentOfficer in officersDto)
            {
                if (!IsValid(currentOfficer) || !currentOfficer.Prisoners.All(x => IsValid(x)))
                {
                    sb.AppendLine("Invalid Data");
                    continue;
                }

                var officer = new Officer
                {
                    FullName         = currentOfficer.Name,
                    Salary           = currentOfficer.Money,
                    Position         = Enum.Parse <Position>(currentOfficer.Position),
                    Weapon           = Enum.Parse <Weapon>(currentOfficer.Weapon),
                    DepartmentId     = currentOfficer.DepartmentId,
                    OfficerPrisoners = currentOfficer.Prisoners.Select(x => new OfficerPrisoner
                    {
                        PrisonerId = x.Id
                    }).ToList()
                };

                officers.Add(officer);

                sb.AppendLine($"Imported {officer.FullName} ({officer.OfficerPrisoners.Count()} prisoners)");
            }

            context.Officers.AddRange(officers);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
        public static string ImportOfficersPrisoners(SoftJailDbContext context, string xmlString)
        {
            var officers = XmlConverter.Deserializer <OfficerXMLInputModel>(xmlString, "Officers");

            var sb = new StringBuilder();

            var officersList = new List <Officer>();

            foreach (var officer in officers)
            {
                if (!IsValid(officer))
                {
                    sb.AppendLine("Invalid Data");
                    continue;
                }

                var newOffcier = new Officer
                {
                    FullName         = officer.Name,
                    Salary           = officer.Money,
                    Position         = (Position)Enum.Parse(typeof(Position), officer.Position),
                    Weapon           = (Weapon)Enum.Parse(typeof(Weapon), officer.Weapon),
                    DepartmentId     = officer.DepartmentId,
                    OfficerPrisoners = officer.Prisoners.Select(x => new OfficerPrisoner
                    {
                        PrisonerId = x.Id,
                    }).ToList()
                };
                officersList.Add(newOffcier);
                sb.AppendLine($"Imported {officer.Name} ({officer.Prisoners.Length} prisoners)");
            }

            context.Officers.AddRange(officersList);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Пример #6
0
        public static string ImportOfficersPrisoners(SoftJailDbContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            ImportOfficerDto[] xmlOfficers = XmlConverter.Deserializer <ImportOfficerDto>(xmlString, "Officers");

            foreach (var xmlOfficer in xmlOfficers)
            {
                if (!IsValid(xmlOfficer))
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                Officer officer = new Officer
                {
                    FullName         = xmlOfficer.Name,
                    Salary           = xmlOfficer.Money,
                    Position         = Enum.Parse <Position>(xmlOfficer.Position),
                    Weapon           = Enum.Parse <Weapon>(xmlOfficer.Weapon),
                    DepartmentId     = xmlOfficer.DepartmentId,
                    OfficerPrisoners = xmlOfficer.Prisoners
                                       .Select(op => new OfficerPrisoner {
                        PrisonerId = op.PrisonerId
                    })
                                       .ToArray()
                };

                context.Officers.Add(officer);
                context.SaveChanges();
                sb.AppendLine(string.Format(IMPORTED_OFFICER,
                                            officer.FullName,
                                            officer.OfficerPrisoners.Count));
            }
            return(sb.ToString().TrimEnd());
        }
        public static string ImportOfficersPrisoners(SoftJailDbContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            var officersXml = XmlConverter.Deserializer <ImportOfficersPrisonersDto>(xmlString, "Officers");

            List <Officer> officers = new List <Officer>();

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

                object position;
                bool   isValidPosiotion = Enum.TryParse(typeof(Position), xmlOfficer.Position, out position);

                object weapon;
                bool   isValidweapon = Enum.TryParse(typeof(Weapon), xmlOfficer.Weapon, out weapon);

                if (!isValidPosiotion || !isValidweapon)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                Position officerPosition = (Position)position;
                Weapon   officerWeapon   = (Weapon)weapon;

                var officer = new Officer()
                {
                    FullName     = xmlOfficer.FullName,
                    Salary       = xmlOfficer.Salary,
                    Position     = officerPosition,
                    Weapon       = officerWeapon,
                    DepartmentId = xmlOfficer.DepartmentId,
                };

                List <OfficerPrisoner> officerPrisoners = new List <OfficerPrisoner>();

                foreach (var prisonerXml in xmlOfficer.Prisoners)
                {
                    var prisonerOfficer = new OfficerPrisoner()
                    {
                        Officer    = officer,
                        PrisonerId = prisonerXml.Id,
                    };

                    officerPrisoners.Add(prisonerOfficer);
                    officer.OfficerPrisoners.Add(prisonerOfficer);
                }

                officers.Add(officer);

                sb.AppendLine($"Imported {officer.FullName} ({officer.OfficerPrisoners.Count()} prisoners)");
            }

            context.Officers.AddRange(officers);

            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }