Esempio n. 1
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var serializer      = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures"));
            var deserializedXml = (ProcedureDto[])serializer.Deserialize(new StringReader(xmlString));

            var validProcedures = new List <Procedure>();

            foreach (var procedureDto in deserializedXml)
            {
                var vetObj    = context.Vets.SingleOrDefault(x => x.Name == procedureDto.Vet);
                var animalObj = context.Animals.SingleOrDefault(a => a.PassportSerialNumber == procedureDto.Animal);

                var validProcedureAnimalAids = new List <ProcedureAnimalAid>();

                var allAidsExists = true;

                foreach (var procedureDtoAnimalAid in procedureDto.AnimalAids)
                {
                    var animalAid = context.AnimalAids
                                    .SingleOrDefault(ai => ai.Name == procedureDtoAnimalAid.Name);
                    if (animalAid == null || validProcedureAnimalAids.Any(p => p.AnimalAid.Name == procedureDtoAnimalAid.Name))
                    {
                        allAidsExists = false;
                        break;
                    }

                    var animalAidProcedure = new ProcedureAnimalAid()
                    {
                        AnimalAid = animalAid
                    };

                    validProcedureAnimalAids.Add(animalAidProcedure);
                }

                if (!IsValid(procedureDto) || !procedureDto.AnimalAids.All(IsValid) ||
                    vetObj == null || animalObj == null || !allAidsExists)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var procedure = new Procedure()
                {
                    Animal              = animalObj,
                    Vet                 = vetObj,
                    DateTime            = DateTime.ParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture),
                    ProcedureAnimalAids = validProcedureAnimalAids
                };
                validProcedures.Add(procedure);
                sb.AppendLine("Record successfully imported.");
            }
            context.Procedures.AddRange(validProcedures);
            context.SaveChanges();

            var result = sb.ToString();

            return(result);
        }
Esempio n. 2
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var serializer = new XmlSerializer(typeof(ImportProcedureDTO[]), new XmlRootAttribute("Procedures"));

            var proceduresDTO = (ImportProcedureDTO[])serializer.Deserialize(new StringReader(xmlString));

            var sb = new StringBuilder();

            var procedures = new List <Procedure>();

            var vets       = context.Vets.ToHashSet();
            var animals    = context.Animals.ToHashSet();
            var animalAids = context.AnimalAids.ToHashSet();

            foreach (var procedureDTO in proceduresDTO)
            {
                var vet    = vets.FirstOrDefault(x => x.Name == procedureDTO.VetName);
                var animal = animals.FirstOrDefault(x => x.PassportSerialNumber == procedureDTO.AnimalSerialNumber);

                bool animalAidsIsValid      = procedureDTO.AnimalAids.Any(x => animalAids.Any(y => x.Name == y.Name));
                bool animalAidsCountIsValid = procedureDTO.AnimalAids.Count == procedureDTO.AnimalAids.Select(x => x.Name).Distinct().Count();

                bool procedureIsValid = (vet != null) && (animal != null) && animalAidsCountIsValid && animalAidsIsValid;

                if (!IsValid(procedureDTO) || !procedureDTO.AnimalAids.All(IsValid) || !procedureIsValid)
                {
                    sb.AppendLine($"Error: Invalid data.");
                    continue;
                }

                var currentProcedureAnimalAids = new List <ProcedureAnimalAid>();
                foreach (var animalAidsDTO in procedureDTO.AnimalAids)
                {
                    var animalAid          = animalAids.FirstOrDefault(x => x.Name == animalAidsDTO.Name);
                    var procedureAnimalAid = new ProcedureAnimalAid
                    {
                        AnimalAid = animalAid
                    };

                    currentProcedureAnimalAids.Add(procedureAnimalAid);
                }

                var procedure = new Procedure
                {
                    Animal = animal,
                    Vet    = vet,
                    ProcedureAnimalAids = currentProcedureAnimalAids,
                    DateTime            = DateTime.ParseExact(procedureDTO.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture)
                };

                procedures.Add(procedure);
                sb.AppendLine($"Record successfully imported.");
            }

            context.Procedures.AddRange(procedures);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var serializer             = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures"));
            var deserializedProcedures = (ProcedureDto[])serializer.Deserialize(new StringReader(xmlString));

            List <Procedure>          procedures          = new List <Procedure>();
            List <ProcedureAnimalAid> procedureAnimalAids = new List <ProcedureAnimalAid>();

            var sb = new StringBuilder();

            foreach (var proc in deserializedProcedures)
            {
                var vet    = context.Vets.SingleOrDefault(v => v.Name == proc.VetName);
                var animal = context.Animals.SingleOrDefault(a => a.PassportSerialNumber == proc.AnimalSerialNumber);

                if (vet == null ||
                    animal == null ||
                    proc.AnimalAids.Length != proc.AnimalAids.Distinct().Count() ||
                    context.AnimalAids.Where(x => proc.AnimalAids.Any(p => p.Name == x.Name)).Count() != proc.AnimalAids.Count())
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var procedure = new Procedure()
                {
                    Vet      = vet,
                    Animal   = animal,
                    DateTime = DateTime.ParseExact(proc.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture)
                };

                List <AnimalAid> animalAids = context.AnimalAids.Where(ai => proc.AnimalAids.Any(x => x.Name == ai.Name)).ToList();

                foreach (var aid in animalAids)
                {
                    var procedureAnimalAid = new ProcedureAnimalAid()
                    {
                        AnimalAid = aid,
                        Procedure = procedure
                    };

                    procedureAnimalAids.Add(procedureAnimalAid);
                }

                procedures.Add(procedure);

                sb.AppendLine(SuccessProcedure);
            }

            context.Procedures.AddRange(procedures);
            context.SaveChanges();

            context.ProceduresAnimalAids.AddRange(procedureAnimalAids);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ProcedureDTO[]), new XmlRootAttribute("Procedures"));

            ProcedureDTO[] deserializedXml = (ProcedureDTO[])serializer.Deserialize(new StringReader(xmlString));

            List <Procedure> validProcedures = new List <Procedure>();
            StringBuilder    sb = new StringBuilder();

            foreach (var procedureDTO in deserializedXml)
            {
                Vet    vet    = context.Vets.FirstOrDefault(v => v.Name == procedureDTO.Vet);
                Animal animal = context.Animals.FirstOrDefault(a => a.PassportSerialNumber == procedureDTO.Animal);

                List <ProcedureAnimalAid> validProcedureAnimalAids = new List <ProcedureAnimalAid>();
                bool allAidsExists = true;
                foreach (var procedureDTOAnimalAid in procedureDTO.AnimalAids)
                {
                    AnimalAid animalAid = context.AnimalAids.FirstOrDefault(a => a.Name == procedureDTOAnimalAid.Name);
                    if (animalAid == null || validProcedureAnimalAids.Any(a => a.AnimalAid.Name == procedureDTOAnimalAid.Name))
                    {
                        allAidsExists = false;
                        break;
                    }

                    ProcedureAnimalAid procedureAnimalAid = new ProcedureAnimalAid()
                    {
                        AnimalAid = animalAid
                    };

                    validProcedureAnimalAids.Add(procedureAnimalAid);
                }

                if (!IsValid(procedureDTO) || !procedureDTO.AnimalAids.All(IsValid) || vet == null || animal == null || !allAidsExists)
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                Procedure procedure = new Procedure()
                {
                    Animal              = animal,
                    Vet                 = vet,
                    DateTime            = DateTime.ParseExact(procedureDTO.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture),
                    ProcedureAnimalAids = validProcedureAnimalAids
                };

                validProcedures.Add(procedure);
                sb.AppendLine(SUCCESS_MESSAGE_PROCEDURES);
            }

            context.Procedures.AddRange(validProcedures);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
