aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/db/query.sql.go
blob: 20a7dc1c541a80ebeeb2fcab48cda670a1bb0d74 (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
// 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 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
}