SHARE
TWEET

Untitled

a guest Dec 5th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static string ImportProcedures(PetClinicContext context, string xmlString)
  2.         {
  3.             var xmlSerializer = new XmlSerializer(typeof(ProcedureImportDto[]), new XmlRootAttribute("Procedures"));
  4.             var procedureDtos = (ProcedureImportDto[])xmlSerializer.Deserialize(new StringReader(xmlString));
  5.  
  6.             var animals = context.Animals.ToHashSet();
  7.             var animalAids = context.AnimalAids.ToHashSet();
  8.             var vets = context.Vets.ToHashSet();
  9.             var proceduresList = new List<Procedure>();
  10.             var result = new StringBuilder();
  11.  
  12.             foreach (var dto in procedureDtos)
  13.             {
  14.                 if (IsValid(dto) == false)
  15.                 {
  16.                     result.AppendLine("Error: Invalid data.");
  17.                     continue;
  18.                 }
  19.  
  20.                 try
  21.                 {
  22.                     var procedure = new Procedure
  23.                     {
  24.                         Animal = animals.First(a => a.PassportSerialNumber == dto.AnimalSerialNumber),
  25.                         Vet = vets.First(v => v.Name == dto.VetName),
  26.                         DateTime = DateTime.ParseExact(dto.DateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture)
  27.                     };
  28.  
  29.                     foreach (var item in dto.AnimalAidNames)
  30.                     {
  31.                         var animalAidId = animalAids.First(a => a.Name == item.Name).Id;
  32.                         if (procedure.ProcedureAnimalAids.Any(paa => paa.AnimalAidId == animalAidId))
  33.                         {
  34.                             throw new Exception();
  35.                         }
  36.                         procedure.ProcedureAnimalAids.Add(new ProcedureAnimalAid { AnimalAidId = animalAidId });
  37.                     }
  38.  
  39.                     proceduresList.Add(procedure);
  40.                     result.AppendLine($"Record successfully imported.");
  41.                 }
  42.                 catch (Exception)
  43.                 {
  44.                     result.AppendLine("Error: Invalid data.");
  45.                     continue;
  46.                 }
  47.             }
  48.  
  49.             context.Procedures.AddRange(proceduresList);
  50.             context.SaveChanges();
  51.  
  52.             return result.ToString().Trim();
  53.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top