31 lines
932 B
Go
31 lines
932 B
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
// GetTicker retourne le ticker (ou CoinGecko ID) mis en cache pour un instrument.
|
|
// Retourne pgx.ErrNoRows si absent.
|
|
func (s *Store) GetTicker(ctx context.Context, instrumentID int32) (string, error) {
|
|
var ticker string
|
|
err := s.pool.QueryRow(ctx,
|
|
`SELECT ticker FROM instrument_ticker_cache WHERE instrument_id = $1`,
|
|
instrumentID).Scan(&ticker)
|
|
return ticker, err
|
|
}
|
|
|
|
// UpsertTicker met à jour ou insère le ticker pour un instrument.
|
|
func (s *Store) UpsertTicker(ctx context.Context, instrumentID int32, ticker string) error {
|
|
_, err := s.pool.Exec(ctx,
|
|
`INSERT INTO instrument_ticker_cache (instrument_id, ticker, fetched_at)
|
|
VALUES ($1, $2, NOW())
|
|
ON CONFLICT (instrument_id) DO UPDATE SET ticker = EXCLUDED.ticker, fetched_at = NOW()`,
|
|
instrumentID, ticker)
|
|
return err
|
|
}
|
|
|
|
func isNotFound(err error) bool {
|
|
return err == pgx.ErrNoRows
|
|
}
|