This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package repository
|
||||
|
||||
import "database/sql"
|
||||
import (
|
||||
"butterfliu/internal/model"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type SongRepository struct {
|
||||
db *sql.DB
|
||||
@@ -10,17 +13,24 @@ func NewSongRepository(db *sql.DB) *SongRepository {
|
||||
return &SongRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *SongRepository) GetAll() ([]Song, error) {
|
||||
rows, err := r.db.Query("SELECT id, title, artist_id, album_id, duration, media_file_id FROM songs")
|
||||
func (r *SongRepository) GetAll() ([]model.Song, error) {
|
||||
rows, err := r.db.Query(`
|
||||
SELECT id, title, artist_id, album_id, duration, media_file_id,
|
||||
COALESCE(track_number, 0), COALESCE(disc_number, 0), COALESCE(genre, '')
|
||||
FROM songs
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
songs := []Song{}
|
||||
songs := []model.Song{}
|
||||
for rows.Next() {
|
||||
var song Song
|
||||
if err := rows.Scan(&song.ID, &song.Title, &song.ArtistID, &song.AlbumID, &song.Duration, &song.MediaFileID); err != nil {
|
||||
var song model.Song
|
||||
if err := rows.Scan(
|
||||
&song.ID, &song.Title, &song.ArtistID, &song.AlbumID, &song.Duration, &song.MediaFileID,
|
||||
&song.TrackNumber, &song.DiscNumber, &song.Genre,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
songs = append(songs, song)
|
||||
@@ -53,13 +63,18 @@ func (r *SongRepository) GetAllWithDetails() ([]SongDetail, error) {
|
||||
return songs, nil
|
||||
}
|
||||
|
||||
func (r *SongRepository) Get(id int) (Song, error) {
|
||||
var song Song
|
||||
err := r.db.QueryRow("SELECT id, title, artist_id, album_id, duration, media_file_id FROM songs WHERE id = ?", id).Scan(
|
||||
func (r *SongRepository) Get(id int) (model.Song, error) {
|
||||
var song model.Song
|
||||
err := r.db.QueryRow(`
|
||||
SELECT id, title, artist_id, album_id, duration, media_file_id,
|
||||
COALESCE(track_number, 0), COALESCE(disc_number, 0), COALESCE(genre, '')
|
||||
FROM songs WHERE id = ?
|
||||
`, id).Scan(
|
||||
&song.ID, &song.Title, &song.ArtistID, &song.AlbumID, &song.Duration, &song.MediaFileID,
|
||||
&song.TrackNumber, &song.DiscNumber, &song.Genre,
|
||||
)
|
||||
if err != nil {
|
||||
return Song{}, err
|
||||
return model.Song{}, err
|
||||
}
|
||||
return song, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user