This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user