package sql import ( "context" "time" "emperror.dev/errors" "git.sleepycat.moe/sam/mercury/internal/database" "github.com/keegancsmith/sqlf" "github.com/oklog/ulid/v2" ) // TokenStore is the interface to tokens in the database. type TokenStore struct { q Querier } // NewTokenStore creates a new TokenStore instance. func NewTokenStore(q Querier) *TokenStore { return &TokenStore{q: q} } func (s *TokenStore) Get(ctx context.Context, id ulid.ULID) (database.Token, error) { q := sqlf.Sprintf("SELECT * FROM tokens WHERE id = %s", id) t, err := Get[database.Token](ctx, s.q, q) return t, errors.Wrap(err, "executing query") } func (s *TokenStore) GetApplication(ctx context.Context, id ulid.ULID) (database.Application, error) { q := sqlf.Sprintf("SELECT * FROM applications WHERE id = %s", id) app, err := Get[database.Application](ctx, s.q, q) return app, errors.Wrap(err, "executing query") } func (s *TokenStore) Create(ctx context.Context, userID, appID ulid.ULID, scopes database.TokenScopes, expires time.Time) (database.Token, error) { q := sqlf.Sprintf(`INSERT INTO tokens (id, user_id, app_id, scopes, expires) values (%s, %s, %s, %v, %v) RETURNING *`, makeULID(), userID, appID, scopes, expires) t, err := Get[database.Token](ctx, s.q, q) return t, errors.Wrap(err, "executing query") } func (s *TokenStore) CreateApplication(ctx context.Context, name string) (database.Application, error) { q := sqlf.Sprintf("INSERT INTO applications (id, name) VALUES (%s, %s) RETURNING *", makeULID(), name) app, err := Get[database.Application](ctx, s.q, q) return app, errors.Wrap(err, "executing query") }