Skip to content

Commit e0a798e

Browse files
authored
[API-216] Migrate /v1/users/:userId/collectibles (#400)
Extremely basic endpoint. We're just fetching the raw data from the row and returning it back. Fun quirks: * I named the column `data` originally, so it's `data.data` in the response. * Similar to a few other endpoints, we don't 404 on not found, just return `null`. Can't guarantee client won't error if we break from that behavior.
1 parent dc8faf7 commit e0a798e

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

api/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ func NewApiServer(config config.Config) *ApiServer {
336336
g.Use("/users/:userId", app.requireUserIdMiddleware)
337337
g.Get("/users/:userId", app.v1User)
338338
g.Get("/users/:userId/challenges", app.v1UsersChallenges)
339+
g.Get("/users/:userId/collectibles", app.v1UsersCollectibles)
339340
g.Get("/users/:userId/comments", app.v1UsersComments)
340341
g.Get("/users/:userId/emails/:grantorUserId/key", app.v1UsersEmailsKey)
341342
g.Get("/users/:userId/followers", app.v1UsersFollowers)

api/v1_users_collectibles.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package api
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/gofiber/fiber/v2"
7+
"github.com/jackc/pgx/v5"
8+
)
9+
10+
type CollectiblesRow struct {
11+
Data json.RawMessage `json:"data"`
12+
}
13+
14+
func (app *ApiServer) v1UsersCollectibles(c *fiber.Ctx) error {
15+
userId := app.getUserId(c)
16+
17+
sql := `
18+
SELECT data FROM collectibles
19+
WHERE user_id = @userId
20+
`
21+
22+
var collectible CollectiblesRow
23+
24+
err := app.pool.QueryRow(c.Context(), sql, pgx.NamedArgs{
25+
"userId": userId,
26+
}).Scan(&collectible.Data)
27+
if err != nil {
28+
if err == pgx.ErrNoRows {
29+
return c.JSON(fiber.Map{
30+
"data": nil,
31+
})
32+
}
33+
return err
34+
}
35+
36+
return c.JSON(fiber.Map{
37+
"data": collectible,
38+
})
39+
}

api/v1_users_collectibles_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package api
2+
3+
import (
4+
"testing"
5+
6+
"api.audius.co/database"
7+
"api.audius.co/trashid"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestUsersCollectibles(t *testing.T) {
12+
app := emptyTestApp(t)
13+
14+
fixtures := database.FixtureMap{
15+
"users": {
16+
{
17+
"user_id": 1,
18+
},
19+
},
20+
"collectibles": {
21+
{
22+
"user_id": 1,
23+
"data": map[string]any{
24+
"1:::0x1234": map[string]any{},
25+
"order": []string{
26+
"1:::0x1234",
27+
},
28+
},
29+
},
30+
},
31+
}
32+
33+
database.Seed(app.writePool, fixtures)
34+
35+
{
36+
status, body := testGet(t, app, "/v1/users/"+trashid.MustEncodeHashID(1)+"/collectibles")
37+
assert.Equal(t, 200, status)
38+
39+
jsonAssert(t, body, map[string]any{
40+
"data.data.1:::0x1234": "{}",
41+
"data.data.order.#": 1,
42+
"data.data.order.0": "1:::0x1234",
43+
})
44+
}
45+
46+
{
47+
status, body := testGet(t, app, "/v1/users/"+trashid.MustEncodeHashID(2)+"/collectibles")
48+
assert.Equal(t, 200, status)
49+
50+
jsonAssert(t, body, map[string]any{
51+
"data": nil,
52+
})
53+
}
54+
}

database/seed.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,14 @@ var (
562562
"created_at": time.Now(),
563563
"updated_at": time.Now(),
564564
},
565+
"collectibles": {
566+
"user_id": nil,
567+
"data": nil,
568+
"blockhash": "block_abc123",
569+
"blocknumber": 101,
570+
"created_at": time.Now(),
571+
"updated_at": time.Now(),
572+
},
565573
}
566574
)
567575

0 commit comments

Comments
 (0)