add tickets api

This commit is contained in:
2026-09-18 16:08:35 +07:00
parent 32191529a5
commit 1be16d706e
11 changed files with 778 additions and 185 deletions
+39
View File
@@ -0,0 +1,39 @@
package tickets_repo
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
)
type Repo struct {
pool *pgxpool.Pool
}
func New(pool *pgxpool.Pool) *Repo {
return &Repo{pool: pool}
}
func (r *Repo) CreateBatch(ctx context.Context, gameID int, tokenHashes [][]byte) ([]int, error) {
tx, err := r.pool.Begin(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback(ctx)
ids := make([]int, 0, len(tokenHashes))
for _, tokenHash := range tokenHashes {
var id int
if err := tx.QueryRow(ctx,
`INSERT INTO tickets (game_id, token_hash) VALUES ($1, $2) RETURNING id`,
gameID, tokenHash,
).Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
if err := tx.Commit(ctx); err != nil {
return nil, err
}
return ids, nil
}