Ejemplo n.º 1
0
        public static string ImportAnimalAids(PetClinicContext context, string jsonString)
        {
            var animalAids = JsonConvert.DeserializeObject <AnimalAid[]>(jsonString);

            var validEntries = new List <AnimalAid>();

            var sb = new StringBuilder();

            foreach (var animalAid in animalAids)
            {
                var animalAidExists = validEntries.Any(a => a.Name == animalAid.Name);

                if (!IsValid(animalAid) || animalAidExists)
                {
                    sb.AppendLine(FailureMessage);
                    continue;
                }

                validEntries.Add(animalAid);
                sb.AppendLine(String.Format(SuccessMessage, animalAid.Name));
            }

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

            return(sb.ToString());
        }
        public static string ImportProcedures(PetClinicContext context, string xmlString)
        {
            var serializer = new XmlSerializer(typeof(List <ProcedureDto>), new XmlRootAttribute("Procedures"));

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

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

            foreach (var proc in deserializedProcedures)
            {
                var vetExists     = context.Vets.Any(v => v.Name == proc.VetName);
                var animalExists  = context.Animals.Any(a => a.PassportSerialNumber == proc.AnimalPassportNumber);
                var aidExists     = context.AnimalAids.Any(a => proc.AnimalAids.Any(aid => aid.Name == a.Name));
                var sameAidGivven = false;
                var validProc     = IsValid(proc);

                foreach (var aid in proc.AnimalAids)
                {
                    var current      = aid.Name;
                    var moreThanOnce = proc.AnimalAids.Where(a => a.Name == current).Count() > 1;

                    if (moreThanOnce)
                    {
                        sameAidGivven = true;
                    }
                }

                if (!vetExists || !animalExists || !aidExists || sameAidGivven || !validProc)
                {
                    sb.AppendLine($"Error: Invalid data.");
                    continue;
                }

                var procedure = new Procedure
                {
                    Vet                 = context.Vets.FirstOrDefault(v => v.Name == proc.VetName),
                    AnimalId            = context.Animals.FirstOrDefault(a => a.PassportSerialNumber == proc.AnimalPassportNumber).Id,
                    DateTime            = DateTime.ParseExact(proc.DateTime, "dd-MM-yyyy", null),
                    ProcedureAnimalAids = proc.AnimalAids.Select(x => new ProcedureAnimalAid
                    {
                        AnimalAid = context.AnimalAids.FirstOrDefault(a => a.Name == x.Name),
                        Procedure = context.Procedures.FirstOrDefault(p => p.Id == proc.Id)
                    }).ToList()
                };

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

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

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

            var deserializedAnimalDtos = JsonConvert.DeserializeObject <AnimalDto[]>(jsonString);

            var passports = new List <Passport>();
            var animals   = new List <Animal>();

            foreach (var animalDto in deserializedAnimalDtos)
            {
                var passportExists = passports.Any(p => p.SerialNumber.Equals(animalDto.Passport.SerialNumber));

                if (!IsValid(animalDto) || (!IsValid(animalDto.Passport)) || passportExists)
                {
                    sb.AppendLine(ERROR_MESSAGE);
                    continue;
                }

                var animal = new Animal
                {
                    Name = animalDto.Name,
                    Type = animalDto.Type,
                    Age  = animalDto.Age,
                    PassportSerialNumber = animalDto.Passport.SerialNumber
                };

                var registrationDate = DateTime.ParseExact(animalDto.Passport.RegistrationDate, "dd-MM-yyyy", CultureInfo.InvariantCulture);

                var passport = new Passport
                {
                    SerialNumber     = animalDto.Passport.SerialNumber,
                    Animal           = animal,
                    OwnerPhoneNumber = animalDto.Passport.OwnerPhoneNumber,
                    OwnerName        = animalDto.Passport.OwnerName,
                    RegistrationDate = registrationDate
                };

                animals.Add(animal);
                passports.Add(passport);

                sb.AppendLine($"Record {animal.Name} Passport №: {passport.SerialNumber} successfully imported.");
            }

            context.Animals.AddRange(animals);
            context.SaveChanges();

            context.AddRange(passports);
            context.SaveChanges();

            var result = sb.ToString();

            return(result);
        }
Ejemplo n.º 4
0
        public static string ImportVets(PetClinicContext context, string xmlString)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(VetDto[]), new XmlRootAttribute("Vets"));

            VetDto[]      vetDtos = (VetDto[])serializer.Deserialize(new StringReader(xmlString));
            StringBuilder sb      = new StringBuilder();
            List <Vet>    vets    = new List <Vet>();

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

                if (string.IsNullOrWhiteSpace(dto.PhoneNumber))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                if (vets.Any(v => v.PhoneNumber == dto.PhoneNumber))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                Vet vet = new Vet()
                {
                    Name        = dto.Name,
                    Profession  = dto.Profession,
                    Age         = dto.Age,
                    PhoneNumber = dto.PhoneNumber
                };

                vets.Add(vet);
                sb.AppendLine(string.Format(SuccessMessage, dto.Name));
            }

            context.AddRange(vets);
            context.SaveChanges();

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