Esempio n. 5
0
        private static List <ProcedureAnimalAid> CreateProceduresAids(List <AnimalAid> validAids, Procedure procedure)
        {
            var validProcedureAnimalAids = new List <ProcedureAnimalAid>();

            foreach (var aid in validAids)
            {
                var validProcedureAnimalAid = new ProcedureAnimalAid()
                {
                    AnimalAid = aid,
                    Procedure = procedure
                };

                validProcedureAnimalAids.Add(validProcedureAnimalAid);
            }
            return(validProcedureAnimalAids);
        }
Esempio n. 6
0
        private static List <ProcedureAnimalAid> GenerateProcedureAnimalAids(PetClinicContext context, Procedure procedure, ProcedureAnimalAidDto[] validAnimalAids)
        {
            List <ProcedureAnimalAid> procedureAnimalAids = new List <ProcedureAnimalAid>();

            foreach (var animalAidDto in validAnimalAids)
            {
                var procedureAnimalAid = new ProcedureAnimalAid
                {
                    AnimalAid = context.AnimalAids.SingleOrDefault(aa => aa.Name == animalAidDto.Name),
                    Procedure = procedure
                };

                procedureAnimalAids.Add(procedureAnimalAid);
            }

            return(procedureAnimalAids);
        }
Esempio n. 7
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures"));

            var proceduresDto = (ProcedureDto[])xmlSerializer.Deserialize(new StringReader(xmlString));

            var sb         = new StringBuilder();
            var procedures = new List <Procedure>();

            foreach (var dto in proceduresDto)
            {
                if (!IsValid(dto))
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                if (!context.Vets.Any(v => v.Name == dto.VetName))
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                var isValidSerial = context.Animals
                                    .Any(x => x.Passport.SerialNumber == dto.AnimalSerial);

                if (!isValidSerial)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                DateTime dateTime;
                var      isValidDateTime = DateTime.TryParseExact(dto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);
                if (!isValidDateTime)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                var isValidAnimalAid = true;
                foreach (var animalAid in dto.AnimalAids)
                {
                    var isValidAid = context.AnimalAids.Any(x => x.Name == animalAid.Name);

                    if (!isValidAid)
                    {
                        isValidAnimalAid = false;
                        break;
                    }
                }

                if (!isValidAnimalAid)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                bool          isExistAnimalAid = false;
                List <string> animalAids       = new List <string>();

                foreach (var animalAidDto in dto.AnimalAids)
                {
                    if (animalAids.Contains(animalAidDto.Name))
                    {
                        isExistAnimalAid = true;
                        break;
                    }

                    animalAids.Add(animalAidDto.Name);
                }

                if (isExistAnimalAid)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                var vetId = context.Vets.FirstOrDefault(v => v.Name == dto.VetName).Id;

                var animalId = context.Animals.FirstOrDefault(x => x.Passport.SerialNumber == dto.AnimalSerial).Id;

                Procedure procedure = new Procedure()
                {
                    VetId    = vetId,
                    AnimalId = animalId,
                    DateTime = dateTime
                };

                context.Procedures.Add(procedure);
                context.SaveChanges();

                sb.AppendLine("Record successfully imported.");

                var procedureAnimalAids = new List <ProcedureAnimalAid>();

                foreach (var dtoAnimalAid in dto.AnimalAids)
                {
                    var procedureAnimalAid = new ProcedureAnimalAid()
                    {
                        Procedure   = procedure,
                        AnimalAidId = context.AnimalAids.SingleOrDefault(x => x.Name == dtoAnimalAid.Name).Id
                    };
                }

                context.ProceduresAnimalAids.AddRange(procedureAnimalAids);
                context.SaveChanges();
            }

            return(sb.ToString().TrimEnd());
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var serializer = new XmlSerializer(typeof(List <ImportProcedureDto>), new XmlRootAttribute("Procedures"));

            var namespaces = new XmlSerializerNamespaces();

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

            var proceduresToAdd = new List <Procedure>();

            var reader = new StringReader(xmlString);

            using (reader)
            {
                var procedureDtos = (List <ImportProcedureDto>)serializer.Deserialize(reader);

                foreach (var procedureDto in procedureDtos)
                {
                    if (!IsValid(procedureDto))
                    {
                        sb.AppendLine(ErrorMessage);

                        continue;
                    }

                    var vet = context.Vets.FirstOrDefault(v => v.Name == procedureDto.Vet);

                    var animal = context.Animals.FirstOrDefault(a => a.PassportSerialNumber == procedureDto.Animal);

                    DateTime dateTime;

                    var isDateTimeValid = DateTime.TryParseExact(procedureDto.DateTime, "dd-MM-yyyy",
                                                                 CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);

                    if (vet == null || animal == null | !isDateTimeValid)
                    {
                        sb.AppendLine(ErrorMessage);

                        continue;
                    }

                    var procedure = new Procedure()
                    {
                        Animal   = animal,
                        Vet      = vet,
                        DateTime = dateTime,
                    };

                    foreach (var animalAidDto in procedureDto.AnimalAids)
                    {
                        if (!IsValid(animalAidDto))
                        {
                            sb.AppendLine(ErrorMessage);

                            continue;
                        }

                        var animalAid = context.AnimalAids.FirstOrDefault(aa => aa.Name == animalAidDto.Name);

                        var isAnimalAidExisting = procedure.ProcedureAnimalAids.Any(p => p.AnimalAid.Name == animalAid.Name);

                        if (animalAid == null || isAnimalAidExisting)
                        {
                            sb.AppendLine(ErrorMessage);

                            continue;
                        }

                        var procedureAnimalAid = new ProcedureAnimalAid()
                        {
                            Procedure = procedure,
                            AnimalAid = animalAid
                        };

                        procedure.ProcedureAnimalAids.Add(procedureAnimalAid);
                    }

                    proceduresToAdd.Add(procedure);

                    sb.AppendLine(string.Format(SuccessMessageProcedure));
                }

                context.Procedures.AddRange(proceduresToAdd);

                context.SaveChanges();

                return(sb.ToString().Trim());
            }
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var serialize = new XmlSerializer(typeof(ProcedureDto[]),
                                              new XmlRootAttribute("Procedures"));

            var procedures = (ProcedureDto[])serialize.Deserialize(
                new MemoryStream(Encoding.UTF8.GetBytes(xmlString)));

            var validProcedures = new List <Procedure>();

            var sb = new StringBuilder();

            foreach (var procedureDto in procedures)
            {
                if (!IsValid(procedureDto))
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                var vet = context.Vets
                          .SingleOrDefault(v => v.Name == procedureDto.Vet);

                var animal = context.Animals
                             .SingleOrDefault(a => a.PassportSerialNumber == procedureDto.Animal);

                var dateTime = DateTime.ParseExact(procedureDto.DateTime,
                                                   "dd-MM-yyyy", CultureInfo.InvariantCulture);

                bool animalAidsInvalid = false;

                var procedureAnimalAids = new List <ProcedureAnimalAid>();

                foreach (var animalAidDto in procedureDto.AnimalAids)
                {
                    var animalAid = context.AnimalAids
                                    .SingleOrDefault(a => a.Name == animalAidDto.Name);

                    if (animalAid == null ||
                        procedureAnimalAids.Any(p => p.AnimalAid == animalAid))
                    {
                        animalAidsInvalid = true;
                        break;
                    }

                    var procedureAnimalAid = new ProcedureAnimalAid
                    {
                        AnimalAid = animalAid
                    };

                    procedureAnimalAids.Add(procedureAnimalAid);
                }

                if (vet == null || animal == null || animalAidsInvalid)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                var procedure = new Procedure
                {
                    Animal              = animal,
                    DateTime            = dateTime,
                    Vet                 = vet,
                    ProcedureAnimalAids = procedureAnimalAids
                };

                validProcedures.Add(procedure);
                sb.AppendLine("Record successfully imported.");
            }

            context.Procedures.AddRange(validProcedures);
            context.SaveChanges();

            return(sb.ToString());
        }
