This commit is contained in:
@@ -195,6 +195,21 @@ func (c *LibraryController) GetArtists(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonResponse(w, artists, http.StatusOK)
|
jsonResponse(w, artists, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *LibraryController) GetArtist(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
|
||||||
|
}
|
||||||
|
artist, err := c.service.GetArtist(id)
|
||||||
|
if err != nil {
|
||||||
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
jsonResponse(w, artist, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *LibraryController) GetAlbums(w http.ResponseWriter, r *http.Request) {
|
func (c *LibraryController) GetAlbums(w http.ResponseWriter, r *http.Request) {
|
||||||
albums, err := c.service.GetAlbums()
|
albums, err := c.service.GetAlbums()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -203,3 +218,18 @@ func (c *LibraryController) GetAlbums(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
jsonResponse(w, albums, http.StatusOK)
|
jsonResponse(w, albums, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *LibraryController) GetAlbum(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
|
||||||
|
}
|
||||||
|
album, err := c.service.GetAlbum(id)
|
||||||
|
if err != nil {
|
||||||
|
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
jsonResponse(w, album, http.StatusOK)
|
||||||
|
}
|
||||||
|
|||||||
@@ -275,6 +275,17 @@ func (r *LibraryRepository) GetArtists() ([]model.Artist, error) {
|
|||||||
return artists, nil
|
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) {
|
func (r *LibraryRepository) GetAlbums() ([]model.Album, error) {
|
||||||
rows, err := r.db.Query("SELECT id, title, artist_id FROM albums")
|
rows, err := r.db.Query("SELECT id, title, artist_id FROM albums")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -292,3 +303,13 @@ func (r *LibraryRepository) GetAlbums() ([]model.Album, error) {
|
|||||||
}
|
}
|
||||||
return albums, nil
|
return albums, 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
|
||||||
|
}
|
||||||
|
|||||||
@@ -71,10 +71,18 @@ func (s *LibraryService) GetArtists() ([]model.Artist, error) {
|
|||||||
return s.repo.GetArtists()
|
return s.repo.GetArtists()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *LibraryService) GetArtist(id int) (model.Artist, error) {
|
||||||
|
return s.repo.GetArtist(id)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *LibraryService) GetAlbums() ([]model.Album, error) {
|
func (s *LibraryService) GetAlbums() ([]model.Album, error) {
|
||||||
return s.repo.GetAlbums()
|
return s.repo.GetAlbums()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *LibraryService) GetAlbum(id int) (model.Album, error) {
|
||||||
|
return s.repo.GetAlbum(id)
|
||||||
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
|||||||
12
main.go
12
main.go
@@ -1,10 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"butterfliu/config"
|
||||||
"butterfliu/internal/controller"
|
"butterfliu/internal/controller"
|
||||||
"butterfliu/internal/repository"
|
"butterfliu/internal/repository"
|
||||||
"butterfliu/internal/service"
|
"butterfliu/internal/service"
|
||||||
"butterfliu/config"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -53,9 +53,13 @@ func main() {
|
|||||||
r.Get("/{id}/cover", songController.Cover)
|
r.Get("/{id}/cover", songController.Cover)
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Route("/api", func(r chi.Router) {
|
r.Route("/api/artists", func(r chi.Router) {
|
||||||
r.Get("/artists", libraryController.GetArtists)
|
r.Get("/", libraryController.GetArtists)
|
||||||
r.Get("/albums", libraryController.GetAlbums)
|
r.Get("/{id}", libraryController.GetArtist)
|
||||||
|
})
|
||||||
|
r.Route("/api/albums", func(r chi.Router) {
|
||||||
|
r.Get("/", libraryController.GetAlbums)
|
||||||
|
r.Get("/{id}", libraryController.GetAlbum)
|
||||||
})
|
})
|
||||||
|
|
||||||
http.ListenAndServe(":8102", r)
|
http.ListenAndServe(":8102", r)
|
||||||
|
|||||||
Reference in New Issue
Block a user