Esempio n. 1
0
        private async Task <ResponseObject> HousingCooperativesLookupAsync(HousingCooperativesJsonResponseModel housingCooperativesData)
        {
            var housingCooperatives = new List <HousingCooperative>();

            foreach (var item in housingCooperativesData.Items)
            {
                if (string.IsNullOrEmpty(item.Uuid))
                {
                    Query housingCooperativesQuery = collection
                                                     .WhereEqualTo("streetName", item.Vejnavn)
                                                     .WhereEqualTo("streetBuildingIdentifier", item.Husnummer)
                                                     .WhereEqualTo("postCodeIdentifier", item.Postnummer)
                                                     .WhereEqualTo("floorIdentifier", item.Etage)
                                                     .WhereEqualTo("sideDoor", item.Side);
                    QuerySnapshot housingCooperativesSnapshot = await housingCooperativesQuery.GetSnapshotAsync();

                    if (housingCooperativesSnapshot.Count > 0)
                    {
                        foreach (var i in housingCooperativesSnapshot.Documents)
                        {
                            housingCooperatives.Add(i.ConvertTo <HousingCooperative>());
                        }
                    }
                    else
                    {
                        var newHousingCooperative = new HousingCooperative()
                        {
                            MunicipalityCode         = item.Kommunenummer,
                            StreetName               = item.Vejnavn,
                            StreetCode               = item.Vejkode,
                            StreetBuildingIdentifier = item.Husnummer,
                            PostCodeIdentifier       = item.Postnummer,
                            DistrictName             = item.Kommunenavn,
                            FloorIdentifier          = item.Etage,
                            SuiteIdentifier          = item.Side
                        };
                        housingCooperatives.Add(newHousingCooperative);
                        await collection.Document().SetAsync(newHousingCooperative);
                    }
                }
                else
                {
                    var newHousingCooperativeUuid = item.Uuid;
                    var document = await collection.Document(newHousingCooperativeUuid).GetSnapshotAsync();

                    if (document.Exists)
                    {
                        housingCooperatives.Add(document.ConvertTo <HousingCooperative>());
                    }
                    else
                    {
                        string bulletinUri = $"https://www.tinglysning.dk/tinglysning/ssl/andelsbolig/andelsbolig/{newHousingCooperativeUuid}";
                        string newHousingCooperativeDataXml = HttpWebHandler.DoSSLGet(bulletinUri);

                        var newHousingCooperativeData = XmlSerializerHelper <AndelSummariskHentResultat> .Deserialaize(newHousingCooperativeDataXml);

                        if (newHousingCooperativeData != null)
                        {
                            var newHousingCooperative = new HousingCooperative()
                            {
                                Uuid                     = newHousingCooperativeUuid,
                                MunicipalityCode         = newHousingCooperativeData.AndelSummarisk.AndelStamoplysninger.AndelIdentifikator.MunicipalityCode,
                                StreetName               = newHousingCooperativeData.AndelSummarisk.AndelStamoplysninger.AdresseStruktur.StreetName,
                                StreetCode               = newHousingCooperativeData.AndelSummarisk.AndelStamoplysninger.AndelIdentifikator.StreetCode,
                                StreetBuildingIdentifier = newHousingCooperativeData.AndelSummarisk.AndelStamoplysninger.AndelIdentifikator.StreetBuildingIdentifier,
                                PostCodeIdentifier       = newHousingCooperativeData.AndelSummarisk.AndelStamoplysninger.AdresseStruktur.PostCodeIdentifier,
                                DistrictName             = newHousingCooperativeData.AndelSummarisk.AndelStamoplysninger.AdresseStruktur.DistrictName,
                                FloorIdentifier          = newHousingCooperativeData.AndelSummarisk.AndelStamoplysninger.AndelIdentifikator.FloorIdentifier,
                                SuiteIdentifier          = newHousingCooperativeData.AndelSummarisk.AndelStamoplysninger.AndelIdentifikator.SideDoerTekst
                            };

                            await collection.Document(newHousingCooperativeUuid).SetAsync(newHousingCooperative);

                            housingCooperatives.Add(newHousingCooperative);
                        }
                    }
                }
            }
            return(new ResponseObject()
            {
                ResponseStatus = housingCooperatives.Count > 0 ? ResponseStatus.OK : ResponseStatus.NOT_FOUND,
                Content = JsonConvert.SerializeObject(housingCooperatives)
            });
        }