Esempio n. 10
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ProcedureImportDto[]), new XmlRootAttribute("Procedures"));

            ProcedureImportDto[] procedureDtos = (ProcedureImportDto[])serializer.Deserialize(new StringReader(xmlString));

            List <Procedure> validProcedures = new List <Procedure>();
            StringBuilder    sb = new StringBuilder();

            foreach (var procedureDto in procedureDtos)
            {
                Vet    vet    = context.Vets.SingleOrDefault(v => v.Name == procedureDto.VetName);
                Animal animal = context.Animals.SingleOrDefault(a => a.PassportSerialNumber == procedureDto.AnimalName);

                if (!IsValid(procedureDto) || vet == null || animal == null)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                bool             isError    = false;
                List <AnimalAid> animalAids = new List <AnimalAid>();
                foreach (var animalAidDto in procedureDto.AnimalAids)
                {
                    AnimalAid animalAid = context.AnimalAids.SingleOrDefault(ai => ai.Name == animalAidDto.Name);

                    bool aidAlreadyGiven = animalAids.Any(ai => ai.Name == animalAidDto.Name);
                    if (!IsValid(animalAidDto) || animalAid == null || aidAlreadyGiven)
                    {
                        sb.AppendLine(ErrorMessage);
                        isError = true;
                        break;
                    }

                    animalAids.Add(animalAid);
                }

                if (isError)
                {
                    continue;
                }

                Procedure procedure = new Procedure
                {
                    Animal   = animal,
                    Vet      = vet,
                    DateTime = DateTime.ParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture),
                };

                foreach (var animalAid in animalAids)
                {
                    ProcedureAnimalAid procedureAnimalAid = new ProcedureAnimalAid
                    {
                        AnimalAid = animalAid,
                        Procedure = procedure
                    };

                    procedure.ProcedureAnimalAids.Add(procedureAnimalAid);
                }

                validProcedures.Add(procedure);
                sb.AppendLine(SuccessMessage);
            }

            context.Procedures.AddRange(validProcedures);
            context.SaveChanges();

            return(sb.ToString().Trim());
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var serializer = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures"));
            var deserializedProductDtos = (ProcedureDto[])serializer.Deserialize(new StringReader(xmlString));

            var procedures          = new List <Procedure>();
            var procedureAnimalAids = new List <ProcedureAnimalAid>();

            foreach (var procedureDto in deserializedProductDtos)
            {
                var vet    = context.Vets.SingleOrDefault(v => v.Name == procedureDto.Vet);
                var animal = context.Animals.SingleOrDefault(a => a.PassportSerialNumber == procedureDto.Animal);
                if (!IsValid(procedureDto) || vet == null || animal == null)
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                var dateTime  = DateTime.ParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                var procedure = new Procedure
                {
                    Vet      = vet,
                    Animal   = animal,
                    DateTime = dateTime
                };

                var animalAidsAreValid       = true;
                var validProcedureAnimalAids = new List <ProcedureAnimalAid>();

                foreach (var animalAidDto in procedureDto.AnimalAids)
                {
                    var animalAid    = context.AnimalAids.SingleOrDefault(aa => aa.Name.Equals(animalAidDto.Name));
                    var alreadyAddes = validProcedureAnimalAids
                                       .Any(vpaa => vpaa.AnimalAid.Name.Equals(animalAidDto.Name));

                    if (!IsValid(animalAidDto) || animalAid == null || alreadyAddes)
                    {
                        animalAidsAreValid = false;
                        break;
                    }

                    var procedureAnimalAid = new ProcedureAnimalAid
                    {
                        Procedure = procedure,
                        AnimalAid = animalAid
                    };

                    validProcedureAnimalAids.Add(procedureAnimalAid);
                }

                if (!animalAidsAreValid)
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                procedures.Add(procedure);
                procedureAnimalAids.AddRange(validProcedureAnimalAids);

                sb.AppendLine($"Record successfully imported.");
            }

            context.Procedures.AddRange(procedures);
            context.SaveChanges();

            context.ProceduresAnimalAids.AddRange(procedureAnimalAids);
            context.SaveChanges();

            var result = sb.ToString();

            return(result);
        }
Esempio n. 12
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            var serializer = new XmlSerializer(typeof(ImportProcedureDto[])
                                               , new XmlRootAttribute("Procedures"));

            var procedureDtos = (ImportProcedureDto[])serializer.Deserialize(new StringReader(xmlString));

            var procedures = new List <Procedure>();

            foreach (var procDto in procedureDtos)
            {
                if (!IsValid(procDto) || !procDto.AnimalAids.All(IsValid))
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                var vet = context.Vets.FirstOrDefault(v => v.Name == procDto.VetName);
                if (vet == null)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                var animal = context.Animals
                             .FirstOrDefault(a => a.PassportSerialNumber == procDto.AnimalSerialNumber);

                if (animal == null)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                var procAnimalNames  = procDto.AnimalAids.Select(f => f.Name).ToArray();
                var animalAidChecker = false;

                var allAnimalNames = context.AnimalAids.Select(a => a.Name).ToArray();

                foreach (var animalName in procAnimalNames)
                {
                    if (allAnimalNames.Contains(animalName) == false)
                    {
                        animalAidChecker = true;
                        break;
                    }
                }

                if (animalAidChecker == true)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }
                List <ProcedureAnimalAid> currentAnimalAids = new List <ProcedureAnimalAid>();
                bool duplicateAidChecker = false;
                foreach (var animalAidDto in procDto.AnimalAids)
                {
                    var animalAid = currentAnimalAids
                                    .FirstOrDefault(a => a.AnimalAid.Name == animalAidDto.Name);

                    if (animalAid == null)
                    {
                        animalAid = new ProcedureAnimalAid()
                        {
                            AnimalAid = new AnimalAid()
                            {
                                Name = animalAidDto.Name
                            }
                        };
                        currentAnimalAids.Add(animalAid);
                    }
                    else
                    {
                        duplicateAidChecker = true;
                        sb.AppendLine("Error: Invalid data.");
                        break;
                    }
                }
                if (duplicateAidChecker == false)
                {
                    sb.AppendLine("Record successfully imported.");

                    var procedure = new Procedure()
                    {
                        Animal   = animal,
                        DateTime = DateTime
                                   .ParseExact(procDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture),
                        ProcedureAnimalAids = currentAnimalAids,
                        Vet = vet
                    };

                    procedures.Add(procedure);
                }
            }

            context.Procedures.AddRange(procedures);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            XmlSerializer ser = new XmlSerializer(typeof(ProcedureImportDto[]), new XmlRootAttribute("Procedures"));

            var proceduresDto = (ProcedureImportDto[])ser.Deserialize(new StringReader(xmlString));

            StringBuilder sb = new StringBuilder();

            foreach (var procedureDto in proceduresDto)
            {
                var animal = context.Animals.FirstOrDefault(a => a.PassportSerialNumber == procedureDto.Animal);

                var vet = context.Vets.FirstOrDefault(v => v.Name == procedureDto.Vet);

                string[] aidsNames = procedureDto.AnimalAids.Select(aa => aa.Name).ToArray();

                int distinctCount = aidsNames.Distinct().Count();

                int count = aidsNames.Count();

                if (!IsValid(procedureDto) || animal == null || vet == null || distinctCount != count ||
                    !aidsNames.All(aa => context.AnimalAids.Any(x => x.Name == aa)))
                {
                    sb.AppendLine("Error: Invalid data.");

                    continue;
                }

                Procedure procedure = new Procedure
                {
                    Animal   = animal,
                    Vet      = vet,
                    DateTime = DateTime.ParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture)
                };

                foreach (var animalAidDto in procedureDto.AnimalAids)
                {
                    var animalAid = context.AnimalAids.FirstOrDefault(aa => aa.Name == animalAidDto.Name);

                    ProcedureAnimalAid procedureAnimalAid = new ProcedureAnimalAid
                    {
                        AnimalAid   = animalAid,
                        AnimalAidId = animalAid.Id,
                        Procedure   = procedure,
                        ProcedureId = procedure.Id
                    };

                    procedure.ProcedureAnimalAids.Add(procedureAnimalAid);

                    context.ProceduresAnimalAids.Add(procedureAnimalAid);
                }

                context.Procedures.Add(procedure);

                sb.AppendLine($"Record successfully imported.");

                context.SaveChanges();
            }

            return(sb.ToString().TrimEnd());
        }
