package postgres import ( "context" "github.com/jackc/pgx/v5/pgxpool" "high-loud-apps-labs/internal/domain" ) type SessionRepo struct { pool *pgxpool.Pool } func NewSessionRepo(pool *pgxpool.Pool) *SessionRepo { return &SessionRepo{pool: pool} } func (r *SessionRepo) Create(ctx context.Context, s *domain.Session) error { const q = ` INSERT INTO sessions (token_hash, user_id, expires_at, created_at) VALUES ($1, $2::uuid, $3, $4) ` _, err := r.pool.Exec(ctx, q, s.TokenHash, s.UserID, s.ExpiresAt, s.CreatedAt) if err != nil { if uniqueViolation(err) { return &domain.AlreadyExistsError{ Entity: "session", Field: "token_hash", Value: "", } } return mapError(err) } return nil } func (r *SessionRepo) GetByTokenHash(ctx context.Context, tokenHash []byte) (*domain.Session, error) { const q = ` SELECT token_hash, user_id, expires_at, created_at FROM sessions WHERE token_hash = $1 ` s := &domain.Session{} err := r.pool.QueryRow(ctx, q, tokenHash). Scan(&s.TokenHash, &s.UserID, &s.ExpiresAt, &s.CreatedAt) if err != nil { return nil, notFound(err, "session", "") } return s, nil } func (r *SessionRepo) DeleteByTokenHash(ctx context.Context, tokenHash []byte) error { const q = `DELETE FROM sessions WHERE token_hash = $1` _, err := r.pool.Exec(ctx, q, tokenHash) return mapError(err) } var _ domain.SessionRepository = (*SessionRepo)(nil)