Skip to content

Commit 1e4bd85

Browse files
committed
move query logic into queries folder
1 parent b66e943 commit 1e4bd85

6 files changed

Lines changed: 120 additions & 78 deletions

File tree

api/dbv1/full_account.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package dbv1
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
7+
"github.com/jackc/pgx/v5"
8+
)
9+
10+
type FullAccount struct {
11+
User FullUser `json:"user"`
12+
Playlists []FullAccountPlaylist `json:"playlists"`
13+
PlaylistLibrary json.RawMessage `json:"playlist_library"`
14+
TrackSaveCount int64 `json:"track_save_count"`
15+
}
16+
17+
func (q *Queries) FullAccount(ctx context.Context, wallet string) (*FullAccount, error) {
18+
// resolve wallet to user id
19+
userId, err := q.GetUserForWallet(ctx, wallet)
20+
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
users, err := q.FullUsers(ctx, GetUsersParams{
26+
Ids: []int32{userId},
27+
MyID: userId,
28+
})
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
if len(users) == 0 {
34+
// todo: better error? Need a 404 for this
35+
return nil, pgx.ErrNoRows
36+
}
37+
38+
playlists, err := q.FullAccountPlaylists(ctx, userId)
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
// Extract playlist_library from user record
44+
playlistLibrary := users[0].PlaylistLibrary
45+
trackSaveCount := users[0].TrackSaveCount
46+
// Create a copy of the user without playlist_library/track_save_count as
47+
// they are deprecated fields and we will return them as siblings
48+
userWithoutLibrary := users[0]
49+
userWithoutLibrary.PlaylistLibrary = nil
50+
userWithoutLibrary.TrackSaveCount = nil
51+
52+
return &FullAccount{
53+
User: userWithoutLibrary,
54+
Playlists: playlists,
55+
PlaylistLibrary: playlistLibrary,
56+
TrackSaveCount: *trackSaveCount,
57+
}, nil
58+
}

api/dbv1/get_account_playlists.sql.go

Lines changed: 23 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/dbv1/get_user_for_wallet.sql.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/dbv1/queries/get_account_playlists.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ WHERE (
3131
AND p.is_delete = FALSE
3232
AND p.playlist_id IN (SELECT save_item_id FROM saved_playlists))
3333
)
34-
ORDER BY created_at DESC;
34+
ORDER BY p.created_at DESC;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- name: GetUserForWallet :one
2+
SELECT user_id FROM users
3+
WHERE
4+
wallet = lower(@wallet)
5+
AND is_current = true
6+
ORDER BY handle IS NOT NULL, created_at ASC
7+
LIMIT 1;

api/v1_users_account.go

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,22 @@
11
package api
22

33
import (
4-
"bridgerton.audius.co/api/dbv1"
54
"github.com/gofiber/fiber/v2"
65
)
76

87
// todo: in python this route requires auth
98
func (app *ApiServer) v1UsersAccount(c *fiber.Ctx) error {
10-
// resolve wallet to user id
11-
var userId int32
12-
13-
// todo: this is a duplicate of the authMiddleware, make it common?
14-
err := app.pool.QueryRow(
15-
c.Context(),
16-
`
17-
SELECT user_id FROM users
18-
WHERE
19-
wallet = lower($1)
20-
AND is_current = true
21-
ORDER BY handle IS NOT NULL, created_at ASC
22-
LIMIT 1
23-
`,
24-
c.Params("wallet"),
25-
).Scan(&userId)
26-
27-
if err != nil {
28-
return err
9+
wallet := c.Params("wallet")
10+
if wallet == "" {
11+
return fiber.NewError(fiber.StatusBadRequest, "Missing wallet parameter")
2912
}
3013

31-
users, err := app.queries.FullUsers(c.Context(), dbv1.GetUsersParams{
32-
Ids: []int32{userId},
33-
MyID: userId,
34-
})
14+
account, err := app.queries.FullAccount(c.Context(), wallet)
3515
if err != nil {
3616
return err
3717
}
38-
if len(users) == 0 {
39-
return sendError(c, 404, "wallet not found")
40-
}
41-
42-
playlists, err := app.queries.FullAccountPlaylists(c.Context(), userId)
43-
if err != nil {
44-
return err
45-
}
46-
47-
// Extract playlist_library from user record
48-
playlistLibrary := users[0].PlaylistLibrary
49-
trackSaveCount := users[0].TrackSaveCount
50-
// Create a copy of the user without playlist_library/track_save_count as
51-
// they are deprecated fields and we will return them as siblings
52-
userWithoutLibrary := users[0]
53-
userWithoutLibrary.PlaylistLibrary = nil
54-
userWithoutLibrary.TrackSaveCount = nil
5518

56-
// this route does not have a non-full version...
57-
// and also nests user under data.user
58-
// and there are some additional fields
59-
// so we don't use the v1UserResponse helper
6019
return c.JSON(fiber.Map{
61-
"data": fiber.Map{
62-
"track_save_count": trackSaveCount,
63-
"playlists": playlists,
64-
"playlist_library": playlistLibrary,
65-
"user": userWithoutLibrary,
66-
},
20+
"data": account,
6721
})
6822
}

0 commit comments

Comments
 (0)