Show user's active games on home page after login
This commit is contained in:
@@ -209,6 +209,60 @@ func (q *Queries) GetGamesByUserID(ctx context.Context, userID sql.NullString) (
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getUserActiveGames = `-- name: GetUserActiveGames :many
|
||||
SELECT
|
||||
g.id,
|
||||
g.status,
|
||||
g.current_turn,
|
||||
g.updated_at,
|
||||
gp_user.color as my_color,
|
||||
gp_opponent.nickname as opponent_nickname
|
||||
FROM games g
|
||||
JOIN game_players gp_user ON g.id = gp_user.game_id AND gp_user.user_id = ?
|
||||
LEFT JOIN game_players gp_opponent ON g.id = gp_opponent.game_id AND gp_opponent.slot != gp_user.slot
|
||||
WHERE g.status < 2
|
||||
ORDER BY g.updated_at DESC
|
||||
`
|
||||
|
||||
type GetUserActiveGamesRow struct {
|
||||
ID string
|
||||
Status int64
|
||||
CurrentTurn int64
|
||||
UpdatedAt sql.NullTime
|
||||
MyColor int64
|
||||
OpponentNickname sql.NullString
|
||||
}
|
||||
|
||||
func (q *Queries) GetUserActiveGames(ctx context.Context, userID sql.NullString) ([]GetUserActiveGamesRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getUserActiveGames, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetUserActiveGamesRow
|
||||
for rows.Next() {
|
||||
var i GetUserActiveGamesRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Status,
|
||||
&i.CurrentTurn,
|
||||
&i.UpdatedAt,
|
||||
&i.MyColor,
|
||||
&i.OpponentNickname,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateGame = `-- name: UpdateGame :exec
|
||||
UPDATE games
|
||||
SET board = ?, current_turn = ?, status = ?, winner_user_id = ?, winning_cells = ?, updated_at = CURRENT_TIMESTAMP
|
||||
|
||||
@@ -29,3 +29,17 @@ SELECT g.* FROM games g
|
||||
JOIN game_players gp ON g.id = gp.game_id
|
||||
WHERE gp.user_id = ?
|
||||
ORDER BY g.updated_at DESC;
|
||||
|
||||
-- name: GetUserActiveGames :many
|
||||
SELECT
|
||||
g.id,
|
||||
g.status,
|
||||
g.current_turn,
|
||||
g.updated_at,
|
||||
gp_user.color as my_color,
|
||||
gp_opponent.nickname as opponent_nickname
|
||||
FROM games g
|
||||
JOIN game_players gp_user ON g.id = gp_user.game_id AND gp_user.user_id = ?
|
||||
LEFT JOIN game_players gp_opponent ON g.id = gp_opponent.game_id AND gp_opponent.slot != gp_user.slot
|
||||
WHERE g.status < 2
|
||||
ORDER BY g.updated_at DESC;
|
||||
|
||||
Reference in New Issue
Block a user