예제 #1
0
        public static string ImportVets(PetClinicContext context, string xmlString)
        {
            List <string> phoneNumbersAvailable = context.Vets.Select(x => x.PhoneNumber).ToList();

            var serializer = new XmlSerializer(typeof(imp_xml_vetDto[]), new XmlRootAttribute("Vets"));

            var vetsDtos = (imp_xml_vetDto[])serializer.Deserialize(new StringReader(xmlString));

            StringBuilder sb = new StringBuilder();

            List <Vet> vetsToBeAdded = new List <Vet>();

            foreach (var dto in vetsDtos)
            {
                if (AttributeValidator.IsValid(dto) && phoneNumbersAvailable.All(x => x != dto.PhoneNumber))
                {
                    phoneNumbersAvailable.Add(dto.PhoneNumber);
                    Vet newVet = Mapper.Map <Vet>(dto);
                    vetsToBeAdded.Add(newVet);
                    sb.AppendLine($"Record {newVet.Name} successfully imported.");
                    continue;
                }
                sb.AppendLine(invalidEntryMessage);
            }

            context.Vets.AddRange(vetsToBeAdded);
            context.SaveChanges();
            return(sb.ToString().Trim());
        }
예제 #2
0
        public static string ImportAnimalAids(PetClinicContext context, string jsonString)
        {
            AnimalAid[]   animalAids = JsonConvert.DeserializeObject <AnimalAid[]>(jsonString);
            StringBuilder sb         = new StringBuilder();

            List <AnimalAid> animalAidsToBeAdded = new List <AnimalAid>();

            foreach (var animalAid in animalAids)
            {
                if (AttributeValidator.IsValid(animalAid) && animalAidsToBeAdded.All(x => x.Name != animalAid.Name)
                    )
                {
                    animalAidsToBeAdded.Add(animalAid);
                    sb.AppendLine($"Record {animalAid.Name} successfully imported.");
                    continue;
                }
                sb.AppendLine(invalidEntryMessage);
            }
            context.AnimalAids.AddRange(animalAidsToBeAdded);
            context.SaveChanges();
            return(sb.ToString().Trim());
        }
예제 #3
0
        public static string ImportAnimals(PetClinicContext context, string jsonString)
        {
            var           animalsDTOs              = JsonConvert.DeserializeObject <imp_json_animalDto[]>(jsonString);
            StringBuilder sb                       = new StringBuilder();
            List <Animal> animalsToBeAdded         = new List <Animal>();
            List <string> availablePassportNumbers = context.Passports.Select(x => x.SerialNumber).ToList();

            foreach (var dto in animalsDTOs)
            {
                DateTime registrationDate;

                if (DateTime.TryParseExact(dto.Passport.RegistrationDate, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out registrationDate) &&
                    AttributeValidator.IsValid(dto) && AttributeValidator.IsValid(dto.Passport) && !availablePassportNumbers.Contains(dto.Passport.SerialNumber))
                {
                    var newAnimal = new Animal()
                    {
                        Name     = dto.Name,
                        Age      = dto.Age,
                        Type     = dto.Type,
                        Passport = new Passport()
                        {
                            SerialNumber     = dto.Passport.SerialNumber,
                            OwnerName        = dto.Passport.OwnerName,
                            OwnerPhoneNumber = dto.Passport.OwnerPhoneNumber,
                            RegistrationDate = registrationDate
                        }
                    };
                    animalsToBeAdded.Add(newAnimal);
                    availablePassportNumbers.Add(newAnimal.Passport.SerialNumber);
                    sb.AppendLine($"Record {newAnimal.Name} Passport №: {newAnimal.Passport.SerialNumber} successfully imported.");
                    continue;
                }
                sb.AppendLine(invalidEntryMessage);
            }
            context.Animals.AddRange(animalsToBeAdded);
            context.SaveChanges();
            return(sb.ToString().Trim());
        }