|
|
|
|
@@ -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{
|
|
|
|
|
ID: int(id),
|
|
|
|
|
Path: path,
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|