add get ticket

This commit is contained in:
2026-09-18 16:14:56 +07:00
parent 1be16d706e
commit 22250cb68e
9 changed files with 727 additions and 187 deletions
+28
View File
@@ -2,10 +2,25 @@ package tickets_repo
import (
"context"
"errors"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
var ErrTicketNotFound = errors.New("Билет не найден")
type Info struct {
ID int
Status string
GameName string
StartAt time.Time
Location *string
GameStatus string
UsedAt *time.Time
}
type Repo struct {
pool *pgxpool.Pool
}
@@ -37,3 +52,16 @@ func (r *Repo) CreateBatch(ctx context.Context, gameID int, tokenHashes [][]byte
}
return ids, nil
}
func (r *Repo) GetByHash(ctx context.Context, tokenHash []byte) (*Info, error) {
info := &Info{}
err := r.pool.QueryRow(ctx, `
SELECT t.id, t.status, g.name, g.start_at, g.location, g.status, t.used_at
FROM tickets t JOIN games g ON g.id = t.game_id
WHERE t.token_hash = $1`, tokenHash).Scan(
&info.ID, &info.Status, &info.GameName, &info.StartAt, &info.Location, &info.GameStatus, &info.UsedAt)
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrTicketNotFound
}
return info, err
}