| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package server
- import (
- "encoding/json"
- "net/http"
- "strconv"
- "github.com/felamaslen/go-music-player/pkg/logger"
- "github.com/felamaslen/go-music-player/pkg/services"
- "github.com/go-redis/redis/v7"
- )
- type ArtistsResponse struct {
- Artists []string `json:"artists"`
- More bool `json:"more"`
- }
- func routeFetchArtists(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
- limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
- if err != nil {
- http.Error(w, "Limit must be an integer", http.StatusBadRequest)
- return nil
- }
- if limit < 1 || limit > 1000 {
- http.Error(w, "Limit must be between 1 and 1000", http.StatusBadRequest)
- return nil
- }
- page, err := strconv.Atoi(r.URL.Query().Get("page"))
- if err != nil {
- http.Error(w, "Page must be an integer", http.StatusBadRequest)
- return nil
- }
- if page < 0 {
- http.Error(w, "Page must be non-negative", http.StatusBadRequest)
- return nil
- }
- artists, more := services.GetArtists(limit, page)
- response, err := json.Marshal(ArtistsResponse{
- Artists: *artists,
- More: more,
- })
- if err != nil {
- return err
- }
- w.Write(response)
- return nil
- }
|