47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
|
package backend
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"database/sql"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
|
||
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model"
|
||
|
)
|
||
|
|
||
|
var ErrNotFound = errors.New("backend not found")
|
||
|
|
||
|
// const backendRows = `"id", "name", "oidc_id", "oidc_secret"`
|
||
|
const backendRows = `"id", "name"`
|
||
|
|
||
|
type BackendDB interface {
|
||
|
GetBackendByName(ctx context.Context, name string) (*model.Backend, error)
|
||
|
}
|
||
|
|
||
|
type sqlBackendDB struct {
|
||
|
db *sql.DB
|
||
|
}
|
||
|
|
||
|
func backendFromRow(row sql.Row) (*model.Backend, error) {
|
||
|
var res model.Backend
|
||
|
|
||
|
if err := row.Scan(&res.ID, &res.Name); err != nil {
|
||
|
if errors.Is(err, sql.ErrNoRows) {
|
||
|
return nil, ErrNotFound
|
||
|
}
|
||
|
return nil, fmt.Errorf("invalid format for backend: %w", err)
|
||
|
}
|
||
|
return &res, nil
|
||
|
}
|
||
|
|
||
|
func (db *sqlBackendDB) GetBackendByName(ctx context.Context, name string) (*model.Backend, error) {
|
||
|
query := fmt.Sprintf(`SELECT %s FROM "backend" WHERE "name" = ?`, backendRows)
|
||
|
fmt.Println(query, name)
|
||
|
row := db.db.QueryRowContext(ctx, query, name)
|
||
|
return backendFromRow(*row)
|
||
|
}
|
||
|
|
||
|
func New(db *sql.DB) *sqlBackendDB {
|
||
|
return &sqlBackendDB{db: db}
|
||
|
}
|