Esempio n. 14
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var serializer = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures"));

            var proceduresDto = (ProcedureDto[])serializer.Deserialize(new StringReader(xmlString));

            StringBuilder stringBuilder = new StringBuilder();

            List <Procedure> procedures = new List <Procedure>();

            foreach (var procedure in proceduresDto)
            {
                if (!IsValid(procedure))
                {
                    stringBuilder.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                var vet    = context.Vets.FirstOrDefault(x => x.Name == procedure.VetName);
                var animal = context.Animals.FirstOrDefault(x => x.PassportSerialNumber == procedure.AnimalSerialNumber);

                bool isDateTimeCorrect = DateTime.TryParseExact(procedure.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTime);

                if (vet == null || animal == null || !isDateTimeCorrect)
                {
                    stringBuilder.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                List <AnimalAid> animalAids        = new List <AnimalAid>();
                bool             isAnimalAidsExist = true;

                foreach (var procedureanimalAid in procedure.ProcedureAnimalAids)
                {
                    var animalAid = context.AnimalAids.FirstOrDefault(x => x.Name == procedureanimalAid.Name);

                    if (animalAid == null || animalAids.Any(x => x.Name == animalAid.Name))
                    {
                        isAnimalAidsExist = false;
                        break;
                    }

                    animalAids.Add(animalAid);
                }

                if (!isAnimalAidsExist)
                {
                    stringBuilder.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                var procudure = new Procedure
                {
                    Vet      = vet,
                    Animal   = animal,
                    DateTime = dateTime,
                };

                foreach (var animalAid in animalAids)
                {
                    ProcedureAnimalAid procedureAnimalAid = new ProcedureAnimalAid
                    {
                        AnimalAid = animalAid,
                        Procedure = procudure
                    };

                    procudure.ProcedureAnimalAids.Add(procedureAnimalAid);
                }

                if (!IsValid(procudure))
                {
                    stringBuilder.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                procedures.Add(procudure);
                stringBuilder.AppendLine(SUCCESS_MESSAGE);
            }

            context.Procedures.AddRange(procedures);
            context.SaveChanges();

            return(stringBuilder.ToString().TrimEnd());
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var xmlSerializer          = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures"));
            var deserializedProcedures = (ProcedureDto[])xmlSerializer.Deserialize(new StringReader(xmlString));

            var sb = new StringBuilder();

            var procedures = new List <Procedure>();

            foreach (var procedureDto in deserializedProcedures)
            {
                var vet    = context.Vets.FirstOrDefault(v => v.Name == procedureDto.Vet);
                var animal = context.Animals.FirstOrDefault(a => a.PassportSerialNumber == procedureDto.SerialNumber);
                if (vet == null || animal == null)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var proceduresAnimalAids = new List <ProcedureAnimalAid>();

                var hasInvalidAnimalAid = false;

                foreach (var animalAidDto in procedureDto.AnimalAids)
                {
                    var animalAid       = context.AnimalAids.FirstOrDefault(a => a.Name == animalAidDto.Name);
                    var animalAidExists = proceduresAnimalAids.Any(paa => paa.AnimalAid.Name == animalAidDto.Name);

                    if (animalAid == null || animalAidExists)
                    {
                        hasInvalidAnimalAid = true;
                        break;
                    }

                    var procedureAnimalAid = new ProcedureAnimalAid()
                    {
                        AnimalAid = animalAid
                    };

                    proceduresAnimalAids.Add(procedureAnimalAid);
                }

                if (hasInvalidAnimalAid)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var procedure = new Procedure()
                {
                    Animal              = animal,
                    DateTime            = DateTime.ParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture),
                    Vet                 = vet,
                    ProcedureAnimalAids = proceduresAnimalAids
                };

                procedures.Add(procedure);

                sb.AppendLine(SuccessfullyImportedProcedure);
            }

            context
            .Procedures
            .AddRange(procedures);

            context
            .SaveChanges();

            return(sb.ToString().Trim());
        }
Esempio n. 16
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var serializer = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures"));

            var deserializedProcedures = (ProcedureDto[])serializer.Deserialize(new StringReader(xmlString));

            var sb = new StringBuilder();

            var validProcedures = new List <Procedure>();

            foreach (ProcedureDto procedureDto in deserializedProcedures)
            {
                Vet    vet    = context.Vets.SingleOrDefault(v => v.Name == procedureDto.Vet);
                Animal animal = context.Animals.SingleOrDefault(a => a.PassportSerialNumber == procedureDto.Animal);

                if (!IsValid(procedureDto) || vet == null || animal == null)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var dateTime = DateTime.ParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture);

                var procedure = new Procedure
                {
                    Animal   = animal,
                    Vet      = vet,
                    DateTime = dateTime
                };

                bool allAnimalAidsExist = true;

                var validProcedureAnimalAids = new List <ProcedureAnimalAid>();

                foreach (ProcedureAnimalAidDto procedureAnimalAidDto in procedureDto.AnimalAids)
                {
                    AnimalAid animalAid = context.AnimalAids
                                          .SingleOrDefault(aa => aa.Name == procedureAnimalAidDto.Name);

                    if (animalAid == null ||
                        validProcedureAnimalAids.Any(pa => pa.AnimalAid.Name == procedureAnimalAidDto.Name))
                    {
                        allAnimalAidsExist = false;
                        break;
                    }

                    var procedureAnimalAid = new ProcedureAnimalAid
                    {
                        Procedure = procedure,
                        AnimalAid = animalAid
                    };

                    validProcedureAnimalAids.Add(procedureAnimalAid);
                }

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

                procedure.ProcedureAnimalAids = validProcedureAnimalAids;

                validProcedures.Add(procedure);

                sb.AppendLine("Record successfully imported.");
            }

            context.Procedures.AddRange(validProcedures);

            context.SaveChanges();

            string result = sb.ToString().TrimEnd();

            return(result);
        }
