2022-12-28 12:32:46 -05:00
|
|
|
package model
|
2020-10-20 13:38:44 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-07-18 16:49:31 -04:00
|
|
|
"errors"
|
2020-10-20 13:38:44 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TODO: Should the type be encoded in the ID?
|
2026-02-08 08:57:30 -06:00
|
|
|
func GetEntityByID(ctx context.Context, ds DataStore, id string) (any, error) {
|
2026-08-19 16:25:12 -04:00
|
|
|
entity, _, err := getEntity(ctx, ds, id)
|
|
|
|
|
return entity, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetEntityKindByID resolves a bare entity id to its artwork Kind, searching the same tables as
|
|
|
|
|
// GetEntityByID. It reports ErrNotFound when no entity owns the id.
|
|
|
|
|
func GetEntityKindByID(ctx context.Context, ds DataStore, id string) (Kind, error) {
|
|
|
|
|
_, kind, err := getEntity(ctx, ds, id)
|
|
|
|
|
return kind, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getEntity(ctx context.Context, ds DataStore, id string) (any, Kind, error) {
|
|
|
|
|
getters := []struct {
|
|
|
|
|
kind Kind
|
|
|
|
|
get func() (any, error)
|
|
|
|
|
}{
|
2026-09-25 18:06:10 -04:00
|
|
|
{KindArtistArtwork, func() (any, error) { return ds.Artist().Get(ctx, id) }},
|
|
|
|
|
{KindAlbumArtwork, func() (any, error) { return ds.Album().Get(ctx, id) }},
|
|
|
|
|
{KindPlaylistArtwork, func() (any, error) { return ds.Playlist().Get(ctx, id) }},
|
|
|
|
|
{KindMediaFileArtwork, func() (any, error) { return ds.MediaFile().Get(ctx, id) }},
|
|
|
|
|
{KindRadioArtwork, func() (any, error) { return ds.Radio().Get(ctx, id) }},
|
2020-10-20 13:38:44 -04:00
|
|
|
}
|
2026-08-19 16:25:12 -04:00
|
|
|
for _, g := range getters {
|
|
|
|
|
entity, err := g.get()
|
2026-07-18 16:49:31 -04:00
|
|
|
if err == nil {
|
2026-08-19 16:25:12 -04:00
|
|
|
return entity, g.kind, nil
|
2026-07-18 16:49:31 -04:00
|
|
|
}
|
|
|
|
|
if !errors.Is(err, ErrNotFound) {
|
2026-08-19 16:25:12 -04:00
|
|
|
return nil, Kind{}, err
|
2026-07-18 16:49:31 -04:00
|
|
|
}
|
2020-10-20 13:38:44 -04:00
|
|
|
}
|
2026-08-19 16:25:12 -04:00
|
|
|
return nil, Kind{}, ErrNotFound
|
2020-10-20 13:38:44 -04:00
|
|
|
}
|