feat: add foundation layer (Go module, config, database, Docker)
Squashed commits from foundation-layer branch: - Initialize Go module and project skeleton (cmd/naviwatcher/main.go) - Add configuration management with YAML parsing and validation - Add database layer with schema migrations (artist_settings, external_releases, notifications_sent) - Add CRUD operations for artist_settings, external_releases, notifications_sent - Add Docker setup with multi-stage build and docker-compose - Verify acceptance criteria (tests, vet, fmt) - Update README.md with build/run/test instructions - Fix: filter ignored releases in GetUnnotifiedReleases (spec compliance) - Fix: add FK constraint on notifications_sent.rgid - Fix: add config.yaml to .gitignore (security) - Fix: run Docker container as non-root user - Fix: pin alpine:3.21 instead of alpine:latest - Fix: wrap migrations in transactions for atomicity All 49 tests pass, go vet clean, Docker image builds successfully.
This commit is contained in:
116
internal/database/artist_settings.go
Normal file
116
internal/database/artist_settings.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// GetArtistSettings retrieves an artist_settings row by ID.
|
||||
// Returns sql.ErrNoRows if the artist is not found.
|
||||
func GetArtistSettings(db *DB, id string) (*ArtistSettings, error) {
|
||||
var s ArtistSettings
|
||||
err := db.Conn().QueryRow(
|
||||
"SELECT id, name, ignore_singles, ignore_compilations, monitored FROM artist_settings WHERE id = ?",
|
||||
id,
|
||||
).Scan(&s.ID, &s.Name, &s.IgnoreSingles, &s.IgnoreCompilations, &s.Monitored)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
// SaveArtistSettings inserts or replaces an artist_settings row.
|
||||
func SaveArtistSettings(db *DB, settings *ArtistSettings) error {
|
||||
_, err := db.Conn().Exec(
|
||||
"INSERT OR REPLACE INTO artist_settings (id, name, ignore_singles, ignore_compilations, monitored) VALUES (?, ?, ?, ?, ?)",
|
||||
settings.ID, settings.Name, settings.IgnoreSingles, settings.IgnoreCompilations, settings.Monitored,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("save artist settings: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAllArtistSettings returns all rows from artist_settings.
|
||||
func GetAllArtistSettings(db *DB) ([]ArtistSettings, error) {
|
||||
rows, err := db.Conn().Query(
|
||||
"SELECT id, name, ignore_singles, ignore_compilations, monitored FROM artist_settings",
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query all artist settings: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var results []ArtistSettings
|
||||
for rows.Next() {
|
||||
var s ArtistSettings
|
||||
if err := rows.Scan(&s.ID, &s.Name, &s.IgnoreSingles, &s.IgnoreCompilations, &s.Monitored); err != nil {
|
||||
return nil, fmt.Errorf("scan artist settings: %w", err)
|
||||
}
|
||||
results = append(results, s)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate artist settings: %w", err)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// UpdateArtistSettings updates specific fields of an artist_settings row by ID.
|
||||
// The updates map keys must match column names: "name", "ignore_singles", "ignore_compilations", "monitored".
|
||||
func UpdateArtistSettings(db *DB, id string, updates map[string]interface{}) error {
|
||||
if len(updates) == 0 {
|
||||
return fmt.Errorf("no updates provided")
|
||||
}
|
||||
|
||||
// Build the query using a fixed set of allowed columns to avoid dynamic SQL.
|
||||
const baseQuery = "UPDATE artist_settings SET"
|
||||
|
||||
var args []interface{}
|
||||
setClause := ""
|
||||
for col, val := range updates {
|
||||
switch col {
|
||||
case "name":
|
||||
if setClause != "" {
|
||||
setClause += ", "
|
||||
}
|
||||
setClause += "name = ?"
|
||||
args = append(args, val)
|
||||
case "ignore_singles":
|
||||
if setClause != "" {
|
||||
setClause += ", "
|
||||
}
|
||||
setClause += "ignore_singles = ?"
|
||||
args = append(args, val)
|
||||
case "ignore_compilations":
|
||||
if setClause != "" {
|
||||
setClause += ", "
|
||||
}
|
||||
setClause += "ignore_compilations = ?"
|
||||
args = append(args, val)
|
||||
case "monitored":
|
||||
if setClause != "" {
|
||||
setClause += ", "
|
||||
}
|
||||
setClause += "monitored = ?"
|
||||
args = append(args, val)
|
||||
default:
|
||||
return fmt.Errorf("unknown column: %s", col)
|
||||
}
|
||||
}
|
||||
|
||||
args = append(args, id)
|
||||
query := fmt.Sprintf("%s %s WHERE id = ?", baseQuery, setClause)
|
||||
result, err := db.Conn().Exec(query, args...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update artist settings: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("rows affected: %w", err)
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return fmt.Errorf("artist not found: %s", id)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user