Esempio n. 17
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var serializer = new XmlSerializer(typeof(ImportProcedureDto[]), new XmlRootAttribute("Procedures"));

            var deserializedProcedures = (ImportProcedureDto[])serializer.Deserialize(new StringReader(xmlString));

            List <Procedure> procedures    = new List <Procedure>();
            StringBuilder    resultMessage = new StringBuilder();

            var validVets      = context.Vets.ToList();
            var validAnimals   = context.Animals.ToList();
            var validAnimalAid = context.AnimalAids.ToList();

            foreach (var proc in deserializedProcedures)
            {
                var  animalAidDto = proc.AnimalAids.ToList();
                bool hasValidAids = HasValidAnimalAids(validAnimalAid, animalAidDto);

                bool hasValidData = validVets.Any(x => x.Name == proc.Vet) &&
                                    validAnimals.Any(x => x.PassportSerialNumber == proc.Animal) &&
                                    hasValidAids;

                if (hasValidData)
                {
                    var currentProcedure = new Procedure
                    {
                        Animal   = validAnimals.FirstOrDefault(x => x.PassportSerialNumber == proc.Animal),
                        Vet      = validVets.FirstOrDefault(x => x.Name == proc.Vet),
                        DateTime = DateTime.ParseExact(proc.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture),
                    };

                    bool hasRepeatedAids = false;
                    foreach (var aid in animalAidDto)
                    {
                        var currentAid = new ProcedureAnimalAid
                        {
                            AnimalAid = validAnimalAid.FirstOrDefault(x => x.Name == aid.Name)
                        };

                        if (!currentProcedure.ProcedureAnimalAids.Any(x => x.AnimalAid.Name == aid.Name))
                        {
                            currentProcedure.ProcedureAnimalAids.Add(currentAid);
                        }
                        else
                        {
                            resultMessage.AppendLine("Error: Invalid data.");
                            hasRepeatedAids = true;
                            break;
                        }
                    }

                    if (!hasRepeatedAids)
                    {
                        procedures.Add(currentProcedure);
                        resultMessage.AppendLine($"Record successfully imported.");
                    }
                }

                else
                {
                    resultMessage.AppendLine("Error: Invalid data.");
                    continue;
                }
            }

            context.Procedures.AddRange(procedures);
            context.SaveChanges();

            return(resultMessage.ToString().TrimEnd());
        }
