diff --git a/backend/main/models/community.go b/backend/main/models/community.go index 6f4a43070..e767bc8ac 100644 --- a/backend/main/models/community.go +++ b/backend/main/models/community.go @@ -249,8 +249,7 @@ func (c *Community) CreateCommunity(db *s.Database) error { func (c *Community) UpdateCommunity(db *s.Database, p *UpdateCommunityRequestPayload) error { _, err := db.Conn.Exec( db.Context, - ` - UPDATE communities + `UPDATE communities SET name = COALESCE($1, name), body = COALESCE($2, body), logo = COALESCE($3, logo), @@ -308,6 +307,32 @@ func (c *Community) CanUpdateCommunity(db *s.Database, addr string) error { return nil } +func SearchForCommunity(db *s.Database, query string) ([]Community, error) { + var communities []Community + rows, err := db.Conn.Query( + db.Context, + `SELECT name FROM communities WHERE SIMILARITY(name, $1) > 0.1`, + query, + ) + if err != nil { + return communities, fmt.Errorf("error searching for a community with the the param %s", query) + } + + defer rows.Close() + + for rows.Next() { + var c Community + err = rows.Scan(&c.Name) + if err != nil { + return communities, fmt.Errorf("error scanning community row: %v", err) + } + communities = append(communities, c) + } + + fmt.Printf("communitites : %v", communities) + return communities, nil +} + func MatchStrategyByProposal(s []Strategy, strategyToMatch string) (Strategy, error) { var match Strategy for _, strategy := range s { diff --git a/backend/main/server/controllers.go b/backend/main/server/controllers.go index 73f13fd9f..e7368dd8c 100644 --- a/backend/main/server/controllers.go +++ b/backend/main/server/controllers.go @@ -283,7 +283,6 @@ func (a *App) updateProposal(w http.ResponseWriter, r *http.Request) { } // Communities - func (a *App) getCommunities(w http.ResponseWriter, r *http.Request) { pageParams := getPageParams(*r, 25) @@ -299,10 +298,19 @@ func (a *App) getCommunities(w http.ResponseWriter, r *http.Request) { respondWithJSON(w, http.StatusOK, response) } +func (a *App) searchCommunities(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + results, err := helpers.searchCommuntities(vars["query"]) + if err != nil { + respondWithError(w, http.StatusInternalServerError, err.Error()) + } + + respondWithJSON(w, http.StatusOK, results) +} + func (a *App) getCommunity(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id, err := strconv.Atoi(vars["id"]) - if err != nil { respondWithError(w, http.StatusBadRequest, "Invalid Community ID.") return diff --git a/backend/main/server/helpers.go b/backend/main/server/helpers.go index 3bb4d3748..8b4f725f6 100644 --- a/backend/main/server/helpers.go +++ b/backend/main/server/helpers.go @@ -447,6 +447,15 @@ func (h *Helpers) fetchCommunity(id int) (models.Community, int, error) { return community, http.StatusOK, nil } +func (h *Helpers) searchCommuntities(query string) (interface{}, error) { + results, err := models.SearchForCommunity(h.A.DB, query) + if err != nil { + return []models.Community{}, err + } + + return results, nil +} + func (h *Helpers) createProposal(p models.Proposal) (models.Proposal, int, error) { if p.Voucher != nil { if err := h.validateUserViaVoucher(p.Creator_addr, p.Voucher); err != nil { diff --git a/backend/main/server/routes.go b/backend/main/server/routes.go index c9d35b0ed..16ff410aa 100644 --- a/backend/main/server/routes.go +++ b/backend/main/server/routes.go @@ -13,6 +13,8 @@ func (a *App) initializeRoutes() { a.Router.HandleFunc("/communities/{id:[0-9]+}", a.updateCommunity).Methods("PATCH", "OPTIONS") a.Router.HandleFunc("/communities", a.createCommunity).Methods("POST", "OPTIONS") a.Router.HandleFunc("/communities/{communityId:[0-9]+}/strategies", a.getActiveStrategiesForCommunity).Methods("GET") + //Community Search + a.Router.HandleFunc("/communities/search/{query:[a-zA-Z0-9]+}", a.searchCommunities).Methods("GET") // Proposals a.Router.HandleFunc("/proposals/{id:[0-9]+}", a.getProposal).Methods("GET") a.Router.HandleFunc("/proposals/{id:[0-9]+}", a.updateProposal).Methods("PUT", "OPTIONS") @@ -54,5 +56,6 @@ func (a *App) initializeRoutes() { // Snapshotter a.Router.HandleFunc("/latest-snapshot", a.getLatestSnapshot).Methods("GET") - a.Router.HandleFunc("/add-fungible-token", a.addFungibleToken).Methods("POST","OPTIONS") + a.Router.HandleFunc("/add-fungible-token", a.addFungibleToken).Methods("POST", "OPTIONS") + } diff --git a/backend/migrations/000040_add_tigram_extension.down.sql b/backend/migrations/000040_add_tigram_extension.down.sql new file mode 100644 index 000000000..71e2b37ef --- /dev/null +++ b/backend/migrations/000040_add_tigram_extension.down.sql @@ -0,0 +1,2 @@ +DROP EXTENSION IF EXISTS pg_trgm; +DROP INDEX IF EXISTS trgm_idx; diff --git a/backend/migrations/000040_add_tigram_extension.up.sql b/backend/migrations/000040_add_tigram_extension.up.sql new file mode 100644 index 000000000..f7b451ec0 --- /dev/null +++ b/backend/migrations/000040_add_tigram_extension.up.sql @@ -0,0 +1,2 @@ +CREATE EXTENSION IF NOT EXISTS pg_trgm; +CREATE INDEX IF NOT EXISTS trgm_idx on communities using gin(name gin_trgm_ops);