aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/db/query.sql.go
blob: 1b9f39209274ca12b4e80ac0d5e29215bfaeffa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.26.0
// source: query.sql

package db

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const getGameById = `-- name: GetGameById :one
SELECT game_id, state, display_name, duration_seconds, created_at, started_at, games.problem_id, problems.problem_id, title, description FROM games
LEFT JOIN problems ON games.problem_id = problems.problem_id
WHERE games.game_id = $1
LIMIT 1
`

type GetGameByIdRow struct {
	GameID          int32
	State           string
	DisplayName     string
	DurationSeconds int32
	CreatedAt       pgtype.Timestamp
	StartedAt       pgtype.Timestamp
	ProblemID       pgtype.Int4
	ProblemID_2     pgtype.Int4
	Title           pgtype.Text
	Description     pgtype.Text
}

func (q *Queries) GetGameById(ctx context.Context, gameID int32) (GetGameByIdRow, error) {
	row := q.db.QueryRow(ctx, getGameById, gameID)
	var i GetGameByIdRow
	err := row.Scan(
		&i.GameID,
		&i.State,
		&i.DisplayName,
		&i.DurationSeconds,
		&i.CreatedAt,
		&i.StartedAt,
		&i.ProblemID,
		&i.ProblemID_2,
		&i.Title,
		&i.Description,
	)
	return i, err
}

const getUserAuthByUsername = `-- name: GetUserAuthByUsername :one
SELECT users.user_id, username, display_name, icon_path, is_admin, created_at, user_auth_id, user_auths.user_id, auth_type, password_hash FROM users
JOIN user_auths ON users.user_id = user_auths.user_id
WHERE users.username = $1
LIMIT 1
`

type GetUserAuthByUsernameRow struct {
	UserID       int32
	Username     string
	DisplayName  string
	IconPath     pgtype.Text
	IsAdmin      bool
	CreatedAt    pgtype.Timestamp
	UserAuthID   int32
	UserID_2     int32
	AuthType     string
	PasswordHash pgtype.Text
}

func (q *Queries) GetUserAuthByUsername(ctx context.Context, username string) (GetUserAuthByUsernameRow, error) {
	row := q.db.QueryRow(ctx, getUserAuthByUsername, username)
	var i GetUserAuthByUsernameRow
	err := row.Scan(
		&i.UserID,
		&i.Username,
		&i.DisplayName,
		&i.IconPath,
		&i.IsAdmin,
		&i.CreatedAt,
		&i.UserAuthID,
		&i.UserID_2,
		&i.AuthType,
		&i.PasswordHash,
	)
	return i, err
}

const getUserById = `-- name: GetUserById :one
SELECT user_id, username, display_name, icon_path, is_admin, created_at FROM users
WHERE users.user_id = $1
LIMIT 1
`

func (q *Queries) GetUserById(ctx context.Context, userID int32) (User, error) {
	row := q.db.QueryRow(ctx, getUserById, userID)
	var i User
	err := row.Scan(
		&i.UserID,
		&i.Username,
		&i.DisplayName,
		&i.IconPath,
		&i.IsAdmin,
		&i.CreatedAt,
	)
	return i, err
}

const listGames = `-- name: ListGames :many
SELECT game_id, state, display_name, duration_seconds, created_at, started_at, games.problem_id, problems.problem_id, title, description FROM games
LEFT JOIN problems ON games.problem_id = problems.problem_id
`

type ListGamesRow struct {
	GameID          int32
	State           string
	DisplayName     string
	DurationSeconds int32
	CreatedAt       pgtype.Timestamp
	StartedAt       pgtype.Timestamp
	ProblemID       pgtype.Int4
	ProblemID_2     pgtype.Int4
	Title           pgtype.Text
	Description     pgtype.Text
}

func (q *Queries) ListGames(ctx context.Context) ([]ListGamesRow, error) {
	rows, err := q.db.Query(ctx, listGames)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListGamesRow
	for rows.Next() {
		var i ListGamesRow
		if err := rows.Scan(
			&i.GameID,
			&i.State,
			&i.DisplayName,
			&i.DurationSeconds,
			&i.CreatedAt,
			&i.StartedAt,
			&i.ProblemID,
			&i.ProblemID_2,
			&i.Title,
			&i.Description,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listGamesForPlayer = `-- name: ListGamesForPlayer :many
SELECT games.game_id, state, display_name, duration_seconds, created_at, started_at, games.problem_id, problems.problem_id, title, description, game_players.game_id, user_id FROM games
LEFT JOIN problems ON games.problem_id = problems.problem_id
JOIN game_players ON games.game_id = game_players.game_id
WHERE game_players.user_id = $1
`

type ListGamesForPlayerRow struct {
	GameID          int32
	State           string
	DisplayName     string
	DurationSeconds int32
	CreatedAt       pgtype.Timestamp
	StartedAt       pgtype.Timestamp
	ProblemID       pgtype.Int4
	ProblemID_2     pgtype.Int4
	Title           pgtype.Text
	Description     pgtype.Text
	GameID_2        int32
	UserID          int32
}

func (q *Queries) ListGamesForPlayer(ctx context.Context, userID int32) ([]ListGamesForPlayerRow, error) {
	rows, err := q.db.Query(ctx, listGamesForPlayer, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListGamesForPlayerRow
	for rows.Next() {
		var i ListGamesForPlayerRow
		if err := rows.Scan(
			&i.GameID,
			&i.State,
			&i.DisplayName,
			&i.DurationSeconds,
			&i.CreatedAt,
			&i.StartedAt,
			&i.ProblemID,
			&i.ProblemID_2,
			&i.Title,
			&i.Description,
			&i.GameID_2,
			&i.UserID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateGameStartedAt = `-- name: UpdateGameStartedAt :exec
UPDATE games
SET started_at = $2
WHERE game_id = $1
`

type UpdateGameStartedAtParams struct {
	GameID    int32
	StartedAt pgtype.Timestamp
}

func (q *Queries) UpdateGameStartedAt(ctx context.Context, arg UpdateGameStartedAtParams) error {
	_, err := q.db.Exec(ctx, updateGameStartedAt, arg.GameID, arg.StartedAt)
	return err
}

const updateGameState = `-- name: UpdateGameState :exec
UPDATE games
SET state = $2
WHERE game_id = $1
`

type UpdateGameStateParams struct {
	GameID int32
	State  string
}

func (q *Queries) UpdateGameState(ctx context.Context, arg UpdateGameStateParams) error {
	_, err := q.db.Exec(ctx, updateGameState, arg.GameID, arg.State)
	return err
}