拆分LibraryRepository
All checks were successful
Go CI / test-and-build (push) Successful in 11s

This commit is contained in:
2026-04-08 14:42:52 +08:00
parent 9637ad2476
commit 55c017f60d
8 changed files with 414 additions and 377 deletions

View File

@@ -38,7 +38,7 @@ func (r *SongRepository) GetAll() ([]model.Song, error) {
return songs, nil
}
func (r *SongRepository) GetAllWithDetails() ([]SongDetail, error) {
func (r *SongRepository) GetAllWithDetails() ([]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
FROM songs s
@@ -52,9 +52,9 @@ func (r *SongRepository) GetAllWithDetails() ([]SongDetail, error) {
}
defer rows.Close()
songs := []SongDetail{}
songs := []model.SongDetail{}
for rows.Next() {
var song SongDetail
var song model.SongDetail
if err := rows.Scan(&song.ID, &song.Title, &song.Artist, &song.Album, &song.Duration, &song.Path); err != nil {
return nil, err
}
@@ -94,3 +94,27 @@ func (r *SongRepository) Get(id int) (model.Song, error) {
}
return song, 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 (?, ?, ?, ?, ?)",
title, artistID, albumID, duration, mediaFileID,
)
if err != nil {
return model.Song{}, err
}
id, err := result.LastInsertId()
if err != nil {
return model.Song{}, err
}
return model.Song{
ID: int(id),
Title: title,
ArtistID: artistID,
AlbumID: albumID,
Duration: duration,
MediaFileID: mediaFileID,
}, nil
}