This commit is contained in:
@@ -249,6 +249,21 @@ func (c *LibraryController) GetAlbums(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonResponse(w, albums, http.StatusOK)
|
jsonResponse(w, albums, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *LibraryController) GetAlbumsByArtist(w http.ResponseWriter, r *http.Request) {
|
||||||
|
idParam := chi.URLParam(r, "id")
|
||||||
|
id, err := strconv.Atoi(idParam)
|
||||||
|
if err != nil {
|
||||||
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
albums, err := c.service.GetAlbumsByArtistWithDetail(id)
|
||||||
|
if err != nil {
|
||||||
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
jsonResponse(w, albums, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *LibraryController) GetAlbum(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) GetAlbum(w http.ResponseWriter, r *http.Request) {
|
||||||
idParam := chi.URLParam(r, "id")
|
idParam := chi.URLParam(r, "id")
|
||||||
id, err := strconv.Atoi(idParam)
|
id, err := strconv.Atoi(idParam)
|
||||||
@@ -263,3 +278,18 @@ func (c *LibraryController) GetAlbum(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
jsonResponse(w, album, http.StatusOK)
|
jsonResponse(w, album, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *LibraryController) GetAlbumCover(w http.ResponseWriter, r *http.Request) {
|
||||||
|
idParam := chi.URLParam(r, "id")
|
||||||
|
id, err := strconv.Atoi(idParam)
|
||||||
|
if err != nil {
|
||||||
|
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
coverPath, err := c.service.GetAlbumCover(id)
|
||||||
|
if err != nil {
|
||||||
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
http.ServeFile(w, r, coverPath)
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,6 +21,6 @@ type ArtistDetail struct {
|
|||||||
type AlbumDetail struct {
|
type AlbumDetail struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Artist int `json:"artist"`
|
Artist string `json:"artist"`
|
||||||
Songs []int `json:"songs"`
|
Songs []int `json:"songs"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"butterfliu/config"
|
||||||
"butterfliu/internal/model"
|
"butterfliu/internal/model"
|
||||||
"butterfliu/internal/repository"
|
"butterfliu/internal/repository"
|
||||||
"butterfliu/internal/scanner"
|
"butterfliu/internal/scanner"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -75,6 +78,10 @@ func (s *LibraryService) GetSongsByAlbum(id int) ([]repository.SongDetail, error
|
|||||||
return s.repo.GetSongsByAlbumWithDetails(id)
|
return s.repo.GetSongsByAlbumWithDetails(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *LibraryService) GetSongIDsByAlbum(id int) ([]repository.SongDetail, error) {
|
||||||
|
return s.repo.GetSongsByAlbumWithDetails(id)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *LibraryService) GetArtists() ([]model.Artist, error) {
|
func (s *LibraryService) GetArtists() ([]model.Artist, error) {
|
||||||
return s.repo.GetArtists()
|
return s.repo.GetArtists()
|
||||||
}
|
}
|
||||||
@@ -104,6 +111,31 @@ func (s *LibraryService) GetAlbums() ([]model.Album, error) {
|
|||||||
return s.repo.GetAlbums()
|
return s.repo.GetAlbums()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *LibraryService) GetAlbumsByArtistWithDetail(artistID int) ([]model.AlbumDetail, error) {
|
||||||
|
albums, err := s.repo.GetAlbumsByArtist(artistID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
details := []model.AlbumDetail{}
|
||||||
|
for _, a := range albums {
|
||||||
|
songs, err := s.repo.GetSongIDsByAlbum(a.ID)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
artist, err := s.repo.GetArtist(artistID)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
details = append(details, model.AlbumDetail{
|
||||||
|
ID: a.ID,
|
||||||
|
Title: a.Title,
|
||||||
|
Artist: artist.Name,
|
||||||
|
Songs: songs,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return details, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *LibraryService) GetAlbum(id int) (model.AlbumDetail, error) {
|
func (s *LibraryService) GetAlbum(id int) (model.AlbumDetail, error) {
|
||||||
album, err := s.repo.GetAlbum(id)
|
album, err := s.repo.GetAlbum(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -113,14 +145,27 @@ func (s *LibraryService) GetAlbum(id int) (model.AlbumDetail, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return model.AlbumDetail{}, err
|
return model.AlbumDetail{}, err
|
||||||
}
|
}
|
||||||
|
artist, err := s.repo.GetArtist(album.ArtistID)
|
||||||
|
if err != nil {
|
||||||
|
return model.AlbumDetail{}, err
|
||||||
|
}
|
||||||
return model.AlbumDetail{
|
return model.AlbumDetail{
|
||||||
ID: id,
|
ID: id,
|
||||||
Title: album.Title,
|
Title: album.Title,
|
||||||
Songs: songs,
|
Songs: songs,
|
||||||
Artist: album.ArtistID,
|
Artist: artist.Name,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *LibraryService) GetAlbumCover(id int) (string, error) {
|
||||||
|
songs, err := s.repo.GetSongIDsByAlbum(id)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
conf := config.LoadConfig()
|
||||||
|
return path.Join(conf.GetCachePath("cover"), strconv.Itoa(songs[0])+".jpg"), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *LibraryService) Scan(id int) (*ScanReport, error) {
|
func (s *LibraryService) Scan(id int) (*ScanReport, error) {
|
||||||
lib, err := s.repo.GetByID(id)
|
lib, err := s.repo.GetByID(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
3
main.go
3
main.go
@@ -56,12 +56,15 @@ func main() {
|
|||||||
r.Route("/api/artists", func(r chi.Router) {
|
r.Route("/api/artists", func(r chi.Router) {
|
||||||
r.Get("/", libraryController.GetArtists)
|
r.Get("/", libraryController.GetArtists)
|
||||||
r.Get("/{id}", libraryController.GetArtist)
|
r.Get("/{id}", libraryController.GetArtist)
|
||||||
|
r.Get("/{id}", libraryController.GetArtist)
|
||||||
r.Get("/{id}/songs", libraryController.GetSongsByArtist)
|
r.Get("/{id}/songs", libraryController.GetSongsByArtist)
|
||||||
|
r.Get("/{id}/albums", libraryController.GetAlbumsByArtist)
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Route("/api/albums", func(r chi.Router) {
|
r.Route("/api/albums", func(r chi.Router) {
|
||||||
r.Get("/", libraryController.GetAlbums)
|
r.Get("/", libraryController.GetAlbums)
|
||||||
r.Get("/{id}", libraryController.GetAlbum)
|
r.Get("/{id}", libraryController.GetAlbum)
|
||||||
|
r.Get("/{id}/cover", libraryController.GetAlbumCover)
|
||||||
r.Get("/{id}/songs", libraryController.GetSongsByAlbum)
|
r.Get("/{id}/songs", libraryController.GetSongsByAlbum)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user