b8be14ea8d
* Add a world cities database with type-ahead search on the Member Directory. * Users can search for a known city to order users by distance from that city rather than from their own configured location on their settings page. * Users must opt-in their own location before this feature may be used, in order to increase adoption of the location feature and to enforce fairness. * The `nonshy setup locations` command can import the world cities database.
35 lines
701 B
Go
35 lines
701 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
|
)
|
|
|
|
// WorldCities API searches the location database for a world city location.
|
|
func WorldCities() http.HandlerFunc {
|
|
type Result struct {
|
|
ID uint64 `json:"id"`
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var query = r.FormValue("query")
|
|
if query == "" {
|
|
SendRawJSON(w, http.StatusOK, []Result{})
|
|
return
|
|
}
|
|
|
|
result, err := models.SearchWorldCities(query)
|
|
if err != nil {
|
|
SendRawJSON(w, http.StatusInternalServerError, []Result{{
|
|
ID: 1,
|
|
Value: err.Error(),
|
|
}})
|
|
return
|
|
}
|
|
|
|
SendRawJSON(w, http.StatusOK, result)
|
|
})
|
|
}
|