60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
)
|
|
|
|
func (h *Handlers) ListLibraryArtists(w http.ResponseWriter, r *http.Request) {
|
|
if h.DB == nil {
|
|
writeError(w, http.StatusServiceUnavailable, "database not connected")
|
|
return
|
|
}
|
|
|
|
artists, err := h.DB.ListArtists(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, artists)
|
|
}
|
|
|
|
func (h *Handlers) ListLibraryAlbums(w http.ResponseWriter, r *http.Request) {
|
|
if h.DB == nil {
|
|
writeError(w, http.StatusServiceUnavailable, "database not connected")
|
|
return
|
|
}
|
|
|
|
albums, err := h.DB.ListAllAlbums(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, albums)
|
|
}
|
|
|
|
func (h *Handlers) LibraryStats(w http.ResponseWriter, r *http.Request) {
|
|
if h.DB == nil {
|
|
writeError(w, http.StatusServiceUnavailable, "database not connected")
|
|
return
|
|
}
|
|
|
|
artistCount, err := h.DB.CountArtists(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
albumCount, err := h.DB.CountAlbums(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]int64{
|
|
"artists": artistCount,
|
|
"albums": albumCount,
|
|
})
|
|
}
|