generated from VLADIMIR/template
add tickets api
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user