Esempio n. 18
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var serializer             = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures"));
            var deserializedProcedures = (ProcedureDto[])serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xmlString)));

            var sb = new StringBuilder();

            var validProcedures = new List <Procedure>();

            foreach (var procedureDto in deserializedProcedures)
            {
                if (!IsValid(procedureDto))
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var validAnimalAid = procedureDto.AnimalAids.All(IsValid);

                if (!validAnimalAid)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var vetExists = context.Vets.Any(v => v.Name == procedureDto.Vet);

                if (!vetExists)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var animalSerialNumberExists = context.Animals.Any(a => a.PassportSerialNumber == procedureDto.Animal);

                if (!animalSerialNumberExists)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var procedureContainsExistingAnimalAids = procedureDto.AnimalAids.All(aa => context.AnimalAids.Any(a => a.Name == aa.Name));

                if (!procedureContainsExistingAnimalAids)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }
                var dateTime = DateTime.ParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture);

                var procedure = new Procedure()
                {
                    Animal   = context.Animals.SingleOrDefault(x => x.PassportSerialNumber == procedureDto.Animal),
                    Vet      = context.Vets.SingleOrDefault(x => x.Name == procedureDto.Vet),
                    DateTime = dateTime
                };

                var procedureAnimalAids = new List <ProcedureAnimalAid>();

                var animalAidNameIsRepeating = false;

                foreach (var animalAidDto in procedureDto.AnimalAids)
                {
                    if (procedureAnimalAids.Any(pa => pa.AnimalAid.Name == animalAidDto.Name))
                    {
                        sb.AppendLine(FailureMessage);
                        animalAidNameIsRepeating = true;
                        break;
                    }

                    var procedureAnimalAid = new ProcedureAnimalAid()
                    {
                        Procedure = procedure,
                        AnimalAid = context.AnimalAids.SingleOrDefault(aa => aa.Name == animalAidDto.Name)
                    };

                    procedureAnimalAids.Add(procedureAnimalAid);
                }

                if (animalAidNameIsRepeating)
                {
                    continue;
                }

                procedure.ProcedureAnimalAids = procedureAnimalAids;

                validProcedures.Add(procedure);
                sb.AppendLine("Record successfully imported.");
            }
            context.Procedures.AddRange(validProcedures);
            context.SaveChanges();

            var result = sb.ToString();

            return(result);
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var sb             = new StringBuilder();
            var proceduresDtos = new List <ProcedureDto>();

            XmlSerializer serializer = new XmlSerializer(typeof(List <ProcedureDto>), new XmlRootAttribute("Procedures"));

            using (TextReader reader = new StringReader(xmlString))
            {
                proceduresDtos = (List <ProcedureDto>)serializer.Deserialize(reader);
            }

            foreach (var item in proceduresDtos)
            {
                var currentVet = context.Vets.FirstOrDefault(e => e.Name == item.VetName);
                if (currentVet == null)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }
                var currentAnimal = context.Animals.FirstOrDefault(e => e.PassportSerialNumber == item.AnimalSerialNumber);
                if (currentAnimal == null)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }
                var currentAnimalAids = context.AnimalAids.Where(e => item.AnimalAids.Any(x => x.AnimalAidName == e.Name)).ToArray();
                if (currentAnimalAids.Select(e => e.Name).ToArray().Distinct().Count() != item.AnimalAids.Count())
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                var procedure = new Procedure()
                {
                    AnimalId = currentAnimal.Id,
                    DateTime = DateTime.ParseExact(item.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture),
                    VetId    = currentVet.Id
                };

                context.Procedures.Add(procedure);
                context.SaveChanges();
                var procedureId         = procedure.Id;
                var procedureAnimalAids = new List <ProcedureAnimalAid>();
                foreach (var animalAid in currentAnimalAids)
                {
                    var procedureAnimalAid = new ProcedureAnimalAid()
                    {
                        ProcedureId = procedureId,
                        AnimalAidId = animalAid.Id
                    };
                    procedureAnimalAids.Add(procedureAnimalAid);
                }

                context.ProceduresAnimalAids.AddRange(procedureAnimalAids);
                context.SaveChanges();

                sb.AppendLine($"Record successfully imported.");
            }



            var result = sb.ToString().TrimEnd();

            return(result);
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var serializer = new XmlSerializer(typeof(List <ProcedureDto>), new XmlRootAttribute("Procedures"));

            var deserializedProcedures = (List <ProcedureDto>)serializer.Deserialize(new StringReader(xmlString));

            var procedures = new List <Procedure>();

            foreach (var procedureDto in deserializedProcedures)
            {
                var vet    = context.Vets.SingleOrDefault(v => v.Name == procedureDto.Vet);
                var animal = context.Animals.SingleOrDefault(a => a.PassportSerialNumber == procedureDto.Animal);

                var procedureAnimalAids = new List <ProcedureAnimalAid>();

                var allAnimalsAidsExists = true;

                foreach (var procedureDtoAnimalAid in procedureDto.AnimalAids)
                {
                    var animalAid = context.AnimalAids.SingleOrDefault(a => a.Name == procedureDtoAnimalAid.Name);

                    if (animalAid == null || procedureAnimalAids.Any(p => p.AnimalAid.Name == procedureDtoAnimalAid.Name))
                    {
                        allAnimalsAidsExists = false;
                        break;
                    }

                    ProcedureAnimalAid procedureAnimalAid = new ProcedureAnimalAid
                    {
                        AnimalAid = animalAid
                    };

                    procedureAnimalAids.Add(procedureAnimalAid);
                }

                if (!IsValid(procedureDto) ||
                    !procedureDto.AnimalAids.All(IsValid) ||
                    vet == null ||
                    animal == null ||
                    !allAnimalsAidsExists)
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                var procedure = new Procedure
                {
                    Animal              = animal,
                    Vet                 = vet,
                    DateTime            = DateTime.ParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture),
                    ProcedureAnimalAids = procedureAnimalAids
                };

                procedures.Add(procedure);

                sb.AppendLine("Record successfully imported.");
            }

            context.Procedures.AddRange(procedures);

            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Esempio n. 21
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            var serializer             = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures"));
            var deserializerProcedures = (ProcedureDto[])serializer.Deserialize(new StringReader(xmlString));

            foreach (var procedureDto in deserializerProcedures)
            {
                bool dateIsValid = DateTime
                                   .TryParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTime);
                if (!IsValid(procedureDto) || !dateIsValid)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                var vet    = context.Vets.Where(x => x.Name == procedureDto.VetName).FirstOrDefault();
                var animal = context.Animals.Where(x => x.Passport.SerialNumber == procedureDto.SerialNumberAnimal).FirstOrDefault();

                if (vet == null || animal == null || procedureDto.AnimalAids == null)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                List <AnimalAid> animalAids = new List <AnimalAid>();
                var allAidsExist            = true;
                foreach (var animalAidDto in procedureDto.AnimalAids)
                {
                    var  animalAid         = context.AnimalAids.Where(x => x.Name == animalAidDto.Name).FirstOrDefault();
                    bool isExistsAnimalAid = animalAids.Any(x => x.Name == animalAid.Name);
                    if (animalAid == null || !IsValid(animalAidDto) || isExistsAnimalAid)
                    {
                        allAidsExist = false;
                        break;
                    }

                    animalAids.Add(animalAid);
                }

                if (!allAidsExist)
                {
                    sb.AppendLine("Error: Invalid data.");
                    continue;
                }

                var date = DateTime
                           .ParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                var procedure = new Procedure()
                {
                    Vet      = vet,
                    Animal   = animal,
                    DateTime = date
                };
                context.Procedures.Add(procedure);
                context.SaveChanges();

                List <ProcedureAnimalAid> procedureAnimalAids = new List <ProcedureAnimalAid>();
                foreach (var aa in animalAids)
                {
                    var procedureAnimalAid = new ProcedureAnimalAid()
                    {
                        Procedure = procedure,
                        AnimalAid = aa
                    };
                    procedureAnimalAids.Add(procedureAnimalAid);
                }
                context.ProceduresAnimalAids.AddRange(procedureAnimalAids);
                context.SaveChanges();

                sb.AppendLine("Record successfully imported.");
            }

            return(sb.ToString().TrimEnd());
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var procedures = new List <Procedure>();
            var result     = new StringBuilder();

            var serializer    = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures"));
            var objProcedures = (ProcedureDto[])serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xmlString)));

            foreach (var objProcedure in objProcedures)
            {
                var ifAnimalAidsValid = true;

                var animal   = context.Animals.FirstOrDefault(a => a.PassportSerialNumber == objProcedure.Animal);
                var vet      = context.Vets.FirstOrDefault(v => v.Name == objProcedure.Vet);
                var ifParsed = DateTime.TryParseExact(objProcedure.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out DateTime date);
                if (!IsValid(objProcedure) || !ifParsed ||
                    animal == null || vet == null)
                {
                    result.AppendLine(ErrorMsg);
                    continue;
                }

                var procedure = new Procedure
                {
                    Animal   = animal,
                    Vet      = vet,
                    DateTime = date
                };

                var procedureAnimalAids = new List <ProcedureAnimalAid>();
                foreach (var objAnimalAid in objProcedure.AnimalAids)
                {
                    var ifAnimalAidExistsInThisProcedure =
                        procedureAnimalAids.Any(paa => paa.AnimalAid.Name == objAnimalAid.Name);
                    var animalAid = context.AnimalAids.FirstOrDefault(aa => aa.Name == objAnimalAid.Name);
                    if (animalAid == null || ifAnimalAidExistsInThisProcedure)
                    {
                        ifAnimalAidsValid = false;
                        break;
                    }

                    var procedureAnimalAid = new ProcedureAnimalAid
                    {
                        Procedure = procedure,
                        AnimalAid = animalAid
                    };

                    procedureAnimalAids.Add(procedureAnimalAid);
                    procedure.ProcedureAnimalAids.Add(procedureAnimalAid);
                }

                if (!ifAnimalAidsValid)
                {
                    result.AppendLine(ErrorMsg);
                    continue;
                }

                procedures.Add(procedure);
                result.AppendLine("Record successfully imported.");
            }

            context.Procedures.AddRange(procedures);
            context.SaveChanges();

            return(result.ToString().Trim());
        }
