Skip to content

Commit

Permalink
update: method for getting city details from pincode
Browse files Browse the repository at this point in the history
  • Loading branch information
rzp-Piyush committed Nov 19, 2024
1 parent 78379db commit 2db1e83
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ func (r *CountrySubdivisions) GetStateByStateCode(code string) (State, bool) {
return State{}, false
}

func (r *CountrySubdivisions) GetCityByCityNameAndStateCode(cityName string, stateCode string) (City, bool) {
if state, exists := r.States[stateCode]; exists {
for _, city := range state.Cities {
if city.Name == cityName {
return city, true
}
}
}
return City{}, false
}

// GetCountrySubdivisions retrieves subdivision information for a specific country code.
func GetCountrySubdivisions(code string) CountrySubdivisions {
if _, present := countrySubDivisionStore[code]; present {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import (
)

type PinCodeDetails struct {
Cities []country_subdivisions.City
Cities []*CityDetails
StateCodes []string
}

type PinCodeData struct {
pinCodeToDetails map[string]PinCodeDetails
pinCodeToDetails map[string]*PinCodeDetails
cityToPinCodes map[string][]string
}
type CityDetails struct {
cityName string
stateCode string
}

var zipCodeStore = make(map[string]*PinCodeData)

Expand All @@ -38,7 +42,14 @@ func GetStatesFromZipCode(zipCode string, countryCode string) []country_subdivis
}
func GetCitiesFromZipCode(zipCode string, countryCode string) []country_subdivisions.City {
pinCodeData := GetCountryZipCodeDetails(countryCode)
return pinCodeData.pinCodeToDetails[zipCode].Cities
subdivision := country_subdivisions.GetCountrySubdivisions(countryCode)
var cities []country_subdivisions.City
for _, cityDetails := range pinCodeData.pinCodeToDetails[zipCode].Cities {
if city, exists := subdivision.GetCityByCityNameAndStateCode(cityDetails.cityName, cityDetails.stateCode); exists {
cities = append(cities, city)
}
}
return cities
}
func IsValidPinCode(zipCode string, countryCode string) bool {
pinCodeData := GetCountryZipCodeDetails(countryCode)
Expand All @@ -52,22 +63,25 @@ func GetPinCodesFromCity(city string, countryCode string) []string {
// initializeZipCodeMap builds the zip code maps for the given CountrySubdivisions.
func initializeZipCodeMap(subdivisions country_subdivisions.CountrySubdivisions) *PinCodeData {
var cityToZipCode = make(map[string][]string)
var details = make(map[string]PinCodeDetails)
var details = make(map[string]*PinCodeDetails)

// Iterate through all states and cities to populate the zip code maps.
for stateCode, state := range subdivisions.States {
for _, city := range state.Cities {
for _, zipcode := range city.Zipcodes {
// check if an entry with specific PinCode already exists, if not create one
if _, exists := details[zipcode]; !exists {
details[zipcode] = PinCodeDetails{
details[zipcode] = &PinCodeDetails{
StateCodes: []string{},
Cities: []country_subdivisions.City{},
Cities: []*CityDetails{},
}
}
pinCodeDetail := details[zipcode]
pinCodeDetail.StateCodes = append(pinCodeDetail.StateCodes, stateCode)
pinCodeDetail.Cities = append(pinCodeDetail.Cities, city)
pinCodeDetail.Cities = append(pinCodeDetail.Cities, &CityDetails{
cityName: city.Name,
stateCode: stateCode,
})
details[zipcode] = pinCodeDetail
}
cityToZipCode[city.Name] = city.Zipcodes
Expand Down

0 comments on commit 2db1e83

Please sign in to comment.