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.
99 lines
3.8 KiB
Go
99 lines
3.8 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/providers/lidarr"
|
|
)
|
|
|
|
// LidarrRequestRepo keeps the artists a person sent to their Lidarr. It implements lidarr.RequestStore.
|
|
type LidarrRequestRepo struct{ db *bun.DB }
|
|
|
|
// NewLidarrRequestRepo builds a LidarrRequestRepo.
|
|
func NewLidarrRequestRepo(db *bun.DB) *LidarrRequestRepo { return &LidarrRequestRepo{db: db} }
|
|
|
|
// Record keeps that the person sent the artist. Sending the same artist again (the same name, whatever its id) keeps
|
|
// the first date and fills what was missing.
|
|
func (r *LidarrRequestRepo) Record(ctx context.Context, userID, artistID int64, at time.Time) error {
|
|
var (
|
|
name string
|
|
mbid sql.NullString
|
|
)
|
|
err := r.db.QueryRowContext(ctx, "SELECT name, mbid FROM artists WHERE id = ?", artistID).Scan(&name, &mbid)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return lidarr.ErrNoSuchArtist
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("read artist: %w", err)
|
|
}
|
|
_, err = r.db.ExecContext(ctx,
|
|
`INSERT INTO lidarr_requests (user_id, artist_id, artist_name, artist_key, artist_mbid, requested_at) VALUES (?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT (user_id, artist_key) DO UPDATE SET
|
|
artist_id = COALESCE(lidarr_requests.artist_id, excluded.artist_id),
|
|
artist_mbid = COALESCE(lidarr_requests.artist_mbid, excluded.artist_mbid)`,
|
|
userID, artistID, name, searchKey(name), mbid, at.Unix())
|
|
if err != nil {
|
|
return fmt.Errorf("keep lidarr request: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// List first notices the waiting artists that have music now (by the artist of the request, or by an artist with the
|
|
// same name that a scan made), and keeps the moment. Then it gives a page of the requests of the person, newest first.
|
|
func (r *LidarrRequestRepo) List(ctx context.Context, userID int64, status string, limit, offset int, now time.Time) (lidarr.RequestPage, error) {
|
|
page := lidarr.RequestPage{Items: []lidarr.Request{}, Limit: limit, Offset: offset}
|
|
if _, err := r.db.ExecContext(ctx,
|
|
`UPDATE lidarr_requests SET arrived_at = ? WHERE user_id = ? AND arrived_at IS NULL AND (
|
|
(artist_id IS NOT NULL AND `+playableArtist("lidarr_requests.artist_id")+`)
|
|
OR EXISTS (SELECT 1 FROM artists a WHERE a.search_key = lidarr_requests.artist_key AND `+playableArtist("a.id")+`))`,
|
|
now.Unix(), userID); err != nil {
|
|
return page, fmt.Errorf("notice arrived artists: %w", err)
|
|
}
|
|
if err := r.db.QueryRowContext(ctx,
|
|
`SELECT COALESCE(SUM(CASE WHEN arrived_at IS NULL THEN 1 ELSE 0 END), 0), COALESCE(SUM(CASE WHEN arrived_at IS NULL THEN 0 ELSE 1 END), 0)
|
|
FROM lidarr_requests WHERE user_id = ?`, userID).Scan(&page.Waiting, &page.Arrived); err != nil {
|
|
return page, fmt.Errorf("count lidarr requests: %w", err)
|
|
}
|
|
where := "user_id = ?"
|
|
switch status {
|
|
case lidarr.Waiting:
|
|
where += " AND arrived_at IS NULL"
|
|
page.Total = page.Waiting
|
|
case lidarr.Arrived:
|
|
where += " AND arrived_at IS NOT NULL"
|
|
page.Total = page.Arrived
|
|
default:
|
|
page.Total = page.Waiting + page.Arrived
|
|
}
|
|
rows, err := r.db.QueryContext(ctx,
|
|
`SELECT id, artist_id, artist_name, artist_mbid, requested_at, arrived_at FROM lidarr_requests WHERE `+where+
|
|
` ORDER BY requested_at DESC, id DESC LIMIT ? OFFSET ?`, userID, limit, offset)
|
|
if err != nil {
|
|
return page, fmt.Errorf("list lidarr requests: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var (
|
|
req lidarr.Request
|
|
artist sql.NullInt64
|
|
mbid sql.NullString
|
|
requested int64
|
|
arrived sql.NullInt64
|
|
)
|
|
if err := rows.Scan(&req.ID, &artist, &req.Name, &mbid, &requested, &arrived); err != nil {
|
|
return page, err
|
|
}
|
|
req.ArtistID, req.MBID, req.RequestedAt = artist.Int64, mbid.String, time.Unix(requested, 0)
|
|
if arrived.Valid {
|
|
req.ArrivedAt = time.Unix(arrived.Int64, 0)
|
|
}
|
|
page.Items = append(page.Items, req)
|
|
}
|
|
return page, rows.Err()
|
|
}
|