优化扫描
This commit is contained in:
@@ -5,6 +5,12 @@ import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type ExistingMediaFile struct {
|
||||
Path string
|
||||
MediaFileID int
|
||||
HasSong bool
|
||||
}
|
||||
|
||||
type MediaRepository struct {
|
||||
db *sql.DB
|
||||
}
|
||||
@@ -63,6 +69,31 @@ func (r *MediaRepository) Create(path string, libraryID int) (model.MediaFile, e
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *MediaRepository) ListExistingByLibrary(libraryID int) ([]ExistingMediaFile, error) {
|
||||
rows, err := r.db.Query(`
|
||||
SELECT mf.path, mf.id, CASE WHEN s.id IS NULL THEN 0 ELSE 1 END AS has_song
|
||||
FROM media_files mf
|
||||
LEFT JOIN songs s ON s.media_file_id = mf.id
|
||||
WHERE mf.library_id = ?
|
||||
`, libraryID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
existing := []ExistingMediaFile{}
|
||||
for rows.Next() {
|
||||
var item ExistingMediaFile
|
||||
var hasSong int
|
||||
if err := rows.Scan(&item.Path, &item.MediaFileID, &hasSong); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
item.HasSong = hasSong == 1
|
||||
existing = append(existing, item)
|
||||
}
|
||||
return existing, nil
|
||||
}
|
||||
|
||||
func (r *MediaRepository) GetSongsByLibraryWithDetails(libraryID int) ([]model.SongDetail, error) {
|
||||
rows, err := r.db.Query(`
|
||||
SELECT s.id, s.title, a.name as artist_name, al.title as album_title, s.duration, mf.path
|
||||
|
||||
Reference in New Issue
Block a user