Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
100 lines
3.4 KiB
Go
100 lines
3.4 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/providers"
|
|
)
|
|
|
|
type providerRow struct {
|
|
bun.BaseModel `bun:"table:provider_settings"`
|
|
|
|
Provider string `bun:"provider,pk"`
|
|
Enabled bool `bun:"enabled,notnull"`
|
|
APIKey string `bun:"api_key,notnull"`
|
|
Contact string `bun:"contact,notnull"`
|
|
URL string `bun:"url,notnull"`
|
|
UpdatedAt int64 `bun:"updated_at,notnull"`
|
|
|
|
QualityProfileID int `bun:"quality_profile_id,notnull"`
|
|
MetadataProfileID int `bun:"metadata_profile_id,notnull"`
|
|
RootFolder string `bun:"root_folder,notnull"`
|
|
Monitor string `bun:"monitor,notnull"`
|
|
SearchMissing bool `bun:"search_missing,notnull"`
|
|
}
|
|
|
|
func (r providerRow) toSettings() providers.Settings {
|
|
return providers.Settings{
|
|
Provider: r.Provider, Enabled: r.Enabled, APIKey: r.APIKey, Contact: r.Contact, URL: r.URL,
|
|
Lidarr: providers.LidarrOptions{
|
|
QualityProfileID: r.QualityProfileID, MetadataProfileID: r.MetadataProfileID,
|
|
RootFolderPath: r.RootFolder, Monitor: r.Monitor, SearchMissing: r.SearchMissing,
|
|
},
|
|
UpdatedAt: time.Unix(r.UpdatedAt, 0),
|
|
}
|
|
}
|
|
|
|
// ProviderRepo stores provider settings. It implements providers.Store.
|
|
type ProviderRepo struct{ db *bun.DB }
|
|
|
|
// NewProviderRepo builds a ProviderRepo.
|
|
func NewProviderRepo(db *bun.DB) *ProviderRepo { return &ProviderRepo{db: db} }
|
|
|
|
// Get returns the settings of one provider, with found=false if there are none.
|
|
func (r *ProviderRepo) Get(ctx context.Context, name string) (providers.Settings, bool, error) {
|
|
var row providerRow
|
|
err := r.db.NewSelect().Model(&row).Where("provider = ?", name).Limit(1).Scan(ctx)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return providers.Settings{}, false, nil
|
|
}
|
|
if err != nil {
|
|
return providers.Settings{}, false, fmt.Errorf("select provider settings: %w", err)
|
|
}
|
|
return row.toSettings(), true, nil
|
|
}
|
|
|
|
// List returns every stored row.
|
|
func (r *ProviderRepo) List(ctx context.Context) ([]providers.Settings, error) {
|
|
var rows []providerRow
|
|
if err := r.db.NewSelect().Model(&rows).OrderExpr("provider").Scan(ctx); err != nil {
|
|
return nil, fmt.Errorf("list provider settings: %w", err)
|
|
}
|
|
out := make([]providers.Settings, len(rows))
|
|
for i, row := range rows {
|
|
out[i] = row.toSettings()
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Save inserts or updates the row of a provider.
|
|
func (r *ProviderRepo) Save(ctx context.Context, s providers.Settings) error {
|
|
row := &providerRow{
|
|
Provider: s.Provider, Enabled: s.Enabled, APIKey: s.APIKey,
|
|
Contact: s.Contact, URL: s.URL, UpdatedAt: s.UpdatedAt.Unix(),
|
|
QualityProfileID: s.Lidarr.QualityProfileID, MetadataProfileID: s.Lidarr.MetadataProfileID,
|
|
RootFolder: s.Lidarr.RootFolderPath, Monitor: s.Lidarr.Monitor, SearchMissing: s.Lidarr.SearchMissing,
|
|
}
|
|
_, err := r.db.NewInsert().Model(row).
|
|
On("CONFLICT (provider) DO UPDATE").
|
|
Set("enabled = EXCLUDED.enabled").
|
|
Set("api_key = EXCLUDED.api_key").
|
|
Set("contact = EXCLUDED.contact").
|
|
Set("url = EXCLUDED.url").
|
|
Set("quality_profile_id = EXCLUDED.quality_profile_id").
|
|
Set("metadata_profile_id = EXCLUDED.metadata_profile_id").
|
|
Set("root_folder = EXCLUDED.root_folder").
|
|
Set("monitor = EXCLUDED.monitor").
|
|
Set("search_missing = EXCLUDED.search_missing").
|
|
Set("updated_at = EXCLUDED.updated_at").
|
|
Exec(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("save provider settings: %w", err)
|
|
}
|
|
return nil
|
|
}
|