Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions backend/main/models/community.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions backend/main/server/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions backend/main/server/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion backend/main/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")

}
2 changes: 2 additions & 0 deletions backend/migrations/000040_add_tigram_extension.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP EXTENSION IF EXISTS pg_trgm;
DROP INDEX IF EXISTS trgm_idx;
2 changes: 2 additions & 0 deletions backend/migrations/000040_add_tigram_extension.up.sql
Original file line number Diff line number Diff line change
@@ -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);