basic app implementation
All checks were successful
build / build (push) Successful in 1m3s
release-tag / release-image (push) Successful in 17m3s

This commit is contained in:
Tijl 2024-08-25 22:14:13 +02:00
parent b8d7a8c4cf
commit 04e302e7b0
Signed by: tijl
GPG Key ID: DAE24BFCD722F053
4 changed files with 45 additions and 12 deletions

View File

@ -1,9 +1,21 @@
-- name: CreateFlagsGame :exec
INSERT INTO app_flags_games (uid, tags, question_amount) VALUES ($1, $2, $3) RETURNING (game_id, game_seed);
INSERT INTO app_flags_games (uid, tags, question_amount)
VALUES ($1, $2, $3)
RETURNING (game_id, game_seed);
-- name: GetFlagsGame :one
SELECT * FROM app_flags_games WHERE game_id = $1 LIMIT 1;
-- name: UpdateFlagsGame :exec
UPDATE app_flags_games SET question_current = $1,
last_activity = CURRENT_TIMESTAMP;
UPDATE app_flags_games
SET question_current = $1, last_activity = CURRENT_TIMESTAMP
WHERE game_id = $2;
-- name: UpdateQuestionCorrect :exec
UPDATE app_flags_games
SET questions_correct = (
SELECT COUNT(*)
FROM app_flags_games_answers
WHERE game_id $1 AND correct = TRUE
)
WHERE game_id = $1;

View File

@ -14,7 +14,9 @@ import (
)
const createFlagsGame = `-- name: CreateFlagsGame :exec
INSERT INTO app_flags_games (uid, tags, question_amount) VALUES ($1, $2, $3) RETURNING (game_id, game_seed)
INSERT INTO app_flags_games (uid, tags, question_amount)
VALUES ($1, $2, $3)
RETURNING (game_id, game_seed)
`
type CreateFlagsGameParams struct {
@ -50,11 +52,32 @@ func (q *Queries) GetFlagsGame(ctx context.Context, gameID uuid.UUID) (AppFlagsG
}
const updateFlagsGame = `-- name: UpdateFlagsGame :exec
UPDATE app_flags_games SET question_current = $1,
last_activity = CURRENT_TIMESTAMP
UPDATE app_flags_games
SET question_current = $1, last_activity = CURRENT_TIMESTAMP
WHERE game_id = $2
`
func (q *Queries) UpdateFlagsGame(ctx context.Context, questionCurrent int32) error {
_, err := q.db.ExecContext(ctx, updateFlagsGame, questionCurrent)
type UpdateFlagsGameParams struct {
QuestionCurrent int32
GameID uuid.UUID
}
func (q *Queries) UpdateFlagsGame(ctx context.Context, arg UpdateFlagsGameParams) error {
_, err := q.db.ExecContext(ctx, updateFlagsGame, arg.QuestionCurrent, arg.GameID)
return err
}
const updateQuestionCorrect = `-- name: UpdateQuestionCorrect :exec
UPDATE app_flags_games
SET questions_correct = (
SELECT COUNT(*)
FROM app_flags_games_answers
WHERE game_id $1 AND correct = TRUE
)
WHERE game_id = $1
`
func (q *Queries) UpdateQuestionCorrect(ctx context.Context, dollar_1 interface{}) error {
_, err := q.db.ExecContext(ctx, updateQuestionCorrect, dollar_1)
return err
}

View File

@ -18,7 +18,7 @@ type AppFlagsGame struct {
Tags []string
QuestionAmount int32
QuestionCurrent int32
QuestionCorrect sql.NullInt32
QuestionCorrect int32
CreatedAt time.Time
LastActivity time.Time
}

View File

@ -5,9 +5,7 @@ CREATE TABLE app_flags_games (
tags VARCHAR[] NOT NULL,
question_amount INT NOT NULL,
question_current INT DEFAULT 0 NOT NULL,
question_correct INT GENERATED ALWAYS AS (
(SELECT COUNT(*) FROM app_flags_games_answers a WHERE a.game_id = app_flags_games.game_id AND a.correct = TRUE)
) STORED,
question_correct INT DEFAULT 0 NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
last_activity TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (uid) REFERENCES users (uid)