优化扫描
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
|
||||
|
||||
@@ -95,6 +95,18 @@ func (r *SongRepository) Get(id int) (model.Song, error) {
|
||||
return song, nil
|
||||
}
|
||||
|
||||
func (r *SongRepository) HasByMediaFileID(mediaFileID int) (bool, error) {
|
||||
var exists int
|
||||
err := r.db.QueryRow("SELECT 1 FROM songs WHERE media_file_id = ? LIMIT 1", mediaFileID).Scan(&exists)
|
||||
if err == sql.ErrNoRows {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (r *SongRepository) Create(title string, artistID, albumID, duration, mediaFileID int) (model.Song, error) {
|
||||
result, err := r.db.Exec(
|
||||
"INSERT INTO songs (title, artist_id, album_id, duration, media_file_id) VALUES (?, ?, ?, ?, ?)",
|
||||
|
||||
Reference in New Issue
Block a user