Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string ImportDistricts(CadastreContext dbContext, string xmlDocument)
- {
- // throw new NotImplementedException();
- StringBuilder sb = new StringBuilder();
- List<District> ListDistrict = new List<District>();
- //
- XmlSerializer xmlSerializer =
- new XmlSerializer(typeof(ImportDistrictDto[]), new XmlRootAttribute("Districts"));
- using StringReader stringReader = new StringReader(xmlDocument);
- ImportDistrictDto[] DistrictsDtos = (ImportDistrictDto[])xmlSerializer.Deserialize(stringReader);
- //
- foreach (var DistrictDto in DistrictsDtos)
- {
- if (!IsValid(DistrictDto))
- {
- sb.AppendLine(ErrorMessage);
- continue;
- }
- //same name
- if ( dbContext.Districts.Any(d=> d.Name== DistrictDto.Name))
- {
- sb.AppendLine(ErrorMessage);
- continue;
- }
- District district = new District()
- {
- Name = DistrictDto.Name,
- PostalCode = DistrictDto.PostalCode,
- Region = (Region) Enum.Parse(typeof(Region), DistrictDto.Region)
- };
- foreach (var prop in DistrictDto.Properties)
- {
- if (!IsValid(prop))
- {
- continue;
- }
- DateTime Date1;
- string Date2 = prop.DateOfAcquisition;
- if (!DateTime.TryParseExact(prop.DateOfAcquisition, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out Date1)){
- sb.AppendLine(ErrorMessage);
- continue;
- }
- // var DistrictID = dbContext.Districts.FirstOrDefault(p => p.Name == DistrictDto.Name).Id;
- if (dbContext.Properties.Any(p => p.PropertyIdentifier == prop.PropertyIdentifier) || district.Properties.Any(dp => dp.PropertyIdentifier == prop.PropertyIdentifier))
- {
- sb.AppendLine(ErrorMessage);
- continue;
- }
- if (dbContext.Properties.Any(p => p.Address == prop.Address) || district.Properties.Any(dp => dp.Address == prop.Address))
- {
- sb.AppendLine(ErrorMessage);
- continue;
- }
- Property p = new Property()
- {
- PropertyIdentifier = prop.PropertyIdentifier,
- Area = prop.Area,
- Details = prop.Details,
- Address = prop.Address,
- DateOfAcquisition = Date1
- };
- district.Properties.Add(p);
- }
- ListDistrict.Add(district);
- sb.AppendLine(String.Format(SuccessfullyImportedDistrict, DistrictDto.Name, district.Properties.Count));
- }
- dbContext.Districts.AddRange(ListDistrict);
- dbContext.SaveChanges();
- return sb.ToString();
- }
- public static string ImportCitizens(CadastreContext dbContext, string jsonDocument)
- {
- StringBuilder sb = new StringBuilder();
- List<Citizen> ListCitizen = new List<Citizen>();
- ImportCitizenDto[] CitizenDtos = JsonConvert.DeserializeObject<ImportCitizenDto[]>(jsonDocument);
- //
- foreach (var citizenDto in CitizenDtos)
- {
- if (!IsValid(citizenDto)){
- sb.AppendLine(ErrorMessage);
- continue;
- }
- if(citizenDto.MaritalStatus !="Unmarried" && citizenDto.MaritalStatus != "Married" &&
- citizenDto.MaritalStatus != "Divorced" && citizenDto.MaritalStatus != "Widowed")
- {
- sb.AppendLine(ErrorMessage);
- continue;
- }
- DateTime dt1;
- if (!DateTime.TryParseExact(citizenDto.BirthDate, "dd-MM-yyyy", CultureInfo.InvariantCulture,
- DateTimeStyles.None,out dt1)){
- sb.AppendLine(ErrorMessage);
- continue;
- }
- Citizen cit = new Citizen()
- {
- FirstName = citizenDto.FirstName,
- LastName = citizenDto.LastName,
- BirthDate = dt1,
- MaritalStatus = (MaritalStatus)Enum.Parse(typeof(MaritalStatus), citizenDto.MaritalStatus)
- };
- var AllIsdProp = dbContext.Properties.Select(p => p.Id).ToArray();
- // var CitizenId = dbContext.Citizens.FirstOrDefault(c => c.FirstName == citizenDto.FirstName && c.LastName == citizenDto.LastName).Id;
- foreach (var prop in citizenDto.IdProperties.Distinct())
- {
- if (!AllIsdProp.Contains(prop))
- {
- sb.AppendLine(ErrorMessage);
- continue;
- }
- cit.PropertiesCitizens.Add(new PropertyCitizen()
- {
- Citizen = cit,
- PropertyId = prop
- });
- }
- ListCitizen.Add(cit);
- sb.AppendLine(String.Format(SuccessfullyImportedCitizen, citizenDto.FirstName, citizenDto.LastName, cit.PropertiesCitizens.Count));
- }
- dbContext.Citizens.AddRange(ListCitizen);
- dbContext.SaveChanges();
- return sb.ToString();
- //throw new NotImplementedException();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement