Skip to content

Instantly share code, notes, and snippets.

@AlBannaTechno
Created September 6, 2021 13:55
Show Gist options
  • Save AlBannaTechno/f37a249ba36a1aa88b72bc615cda6df5 to your computer and use it in GitHub Desktop.
Save AlBannaTechno/f37a249ba36a1aa88b72bc615cda6df5 to your computer and use it in GitHub Desktop.
eXtra_Mapping
public async Task<GetAddressDetailMappingResponse> GetMappingDetails(GetAddressMappingData data)
{
#region Predefined
var ksaCountry = new AddressDetailMappingViewModel()
{
DetailId = Guid.Parse("33B45801-854A-42F8-B842-2D1BF42802FB"),
DetailMasterCodeid = Constants.MasterCodesID.COUNTRY,
DetailName = "Saudi Arabia",
DetailNameAR = "المملكة العربية السعودية"
};
#endregion
#region Check For Area City Combination Existence In Siebel To Google Mapping Table
var mappedCityWithArea = await _siebelGoogleMappingRepository.Get(null)
.FirstOrDefaultAsync(d =>
(d.CityGoogleAr.ToLower() == data.AreaName.ToLower() ||
d.CityGoogleEn.ToLower() == data.CityName.ToLower())
&&
(d.AreaGoogleAr.ToLower() == data.AreaName.ToLower() ||
d.AreaGoogleEn.ToLower() == data.AreaName.ToLower())
);
#endregion
#region City/Area Compination Not Existed
if (mappedCityWithArea == null)
{
#region Log Record
_siebelGoogleMappingMissingLogsService.CreateMissedMapping(new CreateSiebelGoogleMappingMissingLogDto
{
Country = data.CountryName ?? ksaCountry.DetailName,
City = data.CityName,
Area = null,
CountryDetailId = ksaCountry.DetailId,
});
#endregion
#region Return First Matched City In The Mapping Table
var city = await _siebelGoogleMappingRepository.Get(null).FirstOrDefaultAsync(d =>
d.CityGoogleAr.ToLower() == data.CityName.ToLower() ||
d.CityGoogleEn.ToLower() == data.CityName.ToLower());
return new GetAddressDetailMappingResponse
{
Country = ksaCountry,
City = city == null
? null
: new AddressDetailMappingViewModel
{
DetailMasterCodeid = Constants.MasterCodesID.CITY,
DetailId = city.CityId,
DetailNameAR = city.CityAr,
DetailName = city.CityEn,
MappedNameAR = city.CityGoogleAr,
MappedNameEN = city.CityGoogleEn,
NameToSearch = city.CityGoogleEn.ToLower(),
Covered = true
}
};
#endregion
}
#endregion
#region City/Area Compinate Exit
else
{
#region If area name not exist, return all areas related to this city
if (string.IsNullOrWhiteSpace(data.AreaName))
{
var areas = await _siebelGoogleMappingRepository.Get(null)
.Where(mp => mp.CityId == mappedCityWithArea.CityId)
.ToListAsync();
#region Log If No Result
if (areas.Count == 0)
{
_siebelGoogleMappingMissingLogsService.CreateMissedMapping(new CreateSiebelGoogleMappingMissingLogDto
{
Country = data.CountryName,
City = data.CityName,
Area = data.AreaName,
CountryDetailId = ksaCountry.DetailId,
});
}
#endregion
return new GetAddressDetailMappingResponse
{
Country = ksaCountry,
City = new AddressDetailMappingViewModel
{
DetailMasterCodeid = Constants.MasterCodesID.CITY,
DetailId = mappedCityWithArea.CityId,
DetailNameAR = mappedCityWithArea.CityAr,
DetailName = mappedCityWithArea.CityEn,
MappedNameAR = mappedCityWithArea.CityGoogleAr,
MappedNameEN = mappedCityWithArea.CityGoogleEn,
NameToSearch = mappedCityWithArea.CityGoogleEn.ToLower(),
Covered = true
},
Areas = areas.Select(mp => new AddressDetailMappingViewModel
{
DetailMasterCodeid = Constants.MasterCodesID.AREA,
DetailId = mp.AreaId,
DetailNameAR = mp.AreaAr,
DetailName = mp.AreaEn,
MappedNameAR = mp.AreaGoogleAr,
MappedNameEN = mp.AreaGoogleEn,
Covered = true
}).ToList()
};
}
#endregion
#region If area name exist, filter with.
else
{
var mappedCitiesWithAreas = await _siebelGoogleMappingRepository.Get(null)
.Where(d =>
(d.CityGoogleAr.ToLower() == data.AreaName.ToLower() ||
d.CityGoogleEn.ToLower() == data.CityName.ToLower())
&&
(d.AreaGoogleAr.ToLower() == data.AreaName.ToLower() ||
d.AreaGoogleEn.ToLower() == data.AreaName.ToLower())
).ToListAsync();
#region Log If No Result
if (mappedCitiesWithAreas.Count == 0)
{
_siebelGoogleMappingMissingLogsService.CreateMissedMapping(new CreateSiebelGoogleMappingMissingLogDto
{
Country = data.CountryName,
City = data.CityName,
Area = data.AreaName,
CountryDetailId = ksaCountry.DetailId,
});
}
#endregion
return new GetAddressDetailMappingResponse
{
Country = ksaCountry,
City = new AddressDetailMappingViewModel
{
DetailMasterCodeid = Constants.MasterCodesID.CITY,
DetailId = mappedCityWithArea.CityId,
DetailNameAR = mappedCityWithArea.CityAr,
DetailName = mappedCityWithArea.CityEn,
MappedNameAR = mappedCityWithArea.CityGoogleAr,
MappedNameEN = mappedCityWithArea.CityGoogleEn,
NameToSearch = mappedCityWithArea.CityGoogleEn.ToLower(),
Covered = true
},
Areas = mappedCitiesWithAreas.Select(mp => new AddressDetailMappingViewModel
{
DetailMasterCodeid = Constants.MasterCodesID.AREA,
DetailId = mp.AreaId,
DetailNameAR = mp.AreaAr,
DetailName = mp.AreaEn,
MappedNameAR = mp.AreaGoogleAr,
MappedNameEN = mp.AreaGoogleEn,
Covered = true
}).ToList()
};
}
#endregion
}
#endregion
}
@AAboelyazeed
Copy link

public async Task GetMappingDetails(GetAddressMappingData data)
{
var result = new GetAddressDetailMappingResponse()
{
Country = new AddressDetailMappingViewModel()
{
DetailId = Guid.Parse("33B45801-854A-42F8-B842-2D1BF42802FB"),
DetailMasterCodeid = Constants.MasterCodesID.COUNTRY,
DetailName = "Saudi Arabia",
DetailNameAR = "المملكة العربية السعودية"
}
};
if (string.IsNullOrEmpty(data.CityName))
{
return result;
}

        var mappingRecord = _siebelGoogleMappingRepository.Get(null).FirstOrDefault(m =>
          (m.CityGoogleEn.ToLower() == data.CityName.ToLower() || m.CityGoogleAr == data.CityName) &&
          (m.AreaGoogleEn.ToLower() == data.AreaName.ToLower() || m.AreaGoogleAr == data.AreaName));

        if (mappingRecord != null)
        {

            result.City = new AddressDetailMappingViewModel
            {
                DetailMasterCodeid = Constants.MasterCodesID.CITY,
                DetailId = mappingRecord.CityId,
                DetailNameAR = mappingRecord.CityAr,
                DetailName = mappingRecord.CityEn,
                MappedNameAR = mappingRecord.CityGoogleAr,
                MappedNameEN = mappingRecord.CityGoogleEn,
                NameToSearch = mappingRecord.CityGoogleEn.ToLower(),
                Covered = true
            };
            result.Areas = new List<AddressDetailMappingViewModel>()
                {
                    new AddressDetailMappingViewModel
                    {
                        DetailMasterCodeid = Constants.MasterCodesID.AREA,
                        DetailId = mappingRecord.AreaId,
                        DetailNameAR = mappingRecord.AreaAr,
                        DetailName = mappingRecord.AreaEn,
                        MappedNameAR = mappingRecord.AreaGoogleAr,
                        MappedNameEN = mappingRecord.AreaGoogleEn,
                        Covered = true
                    }
                };

            return result;
        }

        var cityMapping = _siebelGoogleMappingRepository.Get(null).FirstOrDefault(m =>
            (m.CityGoogleEn.ToLower() == data.CityName.ToLower() || m.CityGoogleAr == data.CityName));
        if (cityMapping == null)
        {
            _siebelGoogleMappingMissingLogsService.CreateMissedMapping(new CreateSiebelGoogleMappingMissingLogDto
            {
                Country = data.CountryName ?? result.Country.DetailName,
                City = data.CityName,
                Area = data.AreaName,
                CountryDetailId = result.Country.DetailId,
            });

            return result;
        }
        result.City = new AddressDetailMappingViewModel
        {
            DetailMasterCodeid = Constants.MasterCodesID.CITY,
            DetailId = cityMapping.CityId,
            DetailNameAR = cityMapping.CityAr,
            DetailName = cityMapping.CityEn,
            MappedNameAR = cityMapping.CityGoogleAr,
            MappedNameEN = cityMapping.CityGoogleEn,
            NameToSearch = cityMapping.CityGoogleEn.ToLower(),
            Covered = true
        };

        result.Areas = _scDetailCodeRepository.Get(null).Where(d =>
        (d.IsActive || !d.IsDeleted) &&
        d.MasterCodeId == Constants.MasterCodesID.AREA &&
        d.ParentId == cityMapping.CityId)
            .Select(d => new AddressDetailMappingViewModel()
            {
                DetailMasterCodeid = Constants.MasterCodesID.CITY,
                DetailId = d.Id,
                DetailNameAR = d.NameAr,
                DetailName = d.NameEn,
                MappedNameAR = d.NameAr,
                MappedNameEN = d.NameEn,
                NameToSearch = data.AreaName,
                Covered = true
            }).ToList();

        if (result.Areas == null || result.Areas.Count == 0)
        {
            _siebelGoogleMappingMissingLogsService.CreateMissedMapping(new CreateSiebelGoogleMappingMissingLogDto
            {
                Country = data.CountryName ?? result.Country.DetailName,
                City = data.CityName,
                Area = data.AreaName,
                CountryDetailId = result.Country.DetailId,
                
            });

        }
        // Log area not found
        //  return all areas under the city 

        return result;

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment