拆分LibraryRepository
All checks were successful
Go CI / test-and-build (push) Successful in 11s

This commit is contained in:
2026-04-08 14:42:52 +08:00
parent 9637ad2476
commit 55c017f60d
8 changed files with 414 additions and 377 deletions

View File

@@ -5,11 +5,6 @@ import (
"database/sql"
)
// 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
}
@@ -19,7 +14,7 @@ func NewLibraryRepository(db *sql.DB) *LibraryRepository {
}
func (r *LibraryRepository) GetAll() ([]model.Library, error) {
rows, err := r.db.Query("SELECT id, name, path, created_at,updated_at FROM libraries")
rows, err := r.db.Query("SELECT id, name, path, created_at, updated_at FROM libraries")
if err != nil {
return nil, err
}
@@ -111,329 +106,3 @@ func (r *LibraryRepository) Delete(id int) error {
return tx.Commit()
}
func (r *LibraryRepository) GetSongsByLibraryWithDetails(libraryID int) ([]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
INNER JOIN media_files mf ON s.media_file_id = mf.id
INNER JOIN artists a ON s.artist_id = a.id
INNER JOIN albums al ON s.album_id = al.id
WHERE mf.library_id = ?
ORDER BY s.title
`, libraryID)
if err != nil {
return nil, err
}
defer rows.Close()
songs := []SongDetail{}
for rows.Next() {
var song SongDetail
if err := rows.Scan(&song.ID, &song.Title, &song.Artist, &song.Album, &song.Duration, &song.Path); err != nil {
return nil, err
}
songs = append(songs, song)
}
return songs, nil
}
func (r *LibraryRepository) GetSongsByArtistWithDetails(artistID int) ([]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
INNER JOIN media_files mf ON s.media_file_id = mf.id
INNER JOIN artists a ON s.artist_id = a.id
INNER JOIN albums al ON s.album_id = al.id
WHERE s.artist_id = ?
ORDER BY s.title
`, artistID)
if err != nil {
return nil, err
}
defer rows.Close()
songs := []SongDetail{}
for rows.Next() {
var song SongDetail
if err := rows.Scan(&song.ID, &song.Title, &song.Artist, &song.Album, &song.Duration, &song.Path); err != nil {
return nil, err
}
songs = append(songs, song)
}
return songs, nil
}
func (r *LibraryRepository) GetSongsByAlbumWithDetails(albumID int) ([]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
INNER JOIN media_files mf ON s.media_file_id = mf.id
INNER JOIN artists a ON s.artist_id = a.id
INNER JOIN albums al ON s.album_id = al.id
WHERE s.album_id = ?
ORDER BY s.title
`, albumID)
if err != nil {
return nil, err
}
defer rows.Close()
songs := []SongDetail{}
for rows.Next() {
var song SongDetail
if err := rows.Scan(&song.ID, &song.Title, &song.Artist, &song.Album, &song.Duration, &song.Path); err != nil {
return nil, err
}
songs = append(songs, song)
}
return songs, nil
}
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) (model.MediaFile, error) {
result, err := r.db.Exec("INSERT INTO media_files (path, library_id) VALUES (?, ?)", path, libraryID)
if err != nil {
return model.MediaFile{}, err
}
id, err := result.LastInsertId()
if err != nil {
return model.MediaFile{}, err
}
return model.MediaFile{
ID: int(id),
Path: path,
LibraryID: libraryID,
}, nil
}
func (r *LibraryRepository) GetArtistByName(name string) (model.Artist, error) {
row := r.db.QueryRow("SELECT id, name FROM artists WHERE name = ?", name)
var artist model.Artist
if err := row.Scan(&artist.ID, &artist.Name); err != nil {
return model.Artist{}, err
}
return artist, nil
}
func (r *LibraryRepository) CreateArtist(name string) (model.Artist, error) {
result, err := r.db.Exec("INSERT INTO artists (name) VALUES (?)", name)
if err != nil {
return model.Artist{}, err
}
id, err := result.LastInsertId()
if err != nil {
return model.Artist{}, err
}
return model.Artist{
ID: int(id),
Name: name,
}, nil
}
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) (model.Album, error) {
result, err := r.db.Exec("INSERT INTO albums (title, artist_id) VALUES (?, ?)", title, artistID)
if err != nil {
return model.Album{}, err
}
id, err := result.LastInsertId()
if err != nil {
return model.Album{}, err
}
return model.Album{
ID: int(id),
Title: title,
ArtistID: artistID,
}, nil
}
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 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
}
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 := []model.Artist{}
for rows.Next() {
var artist model.Artist
if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
return nil, err
}
artists = append(artists, artist)
}
return artists, nil
}
func (r *LibraryRepository) GetArtist(id int) (model.Artist, error) {
row := r.db.QueryRow("SELECT id, name FROM artists WHERE id = ?", id)
var artist model.Artist
if err := row.Scan(&artist.ID, &artist.Name); err != nil {
return model.Artist{}, err
}
return artist, nil
}
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 := []model.Album{}
for rows.Next() {
var album model.Album
if err := rows.Scan(&album.ID, &album.Title, &album.ArtistID); err != nil {
return nil, err
}
albums = append(albums, album)
}
return albums, nil
}
func (r *LibraryRepository) GetAlbumsByArtist(artistID int) ([]model.Album, error) {
rows, err := r.db.Query("SELECT id, title, artist_id FROM albums WHERE artist_id = ?", artistID)
if err != nil {
return nil, err
}
defer rows.Close()
albums := []model.Album{}
for rows.Next() {
var album model.Album
if err := rows.Scan(&album.ID, &album.Title, &album.ArtistID); err != nil {
return nil, err
}
albums = append(albums, album)
}
return albums, nil
}
func (r *LibraryRepository) GetAlbumIDsByArtist(artistID int) ([]int, error) {
rows, err := r.db.Query("SELECT id FROM albums WHERE artist_id = ?", artistID)
if err != nil {
return nil, err
}
defer rows.Close()
ids := []int{}
for rows.Next() {
var id int
if err := rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
return ids, nil
}
func (r *LibraryRepository) GetSongIDsByArtist(artistID int) ([]int, error) {
rows, err := r.db.Query("SELECT id FROM songs WHERE artist_id = ?", artistID)
if err != nil {
return nil, err
}
defer rows.Close()
ids := []int{}
for rows.Next() {
var id int
if err := rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
return ids, nil
}
func (r *LibraryRepository) GetAlbum(id int) (model.Album, error) {
row := r.db.QueryRow("SELECT id, title, artist_id FROM albums WHERE id = ?", id)
var album model.Album
if err := row.Scan(&album.ID, &album.Title, &album.ArtistID); err != nil {
return model.Album{}, err
}
return album, nil
}
func (r *LibraryRepository) GetSongIDsByAlbum(artistID int) ([]int, error) {
rows, err := r.db.Query("SELECT id FROM songs WHERE album_id = ?", artistID)
if err != nil {
return nil, err
}
defer rows.Close()
ids := []int{}
for rows.Next() {
var id int
if err := rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
return ids, nil
}