This commit is contained in:
@@ -78,8 +78,41 @@ func (r *LibraryRepository) UpdatePath(id int, path string) error {
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) Delete(id int) error {
|
||||
_, err := r.db.Exec("DELETE FROM libraries WHERE id = ?", id)
|
||||
return err
|
||||
tx, err := r.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
// Delete the library — CASCADE removes media_files and songs automatically
|
||||
_, err = tx.Exec("DELETE FROM libraries WHERE id = ?", id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Clean up orphan albums (no songs reference them)
|
||||
_, err = tx.Exec(`
|
||||
DELETE FROM albums WHERE id NOT IN (
|
||||
SELECT DISTINCT album_id FROM songs WHERE album_id IS NOT NULL
|
||||
)
|
||||
`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Clean up orphan artists (no songs and no albums reference them)
|
||||
_, err = tx.Exec(`
|
||||
DELETE FROM artists WHERE id NOT IN (
|
||||
SELECT DISTINCT artist_id FROM songs WHERE artist_id IS NOT NULL
|
||||
UNION
|
||||
SELECT DISTINCT artist_id FROM albums WHERE artist_id IS NOT NULL
|
||||
)
|
||||
`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) GetSongsByLibraryWithDetails(libraryID int) ([]SongDetail, error) {
|
||||
|
||||
@@ -12,15 +12,11 @@ func NewMediaRepository(db *sql.DB) *MediaRepository {
|
||||
|
||||
func (r *MediaRepository) Get(id int) (MediaFile, error) {
|
||||
var m MediaFile
|
||||
rows, err := r.db.Query("SELECT id, path, library_id FROM media_files WHERE id = ?", id)
|
||||
err := r.db.QueryRow("SELECT id, path, library_id FROM media_files WHERE id = ?", id).Scan(
|
||||
&m.ID, &m.Path, &m.LibraryID,
|
||||
)
|
||||
if err != nil {
|
||||
return MediaFile{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
if rows.Next() {
|
||||
if err := rows.Scan(&m.ID, &m.Path, &m.LibraryID); err != nil {
|
||||
return MediaFile{}, err
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@@ -55,13 +55,11 @@ func (r *SongRepository) GetAllWithDetails() ([]SongDetail, error) {
|
||||
|
||||
func (r *SongRepository) Get(id int) (Song, error) {
|
||||
var song Song
|
||||
rows, err := r.db.Query("SELECT id, title, artist_id, album_id, duration, media_file_id FROM songs WHERE id = ?", id)
|
||||
err := r.db.QueryRow("SELECT id, title, artist_id, album_id, duration, media_file_id FROM songs WHERE id = ?", id).Scan(
|
||||
&song.ID, &song.Title, &song.ArtistID, &song.AlbumID, &song.Duration, &song.MediaFileID,
|
||||
)
|
||||
if err != nil {
|
||||
return Song{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
if rows.Next() {
|
||||
rows.Scan(&song.ID, &song.Title, &song.ArtistID, &song.AlbumID, &song.Duration, &song.MediaFileID)
|
||||
}
|
||||
return song, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user