mirror of
https://github.com/GrassrootsEconomics/cic-dw.git
synced 2026-05-27 23:17:55 +02:00
add: (wip) balances query API
This commit is contained in:
28
internal/public/api.go
Normal file
28
internal/public/api.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package public
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/nleof/goyesql"
|
||||
)
|
||||
|
||||
type api struct {
|
||||
db *pgxpool.Pool
|
||||
q goyesql.Queries
|
||||
}
|
||||
|
||||
func InitPublicApi(e *echo.Echo, db *pgxpool.Pool, queries goyesql.Queries) {
|
||||
g := e.Group("/public")
|
||||
|
||||
g.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
c.Set("api", &api{
|
||||
db: db,
|
||||
q: queries,
|
||||
})
|
||||
return next(c)
|
||||
}
|
||||
})
|
||||
|
||||
g.GET("/balances/:address", handleBalancesQuery)
|
||||
}
|
||||
31
internal/public/balances.go
Normal file
31
internal/public/balances.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package public
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/georgysavva/scany/pgxscan"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type dbRes struct {
|
||||
TokenSymbol string `db:"token_symbol"`
|
||||
TokenAddress string `db:"token_address"`
|
||||
}
|
||||
|
||||
func handleBalancesQuery(c echo.Context) error {
|
||||
var (
|
||||
api = c.Get("api").(*api)
|
||||
data []dbRes
|
||||
)
|
||||
|
||||
rows, err := api.db.Query(context.Background(), api.q["all-known-tokens"], c.Param("address"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := pgxscan.ScanAll(&data, rows); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, data)
|
||||
}
|
||||
Reference in New Issue
Block a user