优化扫描
All checks were successful
Go CI / test-and-build (push) Successful in 14s
Web CI / lint-test-build (push) Successful in 28s

This commit is contained in:
2026-04-11 13:57:48 +08:00
parent 169b2c1858
commit 3f82e29c6c
11 changed files with 392 additions and 76 deletions

View File

@@ -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