Store MusicFolder as a library in DB

This commit is contained in:
Deluan 2023-12-14 20:55:43 -05:00
parent dbabb9f9bf
commit 34f87bedc3
5 changed files with 61 additions and 23 deletions

View File

@ -14,13 +14,13 @@ func init() {
func upAddLibraryTable(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, `
create table library (
id string primary key,
name text not null,
path text not null,
id integer primary key autoincrement,
name text not null unique,
path text not null unique,
remote_path text null default '',
last_scan_at datetime not null default '0000-00-00 00:00:00',
updated_at datetime not null,
created_at datetime not null
updated_at datetime not null default current_timestamp,
created_at datetime not null default current_timestamp
);`)
return err
}

View File

@ -7,7 +7,7 @@ import (
)
type Library struct {
ID int32
ID int
Name string
Path string
RemotePath string
@ -23,6 +23,7 @@ func (f Library) FS() fs.FS {
type Libraries []Library
type LibraryRepository interface {
Get(id int32) (*Library, error)
GetAll() (Libraries, error)
Get(id int) (*Library, error)
Put(*Library) error
GetAll(...QueryOptions) (Libraries, error)
}

View File

@ -2,33 +2,57 @@ package persistence
import (
"context"
"time"
"github.com/navidrome/navidrome/conf"
. "github.com/Masterminds/squirrel"
"github.com/navidrome/navidrome/model"
"github.com/pocketbase/dbx"
)
type libraryRepository struct {
ctx context.Context
sqlRepository
sqlRestful
}
func NewLibraryRepository(ctx context.Context, _ dbx.Builder) model.LibraryRepository {
return &libraryRepository{ctx}
func NewLibraryRepository(ctx context.Context, db dbx.Builder) model.LibraryRepository {
r := &libraryRepository{}
r.ctx = ctx
r.db = db
r.tableName = "library"
return r
}
func (r *libraryRepository) Get(id int32) (*model.Library, error) {
library := hardCoded()
return &library, nil
func (r *libraryRepository) Get(id int) (*model.Library, error) {
sq := r.newSelect().Columns("*").Where(Eq{"id": id})
var res model.Library
err := r.queryOne(sq, &res)
return &res, err
}
func (*libraryRepository) GetAll() (model.Libraries, error) {
return model.Libraries{hardCoded()}, nil
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(),
}
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`)
_, err := r.executeSQL(sq)
return err
}
func hardCoded() model.Library {
library := model.Library{ID: 0, Path: conf.Server.MusicFolder}
library.Name = "Music Library"
return library
func (r *libraryRepository) GetAll(ops ...model.QueryOptions) (model.Libraries, error) {
sq := r.newSelect(ops...).Columns("*")
res := model.Libraries{}
err := r.queryAll(sq, &res)
return res, err
}
var _ model.LibraryRepository = (*libraryRepository)(nil)

View File

@ -16,6 +16,10 @@ import (
func initialSetup(ds model.DataStore) {
_ = ds.WithTx(func(tx model.DataStore) error {
if err := createOrUpdateMusicFolder(ds); err != nil {
return err
}
properties := ds.Property(context.TODO())
_, err := properties.Get(consts.InitialSetupFlagKey)
if err == nil {
@ -112,3 +116,12 @@ func checkExternalCredentials() {
}
}
}
func createOrUpdateMusicFolder(ds model.DataStore) error {
lib := model.Library{ID: 1, Name: "Music Library", Path: conf.Server.MusicFolder}
err := ds.Library(context.TODO()).Put(&lib)
if err != nil {
log.Error("Could not access Library table", err)
}
return err
}

View File

@ -21,7 +21,7 @@ func (api *Router) GetMusicFolders(r *http.Request) (*responses.Subsonic, error)
libraries, _ := api.ds.Library(r.Context()).GetAll()
folders := make([]responses.MusicFolder, len(libraries))
for i, f := range libraries {
folders[i].Id = f.ID
folders[i].Id = int32(f.ID)
folders[i].Name = f.Name
}
response := newResponse()
@ -31,7 +31,7 @@ func (api *Router) GetMusicFolders(r *http.Request) (*responses.Subsonic, error)
func (api *Router) getArtistIndex(r *http.Request, libId int, ifModifiedSince time.Time) (*responses.Indexes, error) {
ctx := r.Context()
folder, err := api.ds.Library(ctx).Get(int32(libId))
folder, err := api.ds.Library(ctx).Get(libId)
if err != nil {
log.Error(ctx, "Error retrieving Library", "id", libId, err)
return nil, err