This commit is contained in:
12
internal/model/dto.go
Normal file
12
internal/model/dto.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
// SongDetail is a DTO for API responses with joined artist/album names.
|
||||||
|
// This is not a database model but a presentation-layer struct.
|
||||||
|
type SongDetail struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Artist string `json:"artist"`
|
||||||
|
Album string `json:"album"`
|
||||||
|
Duration int `json:"duration"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
}
|
||||||
53
internal/model/model.go
Normal file
53
internal/model/model.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// Library represents a music library directory.
|
||||||
|
type Library struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// MediaFile represents a single audio file on disk.
|
||||||
|
type MediaFile struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
LibraryID int `json:"library_id"`
|
||||||
|
FileSize int64 `json:"file_size"`
|
||||||
|
Format string `json:"format"`
|
||||||
|
BitRate int `json:"bit_rate"`
|
||||||
|
SampleRate int `json:"sample_rate"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Artist represents a music artist.
|
||||||
|
type Artist struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Album represents a music album.
|
||||||
|
type Album struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
ArtistID int `json:"artist_id"`
|
||||||
|
Cover string `json:"cover"`
|
||||||
|
Year int `json:"year"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Song represents a single track within an album.
|
||||||
|
type Song struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
ArtistID int `json:"artist_id"`
|
||||||
|
AlbumID int `json:"album_id"`
|
||||||
|
Duration int `json:"duration"`
|
||||||
|
MediaFileID int `json:"media_file_id"`
|
||||||
|
TrackNumber int `json:"track_number"`
|
||||||
|
DiscNumber int `json:"disc_number"`
|
||||||
|
Genre string `json:"genre"`
|
||||||
|
}
|
||||||
@@ -1,17 +1,14 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"butterfliu/internal/model"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SongDetail struct {
|
// SongDetail is a DTO for API responses with joined artist/album names.
|
||||||
ID int `json:"id"`
|
// Kept here temporarily for backward compatibility with service layer.
|
||||||
Title string `json:"title"`
|
// Consider moving to model package in future refactoring.
|
||||||
Artist string `json:"artist"`
|
type SongDetail = model.SongDetail
|
||||||
Album string `json:"album"`
|
|
||||||
Duration int `json:"duration"`
|
|
||||||
Path string `json:"path"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type LibraryRepository struct {
|
type LibraryRepository struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
@@ -21,17 +18,17 @@ func NewLibraryRepository(db *sql.DB) *LibraryRepository {
|
|||||||
return &LibraryRepository{db: db}
|
return &LibraryRepository{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) GetAll() ([]Library, error) {
|
func (r *LibraryRepository) GetAll() ([]model.Library, error) {
|
||||||
rows, err := r.db.Query("SELECT id, name, path FROM libraries")
|
rows, err := r.db.Query("SELECT id, name, path, COALESCE(created_at, '1970-01-01T00:00:00Z'), COALESCE(updated_at, '1970-01-01T00:00:00Z') FROM libraries")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
libraries := []Library{}
|
libraries := []model.Library{}
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var lib Library
|
var lib model.Library
|
||||||
if err := rows.Scan(&lib.ID, &lib.Name, &lib.Path); err != nil {
|
if err := rows.Scan(&lib.ID, &lib.Name, &lib.Path, &lib.CreatedAt, &lib.UpdatedAt); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
libraries = append(libraries, lib)
|
libraries = append(libraries, lib)
|
||||||
@@ -40,27 +37,27 @@ func (r *LibraryRepository) GetAll() ([]Library, error) {
|
|||||||
return libraries, nil
|
return libraries, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) GetByID(id int) (Library, error) {
|
func (r *LibraryRepository) GetByID(id int) (model.Library, error) {
|
||||||
row := r.db.QueryRow("SELECT id, name, path FROM libraries WHERE id = ?", id)
|
row := r.db.QueryRow("SELECT id, name, path, COALESCE(created_at, '1970-01-01T00:00:00Z'), COALESCE(updated_at, '1970-01-01T00:00:00Z') FROM libraries WHERE id = ?", id)
|
||||||
var lib Library
|
var lib model.Library
|
||||||
if err := row.Scan(&lib.ID, &lib.Name, &lib.Path); err != nil {
|
if err := row.Scan(&lib.ID, &lib.Name, &lib.Path, &lib.CreatedAt, &lib.UpdatedAt); err != nil {
|
||||||
return Library{}, err
|
return model.Library{}, err
|
||||||
}
|
}
|
||||||
return lib, nil
|
return lib, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) Create(name, path string) (Library, error) {
|
func (r *LibraryRepository) Create(name, path string) (model.Library, error) {
|
||||||
result, err := r.db.Exec("INSERT INTO libraries (name, path) VALUES (?, ?)", name, path)
|
result, err := r.db.Exec("INSERT INTO libraries (name, path) VALUES (?, ?)", name, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Library{}, err
|
return model.Library{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := result.LastInsertId()
|
id, err := result.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Library{}, err
|
return model.Library{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return Library{
|
return model.Library{
|
||||||
ID: int(id),
|
ID: int(id),
|
||||||
Name: name,
|
Name: name,
|
||||||
Path: path,
|
Path: path,
|
||||||
@@ -68,12 +65,12 @@ func (r *LibraryRepository) Create(name, path string) (Library, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) UpdateName(id int, name string) error {
|
func (r *LibraryRepository) UpdateName(id int, name string) error {
|
||||||
_, err := r.db.Exec("UPDATE libraries SET name = ? WHERE id = ?", name, id)
|
_, err := r.db.Exec("UPDATE libraries SET name = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?", name, id)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) UpdatePath(id int, path string) error {
|
func (r *LibraryRepository) UpdatePath(id int, path string) error {
|
||||||
_, err := r.db.Exec("UPDATE libraries SET path = ? WHERE id = ?", path, id)
|
_, err := r.db.Exec("UPDATE libraries SET path = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?", path, id)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,97 +138,116 @@ func (r *LibraryRepository) GetSongsByLibraryWithDetails(libraryID int) ([]SongD
|
|||||||
return songs, nil
|
return songs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) GetMediaFileByPath(path string) (MediaFile, error) {
|
func (r *LibraryRepository) GetMediaFileByPath(path string) (model.MediaFile, error) {
|
||||||
row := r.db.QueryRow("SELECT id, path, library_id FROM media_files WHERE path = ?", path)
|
row := r.db.QueryRow(`
|
||||||
var mediaFile MediaFile
|
SELECT id, path, library_id,
|
||||||
if err := row.Scan(&mediaFile.ID, &mediaFile.Path, &mediaFile.LibraryID); err != nil {
|
COALESCE(file_size, 0), COALESCE(format, ''),
|
||||||
return MediaFile{}, err
|
COALESCE(bit_rate, 0), COALESCE(sample_rate, 0),
|
||||||
|
COALESCE(created_at, '1970-01-01T00:00:00Z'),
|
||||||
|
COALESCE(updated_at, '1970-01-01T00:00:00Z')
|
||||||
|
FROM media_files WHERE path = ?
|
||||||
|
`)
|
||||||
|
var mediaFile model.MediaFile
|
||||||
|
if err := row.Scan(
|
||||||
|
&mediaFile.ID, &mediaFile.Path, &mediaFile.LibraryID,
|
||||||
|
&mediaFile.FileSize, &mediaFile.Format,
|
||||||
|
&mediaFile.BitRate, &mediaFile.SampleRate,
|
||||||
|
&mediaFile.CreatedAt, &mediaFile.UpdatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return model.MediaFile{}, err
|
||||||
}
|
}
|
||||||
return mediaFile, nil
|
return mediaFile, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) CreateMediaFile(path string, libraryID int) (MediaFile, error) {
|
func (r *LibraryRepository) CreateMediaFile(path string, libraryID int) (model.MediaFile, error) {
|
||||||
result, err := r.db.Exec("INSERT INTO media_files (path, library_id) VALUES (?, ?)", path, libraryID)
|
result, err := r.db.Exec("INSERT INTO media_files (path, library_id) VALUES (?, ?)", path, libraryID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MediaFile{}, err
|
return model.MediaFile{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := result.LastInsertId()
|
id, err := result.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MediaFile{}, err
|
return model.MediaFile{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return MediaFile{
|
return model.MediaFile{
|
||||||
ID: int(id),
|
ID: int(id),
|
||||||
Path: path,
|
Path: path,
|
||||||
|
LibraryID: libraryID,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) GetArtistByName(name string) (Artist, error) {
|
func (r *LibraryRepository) GetArtistByName(name string) (model.Artist, error) {
|
||||||
row := r.db.QueryRow("SELECT id, name FROM artists WHERE name = ?", name)
|
row := r.db.QueryRow("SELECT id, name FROM artists WHERE name = ?", name)
|
||||||
var artist Artist
|
var artist model.Artist
|
||||||
if err := row.Scan(&artist.ID, &artist.Name); err != nil {
|
if err := row.Scan(&artist.ID, &artist.Name); err != nil {
|
||||||
return Artist{}, err
|
return model.Artist{}, err
|
||||||
}
|
}
|
||||||
return artist, nil
|
return artist, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) CreateArtist(name string) (Artist, error) {
|
func (r *LibraryRepository) CreateArtist(name string) (model.Artist, error) {
|
||||||
result, err := r.db.Exec("INSERT INTO artists (name) VALUES (?)", name)
|
result, err := r.db.Exec("INSERT INTO artists (name) VALUES (?)", name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Artist{}, err
|
return model.Artist{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := result.LastInsertId()
|
id, err := result.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Artist{}, err
|
return model.Artist{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return Artist{
|
return model.Artist{
|
||||||
ID: int(id),
|
ID: int(id),
|
||||||
Name: name,
|
Name: name,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) GetAlbumByTitleAndArtist(title string, artistID int) (Album, error) {
|
func (r *LibraryRepository) GetAlbumByTitleAndArtist(title string, artistID int) (model.Album, error) {
|
||||||
row := r.db.QueryRow("SELECT id, title, artist_id FROM albums WHERE title = ? AND artist_id = ?", title, artistID)
|
row := r.db.QueryRow(`
|
||||||
var album Album
|
SELECT id, title, artist_id, COALESCE(cover, ''), COALESCE(year, 0)
|
||||||
if err := row.Scan(&album.ID, &album.Title, &album.ArtistID); err != nil {
|
FROM albums WHERE title = ? AND artist_id = ?
|
||||||
return Album{}, err
|
`)
|
||||||
|
var album model.Album
|
||||||
|
if err := row.Scan(&album.ID, &album.Title, &album.ArtistID, &album.Cover, &album.Year); err != nil {
|
||||||
|
return model.Album{}, err
|
||||||
}
|
}
|
||||||
return album, nil
|
return album, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) CreateAlbum(title string, artistID int) (Album, error) {
|
func (r *LibraryRepository) CreateAlbum(title string, artistID int) (model.Album, error) {
|
||||||
result, err := r.db.Exec("INSERT INTO albums (title, artist_id) VALUES (?, ?)", title, artistID)
|
result, err := r.db.Exec("INSERT INTO albums (title, artist_id) VALUES (?, ?)", title, artistID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Album{}, err
|
return model.Album{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := result.LastInsertId()
|
id, err := result.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Album{}, err
|
return model.Album{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return Album{
|
return model.Album{
|
||||||
ID: int(id),
|
ID: int(id),
|
||||||
Title: title,
|
Title: title,
|
||||||
ArtistID: artistID,
|
ArtistID: artistID,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) CreateSong(title string, artistID, albumID, duration, mediaFileID int) (Song, error) {
|
func (r *LibraryRepository) CreateSong(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)
|
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 {
|
if err != nil {
|
||||||
return Song{}, err
|
return model.Song{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := result.LastInsertId()
|
id, err := result.LastInsertId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Song{}, err
|
return model.Song{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return Song{
|
return model.Song{
|
||||||
ID: int(id),
|
ID: int(id),
|
||||||
Title: title,
|
Title: title,
|
||||||
ArtistID: artistID,
|
ArtistID: artistID,
|
||||||
@@ -241,16 +257,16 @@ func (r *LibraryRepository) CreateSong(title string, artistID, albumID, duration
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) GetArtists() ([]Artist, error) {
|
func (r *LibraryRepository) GetArtists() ([]model.Artist, error) {
|
||||||
rows, err := r.db.Query("SELECT id, name FROM artists")
|
rows, err := r.db.Query("SELECT id, name FROM artists")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
artists := []Artist{}
|
artists := []model.Artist{}
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var artist Artist
|
var artist model.Artist
|
||||||
if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
|
if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -259,16 +275,16 @@ func (r *LibraryRepository) GetArtists() ([]Artist, error) {
|
|||||||
return artists, nil
|
return artists, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *LibraryRepository) GetAlbums() ([]Album, error) {
|
func (r *LibraryRepository) GetAlbums() ([]model.Album, error) {
|
||||||
rows, err := r.db.Query("SELECT id, title, artist_id FROM albums")
|
rows, err := r.db.Query("SELECT id, title, artist_id FROM albums")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
albums := []Album{}
|
albums := []model.Album{}
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var album Album
|
var album model.Album
|
||||||
if err := rows.Scan(&album.ID, &album.Title, &album.ArtistID); err != nil {
|
if err := rows.Scan(&album.ID, &album.Title, &album.ArtistID); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import "database/sql"
|
import (
|
||||||
|
"butterfliu/internal/model"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
type MediaRepository struct {
|
type MediaRepository struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
@@ -10,13 +13,13 @@ func NewMediaRepository(db *sql.DB) *MediaRepository {
|
|||||||
return &MediaRepository{db: db}
|
return &MediaRepository{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *MediaRepository) Get(id int) (MediaFile, error) {
|
func (r *MediaRepository) Get(id int) (model.MediaFile, error) {
|
||||||
var m MediaFile
|
var m model.MediaFile
|
||||||
err := r.db.QueryRow("SELECT id, path, library_id FROM media_files WHERE id = ?", id).Scan(
|
err := r.db.QueryRow("SELECT id, path, library_id FROM media_files WHERE id = ?", id).Scan(
|
||||||
&m.ID, &m.Path, &m.LibraryID,
|
&m.ID, &m.Path, &m.LibraryID,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MediaFile{}, err
|
return model.MediaFile{}, err
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
package repository
|
|
||||||
|
|
||||||
type Library struct {
|
|
||||||
ID int `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Path string `json:"path"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type MediaFile struct {
|
|
||||||
ID int `json:"id"`
|
|
||||||
Path string `json:"path"`
|
|
||||||
LibraryID int `json:"library_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Artist struct {
|
|
||||||
ID int `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Album struct {
|
|
||||||
ID int `json:"id"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
ArtistID int `json:"artist_id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Song struct {
|
|
||||||
ID int `json:"id"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
ArtistID int `json:"artist_id"`
|
|
||||||
AlbumID int `json:"album_id"`
|
|
||||||
Duration int `json:"duration"`
|
|
||||||
MediaFileID int `json:"media_file_id"`
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import "database/sql"
|
import (
|
||||||
|
"butterfliu/internal/model"
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
type SongRepository struct {
|
type SongRepository struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
@@ -10,17 +13,24 @@ func NewSongRepository(db *sql.DB) *SongRepository {
|
|||||||
return &SongRepository{db: db}
|
return &SongRepository{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *SongRepository) GetAll() ([]Song, error) {
|
func (r *SongRepository) GetAll() ([]model.Song, error) {
|
||||||
rows, err := r.db.Query("SELECT id, title, artist_id, album_id, duration, media_file_id FROM songs")
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
songs := []Song{}
|
songs := []model.Song{}
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var song Song
|
var song model.Song
|
||||||
if err := rows.Scan(&song.ID, &song.Title, &song.ArtistID, &song.AlbumID, &song.Duration, &song.MediaFileID); err != nil {
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
songs = append(songs, song)
|
songs = append(songs, song)
|
||||||
@@ -53,13 +63,18 @@ func (r *SongRepository) GetAllWithDetails() ([]SongDetail, error) {
|
|||||||
return songs, nil
|
return songs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *SongRepository) Get(id int) (Song, error) {
|
func (r *SongRepository) Get(id int) (model.Song, error) {
|
||||||
var song Song
|
var song model.Song
|
||||||
err := r.db.QueryRow("SELECT id, title, artist_id, album_id, duration, media_file_id FROM songs WHERE id = ?", id).Scan(
|
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.ID, &song.Title, &song.ArtistID, &song.AlbumID, &song.Duration, &song.MediaFileID,
|
||||||
|
&song.TrackNumber, &song.DiscNumber, &song.Genre,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Song{}, err
|
return model.Song{}, err
|
||||||
}
|
}
|
||||||
return song, nil
|
return song, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"butterfliu/internal/model"
|
||||||
"butterfliu/internal/repository"
|
"butterfliu/internal/repository"
|
||||||
"butterfliu/internal/scanner"
|
"butterfliu/internal/scanner"
|
||||||
"errors"
|
"errors"
|
||||||
@@ -26,20 +27,20 @@ func NewLibraryService(repo *repository.LibraryRepository) *LibraryService {
|
|||||||
return &LibraryService{repo: repo}
|
return &LibraryService{repo: repo}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LibraryService) GetAll() ([]repository.Library, error) {
|
func (s *LibraryService) GetAll() ([]model.Library, error) {
|
||||||
return s.repo.GetAll()
|
return s.repo.GetAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LibraryService) GetByID(id int) (repository.Library, error) {
|
func (s *LibraryService) GetByID(id int) (model.Library, error) {
|
||||||
return s.repo.GetByID(id)
|
return s.repo.GetByID(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LibraryService) Create(name, path string) (repository.Library, error) {
|
func (s *LibraryService) Create(name, path string) (model.Library, error) {
|
||||||
if name = strings.TrimSpace(name); name == "" {
|
if name = strings.TrimSpace(name); name == "" {
|
||||||
return repository.Library{}, errors.New("library name cannot be empty")
|
return model.Library{}, errors.New("library name cannot be empty")
|
||||||
}
|
}
|
||||||
if path = strings.TrimSpace(path); path == "" {
|
if path = strings.TrimSpace(path); path == "" {
|
||||||
return repository.Library{}, errors.New("library path cannot be empty")
|
return model.Library{}, errors.New("library path cannot be empty")
|
||||||
}
|
}
|
||||||
return s.repo.Create(name, path)
|
return s.repo.Create(name, path)
|
||||||
}
|
}
|
||||||
@@ -66,11 +67,11 @@ func (s *LibraryService) GetSongs(id int) ([]repository.SongDetail, error) {
|
|||||||
return s.repo.GetSongsByLibraryWithDetails(id)
|
return s.repo.GetSongsByLibraryWithDetails(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LibraryService) GetArtists() ([]repository.Artist, error) {
|
func (s *LibraryService) GetArtists() ([]model.Artist, error) {
|
||||||
return s.repo.GetArtists()
|
return s.repo.GetArtists()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LibraryService) GetAlbums() ([]repository.Album, error) {
|
func (s *LibraryService) GetAlbums() ([]model.Album, error) {
|
||||||
return s.repo.GetAlbums()
|
return s.repo.GetAlbums()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,7 +124,7 @@ func (s *LibraryService) addScannedSong(song scanner.ScannedSong, libraryID int,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LibraryService) getOrCreateMediaFile(path string, libraryID int) (repository.MediaFile, error) {
|
func (s *LibraryService) getOrCreateMediaFile(path string, libraryID int) (model.MediaFile, error) {
|
||||||
mf, err := s.repo.GetMediaFileByPath(path)
|
mf, err := s.repo.GetMediaFileByPath(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return mf, nil
|
return mf, nil
|
||||||
@@ -131,7 +132,7 @@ func (s *LibraryService) getOrCreateMediaFile(path string, libraryID int) (repos
|
|||||||
return s.repo.CreateMediaFile(path, libraryID)
|
return s.repo.CreateMediaFile(path, libraryID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LibraryService) getOrCreateArtist(name string) (repository.Artist, error) {
|
func (s *LibraryService) getOrCreateArtist(name string) (model.Artist, error) {
|
||||||
a, err := s.repo.GetArtistByName(name)
|
a, err := s.repo.GetArtistByName(name)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return a, nil
|
return a, nil
|
||||||
@@ -139,7 +140,7 @@ func (s *LibraryService) getOrCreateArtist(name string) (repository.Artist, erro
|
|||||||
return s.repo.CreateArtist(name)
|
return s.repo.CreateArtist(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LibraryService) getOrCreateAlbum(title string, artistID int) (repository.Album, error) {
|
func (s *LibraryService) getOrCreateAlbum(title string, artistID int) (model.Album, error) {
|
||||||
al, err := s.repo.GetAlbumByTitleAndArtist(title, artistID)
|
al, err := s.repo.GetAlbumByTitleAndArtist(title, artistID)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return al, nil
|
return al, nil
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"butterfliu/internal/model"
|
||||||
"butterfliu/internal/repository"
|
"butterfliu/internal/repository"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,10 +22,10 @@ func (s *SongService) GetAllWithDetails() ([]repository.SongDetail, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetMediaFilePath returns the file path of a song by its song ID.
|
// GetMediaFilePath returns the file path of a song by its song ID.
|
||||||
func (s *SongService) GetMediaFile(id int) (repository.MediaFile, error) {
|
func (s *SongService) GetMediaFile(id int) (model.MediaFile, error) {
|
||||||
song, err := s.songRepo.Get(id)
|
song, err := s.songRepo.Get(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return repository.MediaFile{}, err
|
return model.MediaFile{}, err
|
||||||
}
|
}
|
||||||
return s.mediaRepo.Get(song.MediaFileID)
|
return s.mediaRepo.Get(song.MediaFileID)
|
||||||
}
|
}
|
||||||
|
|||||||
215
migrations/003_add_metadata_fields.go
Normal file
215
migrations/003_add_metadata_fields.go
Normal file
@@ -0,0 +1,215 @@
|
|||||||
|
package migrations
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterMigration(
|
||||||
|
3,
|
||||||
|
"Add metadata fields and timestamps",
|
||||||
|
migrateAddMetadataUp,
|
||||||
|
migrateAddMetadataDown,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func migrateAddMetadataUp(tx *sql.Tx) error {
|
||||||
|
// Add timestamps to libraries
|
||||||
|
_, err := tx.Exec(`
|
||||||
|
ALTER TABLE libraries ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
ALTER TABLE libraries ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add metadata and timestamps to media_files
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
ALTER TABLE media_files ADD COLUMN file_size INTEGER DEFAULT 0
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
ALTER TABLE media_files ADD COLUMN format TEXT DEFAULT ''
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
ALTER TABLE media_files ADD COLUMN bit_rate INTEGER DEFAULT 0
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
ALTER TABLE media_files ADD COLUMN sample_rate INTEGER DEFAULT 0
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
ALTER TABLE media_files ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
ALTER TABLE media_files ADD COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add fields to albums
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
ALTER TABLE albums ADD COLUMN cover TEXT DEFAULT ''
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
ALTER TABLE albums ADD COLUMN year INTEGER DEFAULT 0
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add fields to songs
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
ALTER TABLE songs ADD COLUMN track_number INTEGER DEFAULT 0
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
ALTER TABLE songs ADD COLUMN disc_number INTEGER DEFAULT 0
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
ALTER TABLE songs ADD COLUMN genre TEXT DEFAULT ''
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func migrateAddMetadataDown(tx *sql.Tx) error {
|
||||||
|
// SQLite doesn't support DROP COLUMN in older versions, so we need to rebuild tables
|
||||||
|
|
||||||
|
// Rebuild libraries
|
||||||
|
_, err := tx.Exec(`
|
||||||
|
CREATE TABLE libraries_old (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
path TEXT NOT NULL
|
||||||
|
)
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`INSERT INTO libraries_old SELECT id, name, path FROM libraries`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec("DROP TABLE libraries")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec("ALTER TABLE libraries_old RENAME TO libraries")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rebuild media_files
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
CREATE TABLE media_files_old (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
path TEXT NOT NULL,
|
||||||
|
library_id INTEGER NOT NULL,
|
||||||
|
FOREIGN KEY (library_id) REFERENCES libraries(id) ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`INSERT INTO media_files_old SELECT id, path, library_id FROM media_files`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec("DROP TABLE media_files")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec("ALTER TABLE media_files_old RENAME TO media_files")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rebuild albums
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
CREATE TABLE albums_old (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
artist_id INTEGER,
|
||||||
|
FOREIGN KEY (artist_id) REFERENCES artists(id),
|
||||||
|
UNIQUE(title, artist_id)
|
||||||
|
)
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`INSERT INTO albums_old SELECT id, title, artist_id FROM albums`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec("DROP TABLE albums")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec("ALTER TABLE albums_old RENAME TO albums")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rebuild songs
|
||||||
|
_, err = tx.Exec(`
|
||||||
|
CREATE TABLE songs_old (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
album_id INTEGER,
|
||||||
|
artist_id INTEGER,
|
||||||
|
duration INTEGER,
|
||||||
|
media_file_id INTEGER UNIQUE,
|
||||||
|
FOREIGN KEY (album_id) REFERENCES albums(id),
|
||||||
|
FOREIGN KEY (artist_id) REFERENCES artists(id),
|
||||||
|
FOREIGN KEY (media_file_id) REFERENCES media_files(id) ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`INSERT INTO songs_old SELECT id, title, album_id, artist_id, duration, media_file_id FROM songs`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec("DROP TABLE songs")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec("ALTER TABLE songs_old RENAME TO songs")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user