Go · 5684 bytes Raw Blame History
1 // Code generated by sqlc. DO NOT EDIT.
2 // versions:
3 // sqlc v1.31.1
4 // source: stars.sql
5
6 package socialdb
7
8 import (
9 "context"
10
11 "github.com/jackc/pgx/v5/pgtype"
12 )
13
14 const countStargazersForRepo = `-- name: CountStargazersForRepo :one
15 SELECT COUNT(*) FROM stars s
16 JOIN users u ON u.id = s.user_id
17 WHERE s.repo_id = $1
18 AND u.suspended_at IS NULL
19 `
20
21 func (q *Queries) CountStargazersForRepo(ctx context.Context, db DBTX, repoID int64) (int64, error) {
22 row := db.QueryRow(ctx, countStargazersForRepo, repoID)
23 var count int64
24 err := row.Scan(&count)
25 return count, err
26 }
27
28 const countStarsForUser = `-- name: CountStarsForUser :one
29 SELECT COUNT(*) FROM stars s
30 JOIN repos r ON r.id = s.repo_id
31 WHERE s.user_id = $1
32 AND r.deleted_at IS NULL
33 `
34
35 func (q *Queries) CountStarsForUser(ctx context.Context, db DBTX, userID int64) (int64, error) {
36 row := db.QueryRow(ctx, countStarsForUser, userID)
37 var count int64
38 err := row.Scan(&count)
39 return count, err
40 }
41
42 const deleteStar = `-- name: DeleteStar :exec
43 DELETE FROM stars WHERE user_id = $1 AND repo_id = $2
44 `
45
46 type DeleteStarParams struct {
47 UserID int64
48 RepoID int64
49 }
50
51 func (q *Queries) DeleteStar(ctx context.Context, db DBTX, arg DeleteStarParams) error {
52 _, err := db.Exec(ctx, deleteStar, arg.UserID, arg.RepoID)
53 return err
54 }
55
56 const hasStar = `-- name: HasStar :one
57 SELECT EXISTS (
58 SELECT 1 FROM stars WHERE user_id = $1 AND repo_id = $2
59 ) AS has_star
60 `
61
62 type HasStarParams struct {
63 UserID int64
64 RepoID int64
65 }
66
67 func (q *Queries) HasStar(ctx context.Context, db DBTX, arg HasStarParams) (bool, error) {
68 row := db.QueryRow(ctx, hasStar, arg.UserID, arg.RepoID)
69 var has_star bool
70 err := row.Scan(&has_star)
71 return has_star, err
72 }
73
74 const insertStar = `-- name: InsertStar :exec
75
76 INSERT INTO stars (user_id, repo_id) VALUES ($1, $2)
77 ON CONFLICT (user_id, repo_id) DO NOTHING
78 `
79
80 type InsertStarParams struct {
81 UserID int64
82 RepoID int64
83 }
84
85 // ─── stars ─────────────────────────────────────────────────────────
86 // ON CONFLICT DO NOTHING is the idempotency guard: re-starring an
87 // already-starred repo doesn't double-increment the count (the
88 // AFTER INSERT trigger only fires on actual insert).
89 func (q *Queries) InsertStar(ctx context.Context, db DBTX, arg InsertStarParams) error {
90 _, err := db.Exec(ctx, insertStar, arg.UserID, arg.RepoID)
91 return err
92 }
93
94 const listStargazersForRepo = `-- name: ListStargazersForRepo :many
95 SELECT s.user_id, s.starred_at, u.username, u.display_name
96 FROM stars s
97 JOIN users u ON u.id = s.user_id
98 WHERE s.repo_id = $1
99 AND u.suspended_at IS NULL
100 ORDER BY s.starred_at DESC
101 LIMIT $2 OFFSET $3
102 `
103
104 type ListStargazersForRepoParams struct {
105 RepoID int64
106 Limit int32
107 Offset int32
108 }
109
110 type ListStargazersForRepoRow struct {
111 UserID int64
112 StarredAt pgtype.Timestamptz
113 Username string
114 DisplayName string
115 }
116
117 // Public-repo stargazer list. Paginated by `starred_at DESC`.
118 // Excludes suspended users so they don't taint public lists. The
119 // private-repo gate is at the handler layer (policy.IsVisibleTo).
120 func (q *Queries) ListStargazersForRepo(ctx context.Context, db DBTX, arg ListStargazersForRepoParams) ([]ListStargazersForRepoRow, error) {
121 rows, err := db.Query(ctx, listStargazersForRepo, arg.RepoID, arg.Limit, arg.Offset)
122 if err != nil {
123 return nil, err
124 }
125 defer rows.Close()
126 items := []ListStargazersForRepoRow{}
127 for rows.Next() {
128 var i ListStargazersForRepoRow
129 if err := rows.Scan(
130 &i.UserID,
131 &i.StarredAt,
132 &i.Username,
133 &i.DisplayName,
134 ); err != nil {
135 return nil, err
136 }
137 items = append(items, i)
138 }
139 if err := rows.Err(); err != nil {
140 return nil, err
141 }
142 return items, nil
143 }
144
145 const listStarsForUser = `-- name: ListStarsForUser :many
146 SELECT s.repo_id, s.starred_at,
147 r.name AS repo_name, r.description, r.visibility,
148 r.star_count, r.primary_language, r.updated_at,
149 r.owner_user_id, r.owner_org_id,
150 COALESCE(u.username, o.slug)::text AS owner_slug
151 FROM stars s
152 JOIN repos r ON r.id = s.repo_id
153 LEFT JOIN users u ON u.id = r.owner_user_id
154 LEFT JOIN orgs o ON o.id = r.owner_org_id
155 WHERE s.user_id = $1
156 AND r.deleted_at IS NULL
157 ORDER BY s.starred_at DESC
158 LIMIT $2 OFFSET $3
159 `
160
161 type ListStarsForUserParams struct {
162 UserID int64
163 Limit int32
164 Offset int32
165 }
166
167 type ListStarsForUserRow struct {
168 RepoID int64
169 StarredAt pgtype.Timestamptz
170 RepoName string
171 Description string
172 Visibility RepoVisibility
173 StarCount int64
174 PrimaryLanguage pgtype.Text
175 UpdatedAt pgtype.Timestamptz
176 OwnerUserID pgtype.Int8
177 OwnerOrgID pgtype.Int8
178 OwnerSlug string
179 }
180
181 // The "Stars" profile tab. The handler layer post-filters for repo
182 // visibility against the viewer; this query returns everything the
183 // user starred and lets the handler decide what to render. Sort axis
184 // is the spec's day-1 lean: most-recently-starred first.
185 func (q *Queries) ListStarsForUser(ctx context.Context, db DBTX, arg ListStarsForUserParams) ([]ListStarsForUserRow, error) {
186 rows, err := db.Query(ctx, listStarsForUser, arg.UserID, arg.Limit, arg.Offset)
187 if err != nil {
188 return nil, err
189 }
190 defer rows.Close()
191 items := []ListStarsForUserRow{}
192 for rows.Next() {
193 var i ListStarsForUserRow
194 if err := rows.Scan(
195 &i.RepoID,
196 &i.StarredAt,
197 &i.RepoName,
198 &i.Description,
199 &i.Visibility,
200 &i.StarCount,
201 &i.PrimaryLanguage,
202 &i.UpdatedAt,
203 &i.OwnerUserID,
204 &i.OwnerOrgID,
205 &i.OwnerSlug,
206 ); err != nil {
207 return nil, err
208 }
209 items = append(items, i)
210 }
211 if err := rows.Err(); err != nil {
212 return nil, err
213 }
214 return items, nil
215 }
216