Advertisement
Silviya7

Citizen

Aug 26th, 2024
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.06 KB | None | 0 0
  1.  public static string ImportDistricts(CadastreContext dbContext, string xmlDocument)
  2.         {
  3.             // throw new NotImplementedException();
  4.  
  5.             StringBuilder sb = new StringBuilder();
  6.             List<District> ListDistrict = new List<District>();
  7.  
  8.             //
  9.             XmlSerializer xmlSerializer =
  10.               new XmlSerializer(typeof(ImportDistrictDto[]), new XmlRootAttribute("Districts"));
  11.  
  12.             using StringReader stringReader = new StringReader(xmlDocument);
  13.             ImportDistrictDto[] DistrictsDtos = (ImportDistrictDto[])xmlSerializer.Deserialize(stringReader);
  14.  
  15.             //
  16.             foreach (var DistrictDto in DistrictsDtos)
  17.             {
  18.                 if (!IsValid(DistrictDto))
  19.                 {
  20.                     sb.AppendLine(ErrorMessage);
  21.                     continue;
  22.                 }
  23.  
  24.  
  25.                 //same name
  26.                 if ( dbContext.Districts.Any(d=> d.Name== DistrictDto.Name))
  27.                 {
  28.                     sb.AppendLine(ErrorMessage);
  29.                     continue;
  30.                 }
  31.  
  32.  
  33.                 District district = new District()
  34.                 {
  35.                     Name = DistrictDto.Name,
  36.                     PostalCode = DistrictDto.PostalCode,
  37.                     Region = (Region) Enum.Parse(typeof(Region), DistrictDto.Region)
  38.  
  39.  
  40.  
  41.                 };
  42.  
  43.                 foreach (var prop in DistrictDto.Properties)
  44.                 {
  45.                     if (!IsValid(prop))
  46.                     {
  47.                         continue;
  48.                     }
  49.  
  50.                     DateTime Date1;
  51.  
  52.                     string Date2 = prop.DateOfAcquisition;
  53.  
  54.                     if (!DateTime.TryParseExact(prop.DateOfAcquisition, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out Date1)){
  55.                         sb.AppendLine(ErrorMessage);
  56.                         continue;
  57.                     }
  58.  
  59.  
  60.                     // var DistrictID = dbContext.Districts.FirstOrDefault(p => p.Name == DistrictDto.Name).Id;
  61.  
  62.                     if (dbContext.Properties.Any(p => p.PropertyIdentifier == prop.PropertyIdentifier) || district.Properties.Any(dp => dp.PropertyIdentifier == prop.PropertyIdentifier))
  63.                     {
  64.                         sb.AppendLine(ErrorMessage);
  65.                         continue;
  66.                     }
  67.                     if (dbContext.Properties.Any(p => p.Address == prop.Address) || district.Properties.Any(dp => dp.Address == prop.Address))
  68.                     {
  69.                         sb.AppendLine(ErrorMessage);
  70.                         continue;
  71.                     }
  72.  
  73.  
  74.                     Property p = new Property()
  75.                     {
  76.                         PropertyIdentifier = prop.PropertyIdentifier,
  77.                         Area = prop.Area,
  78.                         Details = prop.Details,
  79.                         Address = prop.Address,
  80.                         DateOfAcquisition = Date1
  81.                      
  82.                     };
  83.  
  84.                     district.Properties.Add(p);
  85.                 }
  86.  
  87.  
  88.                 ListDistrict.Add(district);              
  89.                 sb.AppendLine(String.Format(SuccessfullyImportedDistrict, DistrictDto.Name, district.Properties.Count));
  90.             }
  91.  
  92.  
  93.  
  94.             dbContext.Districts.AddRange(ListDistrict);
  95.             dbContext.SaveChanges();
  96.             return sb.ToString();
  97.  
  98.  
  99.            
  100.  
  101.         }
  102.  
  103.         public static string ImportCitizens(CadastreContext dbContext, string jsonDocument)
  104.         {
  105.  
  106.  
  107.             StringBuilder sb = new StringBuilder();
  108.             List<Citizen> ListCitizen = new List<Citizen>();
  109.  
  110.             ImportCitizenDto[] CitizenDtos = JsonConvert.DeserializeObject<ImportCitizenDto[]>(jsonDocument);
  111.             //
  112.  
  113.             foreach (var citizenDto in CitizenDtos)
  114.             {
  115.                
  116.  
  117.                 if (!IsValid(citizenDto)){
  118.                     sb.AppendLine(ErrorMessage);
  119.                     continue;
  120.                 }
  121.                 if(citizenDto.MaritalStatus !="Unmarried" && citizenDto.MaritalStatus != "Married" &&
  122.                     citizenDto.MaritalStatus != "Divorced" && citizenDto.MaritalStatus != "Widowed")
  123.                 {
  124.                     sb.AppendLine(ErrorMessage);
  125.                     continue;
  126.                 }
  127.  
  128.                 DateTime dt1;
  129.  
  130.                 if (!DateTime.TryParseExact(citizenDto.BirthDate, "dd-MM-yyyy", CultureInfo.InvariantCulture,
  131.                     DateTimeStyles.None,out dt1)){
  132.                     sb.AppendLine(ErrorMessage);
  133.                     continue;
  134.                 }
  135.  
  136.                 Citizen cit = new Citizen()
  137.                 {
  138.                     FirstName = citizenDto.FirstName,
  139.                     LastName = citizenDto.LastName,
  140.                     BirthDate = dt1,
  141.                     MaritalStatus = (MaritalStatus)Enum.Parse(typeof(MaritalStatus), citizenDto.MaritalStatus)
  142.  
  143.                 };
  144.  
  145.  
  146.                 var AllIsdProp = dbContext.Properties.Select(p => p.Id).ToArray();
  147.                // var CitizenId = dbContext.Citizens.FirstOrDefault(c => c.FirstName == citizenDto.FirstName && c.LastName == citizenDto.LastName).Id;
  148.  
  149.                 foreach (var prop in citizenDto.IdProperties.Distinct())
  150.                 {
  151.                     if (!AllIsdProp.Contains(prop))
  152.                     {
  153.                         sb.AppendLine(ErrorMessage);
  154.                         continue;
  155.                     }
  156.                     cit.PropertiesCitizens.Add(new PropertyCitizen()
  157.                     {
  158.                         Citizen = cit,
  159.                         PropertyId = prop
  160.                     });
  161.  
  162.                 }
  163.  
  164.                 ListCitizen.Add(cit);
  165.                 sb.AppendLine(String.Format(SuccessfullyImportedCitizen, citizenDto.FirstName, citizenDto.LastName, cit.PropertiesCitizens.Count));
  166.             }
  167.  
  168.             dbContext.Citizens.AddRange(ListCitizen);
  169.             dbContext.SaveChanges();
  170.             return sb.ToString();
  171.  
  172.             //throw new NotImplementedException();
  173.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement