Skip to content

Commit

Permalink
feat: add search, implement autosuggest endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rkettelerij committed Nov 29, 2024
1 parent 960d27d commit 2163e3f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"

"github.com/PDOK/gomagpie/config"
"github.com/PDOK/gomagpie/internal/search"
"github.com/iancoleman/strcase"

eng "github.com/PDOK/gomagpie/internal/engine"
Expand Down Expand Up @@ -172,6 +173,8 @@ func main() {
}
// Each OGC API building block makes use of said Engine
ogc.SetupBuildingBlocks(engine, dbConn)
// Start search logic
search.NewSearch(engine)

return engine.Start(address, debugPort, shutdownDelay)
},
Expand Down
2 changes: 0 additions & 2 deletions internal/ogc/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ func SetupBuildingBlocks(engine *engine.Engine, _ string) {
if engine.Config.HasCollections() {
geospatial.NewCollections(engine)
}

// TODO Something with the dbConnString param in PDOK-17118
}
Empty file removed internal/search/.keep
Empty file.
68 changes: 68 additions & 0 deletions internal/search/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package search

import (
"log"
"net/http"
"net/url"
"strings"

"github.com/PDOK/gomagpie/internal/engine"
)

type Search struct {
engine *engine.Engine
}

func NewSearch(e *engine.Engine) *Search {
s := &Search{
engine: e,
}
e.Router.Get("/search/suggest", s.Suggest())
return s
}

// Suggest autosuggest locations based on user input
func (s *Search) Suggest() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
params, err := parseQueryParams(r.URL.Query())
if err != nil {
log.Printf("%v", err)
engine.RenderProblem(engine.ProblemBadRequest, w)
return
}
searchQuery := params["q"]
delete(params, "q")
format := params["f"]
delete(params, "f")
crs := params["crs"]
delete(params, "crs")

log.Printf("crs %s, format %s, query %s, params %v", crs, format, searchQuery, params)
}
}

func parseQueryParams(query url.Values) (map[string]any, error) {

Check failure on line 44 in internal/search/main.go

View workflow job for this annotation

GitHub Actions / lint

parseQueryParams - result 1 (error) is always nil (unparam)
result := make(map[string]any, len(query))

deepObjectParams := make(map[string]map[string]string)
for key, values := range query {
if strings.Contains(key, "[") {
// Extract deepObject parameters
parts := strings.SplitN(key, "[", 2)
mainKey := parts[0]
subKey := strings.TrimSuffix(parts[1], "]")

if _, exists := deepObjectParams[mainKey]; !exists {
deepObjectParams[mainKey] = make(map[string]string)
}
deepObjectParams[mainKey][subKey] = values[0]
} else {
// Extract regular (flat) parameters
result[key] = values[0]
}
}
for mainKey, subParams := range deepObjectParams {
result[mainKey] = subParams
}
return result, nil
}

0 comments on commit 2163e3f

Please sign in to comment.