Files
minstrel/internal/lidarrconfig/service.go
T
bvandeusen c265b871c3 feat(admin): metadata-profile picker on integrations page
Persists the operator's metadata-profile choice alongside quality
profile + root folder. Defaults to the first profile Lidarr returns
(usually 'Standard' on a vanilla install) so the common case is
zero-click; operators with custom profiles like 'Singles only' can pick
explicitly.

Backend:
- Migration 0013: adds nullable default_metadata_profile_id to
  lidarr_config. Existing rows get NULL and the service falls back to
  fetch-and-pick-first until they save.
- Updated lidarr_config queries + sqlc + lidarrconfig.Config + admin
  view/put body to round-trip the new field.
- handlePutLidarrConfig requires it (along with QP and root folder)
  when enabled=true — matches the existing missing_defaults gate.
- New GET /api/admin/lidarr/metadata-profiles handler + lidarr.Client
  ListMetadataProfiles (GET /api/v1/metadataprofile, same shape as
  the quality-profile endpoint).
- lidarrrequests.Approve prefers cfg.DefaultMetadataProfileID; falls
  back to the fetch-list path only when 0 (back-compat for upgraders).

Frontend:
- LidarrConfig type + LidarrMetadataProfile type + qk.lidarrMetadataProfiles.
- listMetadataProfiles + createMetadataProfilesQuery client helpers.
- Integrations page: third <select> picker, auto-defaults to first
  profile when the saved value is 0, sends the new field on save and
  clears it on disconnect.
- Updated test fixtures + the duplicate 'Standard' option string in
  the dropdown-populates assertion.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 00:09:13 -04:00

95 lines
2.4 KiB
Go

// Package lidarrconfig is a thin wrapper over the singleton lidarr_config
// row. Get returns a typed Config; Save updates it. Callers branch on
// Config.Enabled — never on raw NULL fields.
package lidarrconfig
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// Config is the typed projection of lidarr_config (no NULL fields exposed
// to callers — empty strings / zero ints carry the "unset" meaning).
type Config struct {
Enabled bool
BaseURL string
APIKey string
DefaultQualityProfileID int
DefaultMetadataProfileID int
DefaultRootFolderPath string
}
// Service reads and writes the singleton.
type Service struct {
pool *pgxpool.Pool
}
func New(pool *pgxpool.Pool) *Service { return &Service{pool: pool} }
func (s *Service) Get(ctx context.Context) (Config, error) {
row, err := dbq.New(s.pool).GetLidarrConfig(ctx)
if err != nil {
return Config{}, fmt.Errorf("lidarrconfig: %w", err)
}
cfg := Config{Enabled: row.Enabled}
if row.BaseUrl != nil {
cfg.BaseURL = *row.BaseUrl
}
if row.ApiKey != nil {
cfg.APIKey = *row.ApiKey
}
if row.DefaultQualityProfileID != nil {
cfg.DefaultQualityProfileID = int(*row.DefaultQualityProfileID)
}
if row.DefaultMetadataProfileID != nil {
cfg.DefaultMetadataProfileID = int(*row.DefaultMetadataProfileID)
}
if row.DefaultRootFolderPath != nil {
cfg.DefaultRootFolderPath = *row.DefaultRootFolderPath
}
return cfg, nil
}
// Save writes the entire row. Callers pass the full Config they want
// stored — this is not a partial update.
func (s *Service) Save(ctx context.Context, cfg Config) error {
var (
baseURL = strPtr(cfg.BaseURL)
apiKey = strPtr(cfg.APIKey)
qpID = int32Ptr(cfg.DefaultQualityProfileID)
mpID = int32Ptr(cfg.DefaultMetadataProfileID)
rootPath = strPtr(cfg.DefaultRootFolderPath)
)
_, err := dbq.New(s.pool).UpdateLidarrConfig(ctx, dbq.UpdateLidarrConfigParams{
Enabled: cfg.Enabled,
BaseUrl: baseURL,
ApiKey: apiKey,
DefaultQualityProfileID: qpID,
DefaultMetadataProfileID: mpID,
DefaultRootFolderPath: rootPath,
})
if err != nil {
return fmt.Errorf("lidarrconfig: %w", err)
}
return nil
}
func strPtr(s string) *string {
if s == "" {
return nil
}
return &s
}
func int32Ptr(i int) *int32 {
if i == 0 {
return nil
}
v := int32(i)
return &v
}