Associate main entities with library

This commit is contained in:
Deluan
2024-05-07 17:52:13 +02:00
committed by Deluan Quintão
parent 477bcaee58
commit 478c709a64
14 changed files with 153 additions and 135 deletions
+33 -8
View File
@@ -5,13 +5,13 @@ import (
"time"
. "github.com/Masterminds/squirrel"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/model"
"github.com/pocketbase/dbx"
)
type libraryRepository struct {
sqlRepository
sqlRestful
}
func NewLibraryRepository(ctx context.Context, db dbx.Builder) model.LibraryRepository {
@@ -31,19 +31,44 @@ func (r *libraryRepository) Get(id int) (*model.Library, error) {
func (r *libraryRepository) Put(l *model.Library) error {
cols := map[string]any{
"name": l.Name,
"path": l.Path,
"remote_path": l.RemotePath,
"last_scan_at": l.LastScanAt,
"updated_at": time.Now(),
"name": l.Name,
"path": l.Path,
"remote_path": l.RemotePath,
"updated_at": time.Now(),
}
if l.ID != 0 {
cols["id"] = l.ID
}
sq := Insert(r.tableName).SetMap(cols).
Suffix(`ON CONFLICT(id) DO UPDATE set name = excluded.name, path = excluded.path,
remote_path = excluded.remote_path, last_scan_at = excluded.last_scan_at`)
Suffix(`on conflict(id) do update set name = excluded.name, path = excluded.path,
remote_path = excluded.remote_path, updated_at = excluded.updated_at`)
_, err := r.executeSQL(sq)
return err
}
const hardCodedMusicFolderID = 1
// TODO Remove this method when we have a proper UI to add libraries
func (r *libraryRepository) StoreMusicFolder() error {
sq := Update(r.tableName).Set("path", conf.Server.MusicFolder).Set("updated_at", time.Now()).
Where(Eq{"id": hardCodedMusicFolderID})
_, err := r.executeSQL(sq)
return err
}
func (r *libraryRepository) AddArtist(id int, artistID string) error {
sq := Insert("library_artist").Columns("library_id", "artist_id").Values(id, artistID).
Suffix(`on conflict(library_id, artist_id) do nothing`)
_, err := r.executeSQL(sq)
if err != nil {
return err
}
return nil
}
func (r *libraryRepository) UpdateLastScan(id int, t time.Time) error {
sq := Update(r.tableName).Set("last_scan_at", t).Where(Eq{"id": id})
_, err := r.executeSQL(sq)
return err
}