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
|
||||
|
||||
import (
|
||||
"butterfliu/internal/model"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
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"`
|
||||
}
|
||||
// SongDetail is a DTO for API responses with joined artist/album names.
|
||||
// Kept here temporarily for backward compatibility with service layer.
|
||||
// Consider moving to model package in future refactoring.
|
||||
type SongDetail = model.SongDetail
|
||||
|
||||
type LibraryRepository struct {
|
||||
db *sql.DB
|
||||
@@ -21,17 +18,17 @@ func NewLibraryRepository(db *sql.DB) *LibraryRepository {
|
||||
return &LibraryRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) GetAll() ([]Library, error) {
|
||||
rows, err := r.db.Query("SELECT id, name, path FROM libraries")
|
||||
func (r *LibraryRepository) GetAll() ([]model.Library, error) {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
libraries := []Library{}
|
||||
libraries := []model.Library{}
|
||||
for rows.Next() {
|
||||
var lib Library
|
||||
if err := rows.Scan(&lib.ID, &lib.Name, &lib.Path); err != nil {
|
||||
var lib model.Library
|
||||
if err := rows.Scan(&lib.ID, &lib.Name, &lib.Path, &lib.CreatedAt, &lib.UpdatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
libraries = append(libraries, lib)
|
||||
@@ -40,27 +37,27 @@ func (r *LibraryRepository) GetAll() ([]Library, error) {
|
||||
return libraries, nil
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) GetByID(id int) (Library, error) {
|
||||
row := r.db.QueryRow("SELECT id, name, path FROM libraries WHERE id = ?", id)
|
||||
var lib Library
|
||||
if err := row.Scan(&lib.ID, &lib.Name, &lib.Path); err != nil {
|
||||
return Library{}, err
|
||||
func (r *LibraryRepository) GetByID(id int) (model.Library, error) {
|
||||
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 model.Library
|
||||
if err := row.Scan(&lib.ID, &lib.Name, &lib.Path, &lib.CreatedAt, &lib.UpdatedAt); err != nil {
|
||||
return model.Library{}, err
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
return Library{}, err
|
||||
return model.Library{}, err
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return Library{}, err
|
||||
return model.Library{}, err
|
||||
}
|
||||
|
||||
return Library{
|
||||
return model.Library{
|
||||
ID: int(id),
|
||||
Name: name,
|
||||
Path: path,
|
||||
@@ -68,12 +65,12 @@ func (r *LibraryRepository) Create(name, path string) (Library, 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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -141,97 +138,116 @@ func (r *LibraryRepository) GetSongsByLibraryWithDetails(libraryID int) ([]SongD
|
||||
return songs, nil
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) GetMediaFileByPath(path string) (MediaFile, error) {
|
||||
row := r.db.QueryRow("SELECT id, path, library_id FROM media_files WHERE path = ?", path)
|
||||
var mediaFile MediaFile
|
||||
if err := row.Scan(&mediaFile.ID, &mediaFile.Path, &mediaFile.LibraryID); err != nil {
|
||||
return MediaFile{}, err
|
||||
func (r *LibraryRepository) GetMediaFileByPath(path string) (model.MediaFile, error) {
|
||||
row := r.db.QueryRow(`
|
||||
SELECT id, path, library_id,
|
||||
COALESCE(file_size, 0), COALESCE(format, ''),
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return MediaFile{}, err
|
||||
return model.MediaFile{}, err
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return MediaFile{}, err
|
||||
return model.MediaFile{}, err
|
||||
}
|
||||
|
||||
return MediaFile{
|
||||
return model.MediaFile{
|
||||
ID: int(id),
|
||||
Path: path,
|
||||
LibraryID: libraryID,
|
||||
}, 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)
|
||||
var artist Artist
|
||||
var artist model.Artist
|
||||
if err := row.Scan(&artist.ID, &artist.Name); err != nil {
|
||||
return Artist{}, err
|
||||
return model.Artist{}, err
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
return Artist{}, err
|
||||
return model.Artist{}, err
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return Artist{}, err
|
||||
return model.Artist{}, err
|
||||
}
|
||||
|
||||
return Artist{
|
||||
return model.Artist{
|
||||
ID: int(id),
|
||||
Name: name,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) GetAlbumByTitleAndArtist(title string, artistID int) (Album, error) {
|
||||
row := r.db.QueryRow("SELECT id, title, artist_id FROM albums WHERE title = ? AND artist_id = ?", title, artistID)
|
||||
var album Album
|
||||
if err := row.Scan(&album.ID, &album.Title, &album.ArtistID); err != nil {
|
||||
return Album{}, err
|
||||
func (r *LibraryRepository) GetAlbumByTitleAndArtist(title string, artistID int) (model.Album, error) {
|
||||
row := r.db.QueryRow(`
|
||||
SELECT id, title, artist_id, COALESCE(cover, ''), COALESCE(year, 0)
|
||||
FROM albums WHERE title = ? AND artist_id = ?
|
||||
`)
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return Album{}, err
|
||||
return model.Album{}, err
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return Album{}, err
|
||||
return model.Album{}, err
|
||||
}
|
||||
|
||||
return Album{
|
||||
return model.Album{
|
||||
ID: int(id),
|
||||
Title: title,
|
||||
ArtistID: artistID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) CreateSong(title string, artistID, albumID, duration, mediaFileID int) (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)
|
||||
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,
|
||||
)
|
||||
if err != nil {
|
||||
return Song{}, err
|
||||
return model.Song{}, err
|
||||
}
|
||||
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return Song{}, err
|
||||
return model.Song{}, err
|
||||
}
|
||||
|
||||
return Song{
|
||||
return model.Song{
|
||||
ID: int(id),
|
||||
Title: title,
|
||||
ArtistID: artistID,
|
||||
@@ -241,16 +257,16 @@ func (r *LibraryRepository) CreateSong(title string, artistID, albumID, duration
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *LibraryRepository) GetArtists() ([]Artist, error) {
|
||||
func (r *LibraryRepository) GetArtists() ([]model.Artist, error) {
|
||||
rows, err := r.db.Query("SELECT id, name FROM artists")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
artists := []Artist{}
|
||||
artists := []model.Artist{}
|
||||
for rows.Next() {
|
||||
var artist Artist
|
||||
var artist model.Artist
|
||||
if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -259,16 +275,16 @@ func (r *LibraryRepository) GetArtists() ([]Artist, error) {
|
||||
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")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
albums := []Album{}
|
||||
albums := []model.Album{}
|
||||
for rows.Next() {
|
||||
var album Album
|
||||
var album model.Album
|
||||
if err := rows.Scan(&album.ID, &album.Title, &album.ArtistID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package repository
|
||||
|
||||
import "database/sql"
|
||||
import (
|
||||
"butterfliu/internal/model"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type MediaRepository struct {
|
||||
db *sql.DB
|
||||
@@ -10,13 +13,13 @@ func NewMediaRepository(db *sql.DB) *MediaRepository {
|
||||
return &MediaRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *MediaRepository) Get(id int) (MediaFile, error) {
|
||||
var m MediaFile
|
||||
func (r *MediaRepository) Get(id int) (model.MediaFile, error) {
|
||||
var m model.MediaFile
|
||||
err := r.db.QueryRow("SELECT id, path, library_id FROM media_files WHERE id = ?", id).Scan(
|
||||
&m.ID, &m.Path, &m.LibraryID,
|
||||
)
|
||||
if err != nil {
|
||||
return MediaFile{}, err
|
||||
return model.MediaFile{}, err
|
||||
}
|
||||
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
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"butterfliu/internal/model"
|
||||
"butterfliu/internal/repository"
|
||||
"butterfliu/internal/scanner"
|
||||
"errors"
|
||||
@@ -26,20 +27,20 @@ func NewLibraryService(repo *repository.LibraryRepository) *LibraryService {
|
||||
return &LibraryService{repo: repo}
|
||||
}
|
||||
|
||||
func (s *LibraryService) GetAll() ([]repository.Library, error) {
|
||||
func (s *LibraryService) GetAll() ([]model.Library, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
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 == "" {
|
||||
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 == "" {
|
||||
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)
|
||||
}
|
||||
@@ -66,11 +67,11 @@ func (s *LibraryService) GetSongs(id int) ([]repository.SongDetail, error) {
|
||||
return s.repo.GetSongsByLibraryWithDetails(id)
|
||||
}
|
||||
|
||||
func (s *LibraryService) GetArtists() ([]repository.Artist, error) {
|
||||
func (s *LibraryService) GetArtists() ([]model.Artist, error) {
|
||||
return s.repo.GetArtists()
|
||||
}
|
||||
|
||||
func (s *LibraryService) GetAlbums() ([]repository.Album, error) {
|
||||
func (s *LibraryService) GetAlbums() ([]model.Album, error) {
|
||||
return s.repo.GetAlbums()
|
||||
}
|
||||
|
||||
@@ -123,7 +124,7 @@ func (s *LibraryService) addScannedSong(song scanner.ScannedSong, libraryID int,
|
||||
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)
|
||||
if err == nil {
|
||||
return mf, nil
|
||||
@@ -131,7 +132,7 @@ func (s *LibraryService) getOrCreateMediaFile(path string, libraryID int) (repos
|
||||
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)
|
||||
if err == nil {
|
||||
return a, nil
|
||||
@@ -139,7 +140,7 @@ func (s *LibraryService) getOrCreateArtist(name string) (repository.Artist, erro
|
||||
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)
|
||||
if err == nil {
|
||||
return al, nil
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"butterfliu/internal/model"
|
||||
"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.
|
||||
func (s *SongService) GetMediaFile(id int) (repository.MediaFile, error) {
|
||||
func (s *SongService) GetMediaFile(id int) (model.MediaFile, error) {
|
||||
song, err := s.songRepo.Get(id)
|
||||
if err != nil {
|
||||
return repository.MediaFile{}, err
|
||||
return model.MediaFile{}, err
|
||||
}
|
||||
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