48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/fujin/music-agregator/internal/indexer"
|
|
)
|
|
|
|
func (h *Handlers) ListIndexers(w http.ResponseWriter, r *http.Request) {
|
|
indexers := h.IndexerService.GetIndexers(r.Context())
|
|
writeJSON(w, http.StatusOK, indexers)
|
|
}
|
|
|
|
func (h *Handlers) SearchIndexers(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Artist string `json:"artist"`
|
|
Album *string `json:"album,omitempty"`
|
|
Year *uint32 `json:"year,omitempty"`
|
|
Limit int `json:"limit,omitempty"`
|
|
Offset int `json:"offset,omitempty"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
|
|
if req.Limit == 0 {
|
|
req.Limit = 20
|
|
}
|
|
|
|
criteria := &indexer.MusicSearchCriteria{
|
|
Artist: req.Artist,
|
|
Album: req.Album,
|
|
Year: req.Year,
|
|
Limit: req.Limit,
|
|
Offset: req.Offset,
|
|
}
|
|
|
|
results, err := h.IndexerService.Search(r.Context(), criteria, nil)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, results)
|
|
}
|