public static string ImportOfficersPrisoners(SoftJailDbContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var validOfficers = new List <Officer>();

            var officerPrisoners = XmlConverter.Deserializer <OfficerPrisonerInputModel>(xmlString, "Officers");

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

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

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

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

            return(sb.ToString().TrimEnd());
        }
示例#2
0
        public static string ExportPrisonersInbox(SoftJailDbContext context, string prisonersNames)
        {
            var names = prisonersNames.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();

            var prisoners = context.Prisoners.Where(x => names.Contains(x.FullName))
                            .Select(p => new PrisonersExportXmlmodel
            {
                Id   = p.Id,
                Name = p.FullName,
                IncarcerationDate = p.IncarcerationDate.ToString("yyyy-MM-dd"),
                EncryptedMessages = p.Mails.Select(m => new MessagesExportXmlModel
                {
                    Description = string.Join("", m.Description.Reverse())
                }).ToArray()
            })
                            .OrderBy(x => x.Name)
                            .ThenBy(x => x.Id)
                            .ToList();

            var xml = XmlConverter.Serialize(prisoners, "Prisoners");

            return(xml);
        }
        public static string ExportPrisonersInbox(SoftJailDbContext context, string prisonersNames)
        {
            List <string> prisonerNames = prisonersNames.Split(",", StringSplitOptions.RemoveEmptyEntries).ToList();
            var           prisonersMail = context.Prisoners
                                          .Where(x => prisonerNames.Contains(x.FullName))
                                          .Select(x => new ExportPrisonerDto()
            {
                Id                = x.Id,
                FullName          = x.FullName,
                IncarcerationDate = x.IncarcerationDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
                EncryptedMessages = x.Mails.Select(y => new EncryptedMessagesDto()
                {
                    Description = MailDescriptionReverse(y.Description)
                })
                                    .ToArray(),
            })
                                          .OrderBy(x => x.FullName)
                                          .ThenBy(x => x.Id)
                                          .ToArray();

            var serializedEProject = XmlConverter.Serialize(prisonersMail, "Prisoners");

            return(serializedEProject);
        }
示例#4
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 ExportPrisonersInbox(SoftJailDbContext context, string prisonersNames)
        {
            var names     = prisonersNames.Split(',', StringSplitOptions.RemoveEmptyEntries);
            var prisoners = context.Prisoners
                            .ToList()
                            .Where(x => names.Contains(x.FullName))
                            .Select(x => new PrisonerXmlOutputModel
            {
                Id   = x.Id,
                Name = x.FullName,
                IncarcerationDate = x.IncarcerationDate.ToString("yyyy-MM-dd"),
                EncryptedMessages = x.Mails.Select(m => new EncryptedMailsModel()
                {
                    Description = Reverse(m.Description),
                }).ToArray(),
            })
                            .OrderBy(x => x.Name)
                            .ThenBy(x => x.IncarcerationDate)
                            .ToList();

            var result = XmlConverter.Serialize(prisoners, "Prisoners");

            return(result);
        }
示例#6
0
        public static string ExportPrisonersInbox(SoftJailDbContext context, string prisonersNames)
        {
            var prisoners = context.Prisoners
                            .Where(p => prisonersNames.Contains(p.FullName))
                            .Select(x => new ExPrisonerMailDto
            {
                Id   = x.Id,
                Name = x.FullName,
                IncarcerationDate = x.IncarcerationDate.ToString("yyyy-MM-dd"),
                Messages          = x.Mails.Select(x => new ExMessageDto
                {
                    Description = String.Join("", x.Description.Reverse())
                })
                                    .ToArray()
            })
                            .OrderBy(x => x.Name)
                            .ThenBy(x => x.Id)
                            .ToArray();

            var root   = "Prisoners";
            var result = XmlConverter.Serialize(prisoners, root);

            return(result);
        }
        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());
        }