Beispiel #1
0
func addCityToIndex(
	bucket *bolt.Bucket, id string, name string, locale string, population uint32,
) error {
	var err error
	var cityName *ds.CityName

	if locale == "" {
		locale = "en"
	}

	cityNameKey := []byte(ds.PrepareCityNameKey(name))
	if conflict := bucket.Get(cityNameKey); conflict != nil {
		cityName, err = ds.CityNameFromString(string(cityNameKey), string(conflict))
		if strconv.Itoa(cityName.CityId) != id {
			cityNameKey = []byte(string(cityNameKey) + "|" + id)
		}
	}

	err = bucket.Put(
		cityNameKey, []byte(
			name+"\t"+id+"\t"+locale+"\t"+strconv.Itoa(int(population)),
		),
	)

	return err
}
Beispiel #2
0
func MakeCitiesEndpoint(
	db *bolt.DB, options *config.Options, cache *cache.Cache,
) func(*gin.Context) {
	return func(c *gin.Context) {
		query := ds.PrepareCityNameKey(c.Query("q"))

		if query == "" {
			c.JSON(200, nil)
		} else {
			cities, err := ds.CachedCitiesSearch(db, cache, options.Locales, query, 5)

			if err != nil {
				c.JSON(500, nil)
			} else {
				c.JSON(200, cities.ForSerialization(c.Request.URL, options))
			}
		}
	}
}