Esempio n. 23
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var serializer       = new XmlSerializer(typeof(ProcedureImportDto[]), new XmlRootAttribute("Procedures"));
            var deserializedData = (ProcedureImportDto[])serializer.Deserialize(new StringReader(xmlString));

            var sb = new StringBuilder();

            var procedures = new List <Procedure>();


            foreach (var dto in deserializedData)
            {
                var animalId = context.Animals.FirstOrDefault(a => a.PassportSerialNumber == dto.Animal)?.Id;
                var vetId    = context.Vets.FirstOrDefault(v => v.Name == dto.Vet)?.Id;

                if (animalId == null || vetId == null)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var procedure = new Procedure();
                procedure.AnimalId = animalId.Value;
                procedure.VetId    = vetId.Value;
                procedure.DateTime = DateTime.ParseExact(dto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture);

                var procedureAnimalAids = new List <ProcedureAnimalAid>();

                var hasInvalidAid = false;

                foreach (var aid in dto.AnimalAids)
                {
                    var animalAidId = context.AnimalAids.FirstOrDefault(a => a.Name == aid.Name)?.Id;

                    if (animalAidId == null)
                    {
                        hasInvalidAid = true;
                        continue;
                    }

                    var alreadyExists = procedureAnimalAids.Any(pa => pa.AnimalAidId == animalAidId.Value);

                    if (alreadyExists)
                    {
                        hasInvalidAid = true;
                        continue;
                    }

                    var procedureAnimalAid = new ProcedureAnimalAid()
                    {
                        AnimalAidId = animalAidId.Value,
                        Procedure   = procedure
                    };

                    procedureAnimalAids.Add(procedureAnimalAid);
                }

                if (hasInvalidAid)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                procedure.ProcedureAnimalAids = procedureAnimalAids;
                procedures.Add(procedure);

                sb.AppendLine(ProcedureSuccessMessage);
            }

            context.Procedures.AddRange(procedures);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Esempio n. 24
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var xmlSerializer = new XmlSerializer(typeof(ProcedureDto[]),
                                                  new XmlRootAttribute("Procedures"));

            var procedureDtos = (ProcedureDto[])xmlSerializer.Deserialize(new StringReader(xmlString));

            var sb         = new StringBuilder();
            var procedures = new List <Procedure>();


            foreach (var procedureDto in procedureDtos)
            {
                int? vetId          = context.Vets.SingleOrDefault(v => v.Name == procedureDto.Vet)?.Id;
                bool dateIsValid    = DateTime.TryParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTime);
                bool passportExists = context.Passports.Any(p => p.SerialNumber == procedureDto.Animal);

                if (vetId == null || !passportExists || procedureDto.AnimalAids == null || !dateIsValid)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var  animalAidsIds = new List <int>();
                bool allAidsExist  = true;

                foreach (var aid in procedureDto.AnimalAids)
                {
                    string aidName = aid.Name;

                    int?aidId = context.AnimalAids.SingleOrDefault(a => a.Name == aidName)?.Id;

                    if (aidId == null || animalAidsIds.Any(id => id == aidId))
                    {
                        allAidsExist = false;
                        break;
                    }

                    animalAidsIds.Add(aidId.Value);
                }


                if (!allAidsExist)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var procedure = new Procedure
                {
                    VetId    = vetId.Value,
                    AnimalId = context.Animals.Single(a => a.PassportSerialNumber == procedureDto.Animal).Id,
                    DateTime = dateTime,
                };

                foreach (var id in animalAidsIds)
                {
                    var mapping = new ProcedureAnimalAid()
                    {
                        Procedure   = procedure,
                        AnimalAidId = id
                    };

                    procedure.ProcedureAnimalAids.Add(mapping);
                }

                bool isValid = IsValid(procedure);

                if (!isValid)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                procedures.Add(procedure);
                sb.AppendLine("Record successfully imported.");
            }

            context.Procedures.AddRange(procedures);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Esempio n. 25
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var serializer = new XmlSerializer(typeof(ImportProcedureDto[]), new XmlRootAttribute("Procedures"));
            var sb         = new StringBuilder();

            var proceduresDto = (ImportProcedureDto[])serializer.Deserialize(new StringReader(xmlString));

            var validProcedures          = new List <Procedure>();
            var validProcedureAnimalAids = new List <ProcedureAnimalAid>();

            foreach (var procedureDto in proceduresDto)
            {
                if (!IsValid(procedureDto))
                {
                    sb.AppendLine(errorMessage);
                    continue;
                }

                var vet = (Vet)GetEntity <Vet>(context, procedureDto.VetName);

                var animal = (Animal)GetEntity <Animal>(context, procedureDto.AnimalPassportSerialNumber);

                if (vet == null || animal == null)
                {
                    sb.AppendLine(errorMessage);
                    continue;
                }

                var procedure = new Procedure()
                {
                    Vet      = vet,
                    Animal   = animal,
                    DateTime = DateTime.ParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture)
                };

                var isBreackable = false;

                var validAnimalAids = new List <AnimalAid>();

                foreach (var animalAidDto in procedureDto.AnimalAids)
                {
                    var animalAid = (AnimalAid)GetEntity <AnimalAid>(context, animalAidDto.AidName);

                    if (!IsValid(animalAidDto.AidName) ||
                        animalAid == null ||
                        validAnimalAids.Any(x => x.Name == animalAidDto.AidName))
                    {
                        isBreackable = true;
                        sb.AppendLine(errorMessage);
                        break;
                    }

                    validAnimalAids.Add(animalAid);
                }

                if (isBreackable)
                {
                    continue;
                }

                foreach (var animalAid in validAnimalAids)
                {
                    var procedureAnimalAid = new ProcedureAnimalAid()
                    {
                        Procedure = procedure,
                        AnimalAid = animalAid
                    };

                    procedure.ProcedureAnimalAids.Add(procedureAnimalAid);
                    validProcedureAnimalAids.Add(procedureAnimalAid);
                }

                validProcedures.Add(procedure);
                sb.AppendLine($"Record successfully imported.");
            }

            context.Procedures.AddRange(validProcedures);
            context.ProceduresAnimalAids.AddRange(validProcedureAnimalAids);
            context.SaveChanges();

            var result = sb.ToString().Trim();

            return(result);
        }
Esempio n. 26
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var xml = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures"));
            var deserializerVets = (ProcedureDto[])xml.Deserialize(new StringReader(xmlString));

            List <ProcedureAnimalAid> procedureAnimalAids = new List <ProcedureAnimalAid>();

            foreach (var procedureDto in deserializerVets)
            {
                List <AnimalAid> animalAids     = new List <AnimalAid>();
                bool             validAnimalAid = true;

                if (!IsValid(procedureDto))
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }

                var vet = context.Vets.FirstOrDefault(x => x.Name == procedureDto.Vet);
                if (vet == null)
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }

                var animal = context.Animals.FirstOrDefault(x => x.Passport.SerialNumber == procedureDto.AnimalPassportNumber);
                if (animal == null)
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }

                var date = DateTime.ParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture);

                foreach (var animalAidProcDto in procedureDto.AnimalAids)
                {
                    if (!IsValid(animalAidProcDto))
                    {
                        validAnimalAid = false;
                        break;
                    }

                    if (animalAids.Any(x => x.Name == animalAidProcDto.Name))
                    {
                        validAnimalAid = false;
                        break;
                    }

                    AnimalAid animalAid = context.AnimalAids.FirstOrDefault(x => x.Name == animalAidProcDto.Name);

                    if (animalAid == null)
                    {
                        validAnimalAid = false;
                        break;
                    }

                    animalAids.Add(animalAid);
                }

                if (!validAnimalAid)
                {
                    sb.AppendLine(errorMsg);
                    continue;
                }

                Procedure procedure = new Procedure
                {
                    Animal   = animal,
                    Vet      = vet,
                    DateTime = date,
                };

                foreach (var i in animalAids)
                {
                    ProcedureAnimalAid procedureAnimalAid = new ProcedureAnimalAid
                    {
                        AnimalAid = i,
                        Procedure = procedure
                    };

                    procedureAnimalAids.Add(procedureAnimalAid);
                }

                sb.AppendLine(successMgsPocedure);
            }

            context.ProceduresAnimalAids.AddRange(procedureAnimalAids);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            XmlSerializer serializer             = new XmlSerializer(typeof(ProcedureDto[]), new XmlRootAttribute("Procedures"));
            var           deserializedProcedures = (ProcedureDto[])serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xmlString)));

            var procedures = new List <Procedure>();

            StringBuilder sb = new StringBuilder();

            var vets       = context.Vets.ToList();
            var animals    = context.Animals.ToList();
            var animalAids = context.AnimalAids.ToList();

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

                if (!vets.Any(n => n.Name == dto.VetName))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var vet = vets.SingleOrDefault(n => n.Name == dto.VetName);

                if (!animals.Any(s => s.PassportSerialNumber == dto.AnimalSerialNumber))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var animal = animals.SingleOrDefault(s => s.PassportSerialNumber == dto.AnimalSerialNumber);

                var distenctedListCount = dto.AnimalAids.Select(n => n.Name).Distinct().Count();

                if (distenctedListCount != dto.AnimalAids.Count)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                DateTime date      = DateTime.ParseExact(dto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture);
                var      procedure = new Procedure()
                {
                    Vet    = vet,
                    Animal = animal,
                    Date   = date
                };

                List <ProcedureAnimalAid> procedureAnimalAids = new List <ProcedureAnimalAid>();
                bool isAidExists = true;

                foreach (var aid in dto.AnimalAids)
                {
                    if (!animalAids.Any(n => n.Name == aid.Name))
                    {
                        isAidExists = false;
                        break;
                    }

                    var ai = animalAids.SingleOrDefault(n => n.Name == aid.Name);

                    var procedureAnimalAid = new ProcedureAnimalAid()
                    {
                        AnimalAid = ai, Procedure = procedure
                    };

                    procedure.ProcedureAnimalAids.Add(procedureAnimalAid);
                }

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

                procedures.Add(procedure);

                sb.AppendLine($"Record successfully imported.");
            }

            context.Procedures.AddRange(procedures);
            context.SaveChanges();

            string result = sb.ToString();

            return(result);
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            StringBuilder       output        = new StringBuilder();
            var                 serializer    = new XmlSerializer(typeof(ProcedureImportDto[]), new XmlRootAttribute("Procedures"));
            var                 procedureDtos = (ProcedureImportDto[])serializer.Deserialize(new StringReader(xmlString));
            HashSet <Procedure> procedures    = new HashSet <Procedure>();

            foreach (var procedureDto in procedureDtos)
            {
                if (!IsObjectValid(procedureDto))
                {
                    output.AppendLine(ErrorMessage);
                    continue;
                }
                Vet vet = context.Vets.SingleOrDefault(v => v.Name == procedureDto.VetName);
                if (vet == null)
                {
                    output.AppendLine(ErrorMessage);
                    continue;
                }
                Animal animal = context.Animals.SingleOrDefault(a
                                                                => a.PassportSerialNumber == procedureDto.AnimalPassportSerialNumber);
                if (animal == null)
                {
                    output.AppendLine(ErrorMessage);
                    continue;
                }
                Procedure procedure = new Procedure()
                {
                    Vet      = vet,
                    Animal   = animal,
                    DateTime = DateTime.ParseExact(procedureDto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture)
                };
                HashSet <ProcedureAnimalAid> procedureAnimalAids = new HashSet <ProcedureAnimalAid>();
                bool hasInvalidAnimalAids = false;
                foreach (var animalAidDto in procedureDto.AnimalAids)
                {
                    AnimalAid animalAid = context.AnimalAids.SingleOrDefault(aa => aa.Name == animalAidDto.Name);
                    if (animalAid == null || procedureAnimalAids.Any(paa => paa.AnimalAid.Name == animalAidDto.Name))
                    {
                        hasInvalidAnimalAids = true;
                        break;
                    }
                    else
                    {
                        ProcedureAnimalAid procedureAnimalAid = new ProcedureAnimalAid()
                        {
                            AnimalAid = animalAid,
                            Procedure = procedure
                        };
                        procedureAnimalAids.Add(procedureAnimalAid);
                    }
                }
                if (hasInvalidAnimalAids)
                {
                    output.AppendLine(ErrorMessage);
                    continue;
                }
                procedure.ProcedureAnimalAids = procedureAnimalAids;
                procedures.Add(procedure);
                output.AppendLine(String.Format(SuccessMessage, String.Empty));
            }
            context.Procedures.AddRange(procedures);
            context.SaveChanges();
            return(output.ToString().TrimEnd());
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var xDoc     = XDocument.Parse(xmlString);
            var elements = xDoc.Root.Elements();

            var sb           = new StringBuilder();
            var validEntries = new List <Procedure>();

            foreach (var el in elements)
            {
                var vetName        = el.Element("Vet")?.Value;
                var passportId     = el.Element("Animal")?.Value;
                var dateTimeString = el.Element("DateTime")?.Value;

                var vetId          = context.Vets.SingleOrDefault(v => v.Name == vetName)?.Id;
                var passportExists = context.Passports.Any(p => p.SerialNumber == passportId);

                var dateIsValid = DateTime
                                  .TryParseExact(dateTimeString, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTime);

                var animalAidElements = el.Element("AnimalAids")?.Elements();

                if (vetId == null || !passportExists || animalAidElements == null || !dateIsValid)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var animalAidIds = new List <int>();
                var allAidsExist = true;

                foreach (var aid in animalAidElements)
                {
                    var aidName = aid.Element("Name")?.Value;

                    var aidId = context.AnimalAids.SingleOrDefault(a => a.Name == aidName)?.Id;

                    if (aidId == null || animalAidIds.Any(id => id == aidId))
                    {
                        allAidsExist = false;
                        break;
                    }

                    animalAidIds.Add(aidId.Value);
                }

                if (!allAidsExist)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                var procedure = new Procedure
                {
                    VetId    = vetId.Value,
                    AnimalId = context.Animals.Single(a => a.PassportSerialNumber == passportId).Id,
                    DateTime = dateTime
                };

                foreach (var id in animalAidIds)
                {
                    var mapping = new ProcedureAnimalAid
                    {
                        Procedure   = procedure,
                        AnimalAidId = id
                    };

                    procedure.ProcedureAnimalAids.Add(mapping);
                }

                var isValid = IsValid(procedure);

                if (!isValid)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                validEntries.Add(procedure);
                sb.AppendLine("Record successfully imported.");
            }

            context.Procedures.AddRange(validEntries);
            context.SaveChanges();

            var result = sb.ToString().TrimEnd();

            return(result);
        }
Esempio n. 30
0
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var attr       = new XmlRootAttribute("Procedures");
            var serializer = new XmlSerializer(typeof(List <ProcedureImportDto>), attr);

            StringBuilder sb = new StringBuilder();

            var validProcedures = new List <Procedure>();

            using (StringReader reader = new StringReader(xmlString))
            {
                var proceduresDto = (List <ProcedureImportDto>)serializer.Deserialize(reader);

                foreach (var dto in proceduresDto)
                {
                    var vet = context
                              .Vets
                              .FirstOrDefault(v => v.Name == dto.Vet);

                    var animal = context
                                 .Animals
                                 .FirstOrDefault(a => a.PassportSerialNumber == dto.Animal);

                    var allAidsExists            = true;
                    var validProcedureAnimalAids = new List <ProcedureAnimalAid>();

                    foreach (var dtoAnimalAid in dto.AnimalAids)
                    {
                        var animalAid = context
                                        .AnimalAids
                                        .FirstOrDefault(aa => aa.Name == dtoAnimalAid.Name);
                        if (animalAid == null ||
                            validProcedureAnimalAids
                            .Any(paa => paa.AnimalAid.Name
                                 == dtoAnimalAid.Name))
                        {
                            allAidsExists = false;
                            break;
                        }

                        var animalAidProcedure = new ProcedureAnimalAid
                        {
                            AnimalAid = animalAid
                        };

                        validProcedureAnimalAids.Add(animalAidProcedure);
                    }

                    if (!IsValid(dto) ||
                        !dto.AnimalAids.All(IsValid) ||
                        vet == null ||
                        animal == null ||
                        !allAidsExists)
                    {
                        sb.AppendLine("Error: Invalid data.");
                        continue;
                    }

                    var procedure = new Procedure
                    {
                        Animal              = animal,
                        Vet                 = vet,
                        DateTime            = DateTime.ParseExact(dto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture),
                        ProcedureAnimalAids = validProcedureAnimalAids
                    };

                    validProcedures.Add(procedure);
                    sb.AppendLine($"Record successfully imported.");
                }
            }

            context.Procedures.AddRange(validProcedures);
            context.SaveChanges();

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