Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Commit

Permalink
Add search endpoint (#609)
Browse files Browse the repository at this point in the history
* Add search endpoint

Signed-off-by: Andres Martinez Gotor <[email protected]>
  • Loading branch information
andresmgot authored Mar 11, 2019
1 parent a57e47c commit 1d33d76
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions cmd/chartsvc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,44 @@ func listChartsWithFilters(w http.ResponseWriter, req *http.Request, params Para
response.NewDataResponse(cl).Write(w)
}

// searchCharts returns the list of charts that matches the query param in any of these fields:
// - name
// - description
// - repository name
// - any keyword
// - any source
// - any maintainer name
func searchCharts(w http.ResponseWriter, req *http.Request, params Params) {
db, closer := dbSession.DB()
defer closer()

query := req.FormValue("q")
var charts []*models.Chart
conditions := bson.M{
"$or": []bson.M{
{"name": bson.M{"$regex": query}},
{"description": bson.M{"$regex": query}},
{"repo.name": bson.M{"$regex": query}},
{"keywords": bson.M{"$elemMatch": bson.M{"$regex": query}}},
{"sources": bson.M{"$elemMatch": bson.M{"$regex": query}}},
{"maintainers": bson.M{"$elemMatch": bson.M{"name": bson.M{"$regex": query}}}},
},
}
if params["repo"] != "" {
conditions["repo.name"] = params["repo"]
}
if err := db.C(chartCollection).Find(conditions).All(&charts); err != nil {
log.WithError(err).Errorf(
"could not find charts with the given query %s",
query,
)
// continue to return empty list
}

cl := newChartListResponse(uniqChartList(charts))
response.NewDataResponse(cl).Write(w)
}

func newChartResponse(c *models.Chart) *apiResponse {
latestCV := c.ChartVersions[0]
return &apiResponse{
Expand Down
2 changes: 2 additions & 0 deletions cmd/chartsvc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ func setupRoutes() http.Handler {
apiv1 := r.PathPrefix(pathPrefix).Subrouter()
apiv1.Methods("GET").Path("/charts").Queries("name", "{chartName}", "version", "{version}", "appversion", "{appversion}").Handler(WithParams(listChartsWithFilters))
apiv1.Methods("GET").Path("/charts").HandlerFunc(listCharts)
apiv1.Methods("GET").Path("/charts/search").Queries("q", "{query}").Handler(WithParams(searchCharts))
apiv1.Methods("GET").Path("/charts/{repo}").Handler(WithParams(listRepoCharts))
apiv1.Methods("GET").Path("/charts/{repo}/search").Queries("q", "{query}").Handler(WithParams(searchCharts))
apiv1.Methods("GET").Path("/charts/{repo}/{chartName}").Handler(WithParams(getChart))
apiv1.Methods("GET").Path("/charts/{repo}/{chartName}/versions").Handler(WithParams(listChartVersions))
apiv1.Methods("GET").Path("/charts/{repo}/{chartName}/versions/{version}").Handler(WithParams(getChartVersion))
Expand Down

0 comments on commit 1d33d76

Please sign in to comment.