diff --git a/internal/queries/app_flags.sql b/internal/queries/app_flags.sql index a430d1e..e9792e3 100644 --- a/internal/queries/app_flags.sql +++ b/internal/queries/app_flags.sql @@ -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; diff --git a/internal/queries/app_flags.sql.go b/internal/queries/app_flags.sql.go index 76d3c89..9e7f472 100644 --- a/internal/queries/app_flags.sql.go +++ b/internal/queries/app_flags.sql.go @@ -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 } diff --git a/internal/queries/models.go b/internal/queries/models.go index d9cf217..cbb2bb6 100644 --- a/internal/queries/models.go +++ b/internal/queries/models.go @@ -18,7 +18,7 @@ type AppFlagsGame struct { Tags []string QuestionAmount int32 QuestionCurrent int32 - QuestionCorrect sql.NullInt32 + QuestionCorrect int32 CreatedAt time.Time LastActivity time.Time } diff --git a/migrations/00000002_flags.up.sql b/migrations/00000002_flags.up.sql index b80a1a7..4e0a634 100644 --- a/migrations/00000002_flags.up.sql +++ b/migrations/00000002_flags.up.sql @@ -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)