Skip to content

Commit 5a8f52b

Browse files
committed
implements user managers endpoint
1 parent 9936404 commit 5a8f52b

7 files changed

Lines changed: 274 additions & 11 deletions

File tree

api/dbv1/full_managers.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dbv1
2+
3+
import (
4+
"context"
5+
)
6+
7+
type FullGrant struct {
8+
GetGrantsForUserIdRow
9+
GranteeUserID *struct{} `json:"grantee_user_id,omitempty"`
10+
}
11+
12+
type FullManager struct {
13+
Manager FullUser `json:"manager"`
14+
Grant FullGrant `json:"grant"`
15+
}
16+
17+
func (q *Queries) FullManagers(ctx context.Context, params GetGrantsForUserIdParams) ([]FullManager, error) {
18+
19+
grants, err := q.GetGrantsForUserId(ctx, params)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
user_ids := make([]int32, len(grants))
25+
for i, grant := range grants {
26+
user_ids[i] = int32(grant.GranteeUserID)
27+
}
28+
29+
users, err := q.FullUsersKeyed(ctx, GetUsersParams{
30+
Ids: user_ids,
31+
MyID: params.UserID,
32+
})
33+
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
managers := make([]FullManager, len(grants))
39+
for i, grant := range grants {
40+
managers[i] = FullManager{
41+
Manager: users[int32(grant.GranteeUserID)],
42+
Grant: FullGrant{GetGrantsForUserIdRow: grant},
43+
}
44+
}
45+
46+
return managers, nil
47+
}

api/dbv1/get_grants.sql.go

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

api/dbv1/models.go

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

api/dbv1/queries/get_grants.sql

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
-- name: GetGrantsForUserId :many
2+
SELECT
3+
g.user_id,
4+
g.grantee_address,
5+
g.is_revoked,
6+
g.is_approved,
7+
g.created_at,
8+
g.updated_at,
9+
u.user_id as grantee_user_id
10+
FROM grants g
11+
JOIN users u ON u.wallet = g.grantee_address
12+
WHERE g.user_id = @user_id::int
13+
AND g.is_revoked = @is_revoked
14+
AND g.is_current = true
15+
AND sqlc.narg('is_approved')::boolean IS NULL OR g.is_approved = sqlc.narg('is_approved')
16+
ORDER BY g.created_at DESC;
17+
18+
-- name: GetGrantsForGranteeAddress :many
19+
SELECT
20+
g.user_id,
21+
g.grantee_address,
22+
g.is_revoked,
23+
g.is_approved,
24+
g.created_at,
25+
g.updated_at,
26+
u.user_id as grantee_user_id
27+
FROM grants g
28+
JOIN users u ON u.wallet = g.grantee_address
29+
WHERE g.grantee_address = @grantee_address
30+
AND g.is_current = true
31+
AND g.is_revoked = @is_revoked
32+
AND sqlc.narg('is_approved')::boolean IS NULL OR g.is_approved = sqlc.narg('is_approved')
33+
ORDER BY g.created_at DESC;

api/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ func NewApiServer(config config.Config) *ApiServer {
233233
g.Get("/users/:userId/following", app.v1UsersFollowing)
234234
g.Get("/users/:userId/library/tracks", app.v1UsersLibraryTracks)
235235
g.Get("/users/:userId/library/:playlistType", app.v1UsersLibraryPlaylists)
236+
g.Get("/users/:userId/managers", app.v1UsersManagers)
236237
g.Get("/users/:userId/mutuals", app.v1UsersMutuals)
237238
g.Get("/users/:userId/reposts", app.v1UsersReposts)
238239
g.Get("/users/:userId/related", app.v1UsersRelated)

api/v1_users_managers.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package api
2+
3+
import (
4+
"log"
5+
"strconv"
6+
7+
"bridgerton.audius.co/api/dbv1"
8+
"github.com/gofiber/fiber/v2"
9+
"github.com/jackc/pgx/v5/pgtype"
10+
)
11+
12+
func (app *ApiServer) v1UsersManagers(c *fiber.Ctx) error {
13+
// Behavior of this field is a little odd. We only want to filter by it
14+
// if it is passed, but otherwise not use a default value for either.
15+
var isApproved *bool
16+
if approvedStr := c.Query("is_approved"); approvedStr != "" {
17+
parsed, err := strconv.ParseBool(approvedStr)
18+
if err != nil {
19+
return fiber.NewError(fiber.StatusBadRequest, "Invalid value for is_approved")
20+
}
21+
isApproved = &parsed
22+
}
23+
24+
isRevoked := c.QueryBool("is_revoked", false)
25+
log.Printf("isApproved: %v, isRevoked: %v", isApproved, isRevoked)
26+
params := dbv1.GetGrantsForUserIdParams{
27+
UserID: int32(c.Locals("userId").(int)),
28+
IsApproved: pgtype.Bool{Bool: isApproved != nil && *isApproved, Valid: isApproved != nil},
29+
IsRevoked: isRevoked,
30+
}
31+
32+
managers, err := app.queries.FullManagers(c.Context(), params)
33+
if err != nil {
34+
return err
35+
}
36+
37+
return c.JSON(fiber.Map{
38+
"data": managers,
39+
})
40+
}

sqlc.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ sql:
4646
go_type:
4747
import: "bridgerton.audius.co/trashid"
4848
type: "HashId"
49+
- column: "grants.user_id"
50+
go_type:
51+
import: "bridgerton.audius.co/trashid"
52+
type: "HashId"
4953

5054
- column: "tracks.copyright_line"
5155
go_type:

0 commit comments

Comments
 (0)