fetch.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package server
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/felamaslen/go-music-player/pkg/database"
  7. "github.com/felamaslen/go-music-player/pkg/logger"
  8. "github.com/felamaslen/go-music-player/pkg/read"
  9. "github.com/felamaslen/go-music-player/pkg/repository"
  10. "github.com/felamaslen/go-music-player/pkg/services"
  11. "github.com/go-redis/redis/v7"
  12. )
  13. type ArtistsResponse struct {
  14. Artists []string `json:"artists"`
  15. More bool `json:"more"`
  16. }
  17. func routeFetchArtists(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
  18. limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
  19. if err != nil {
  20. http.Error(w, "Limit must be an integer", http.StatusBadRequest)
  21. return nil
  22. }
  23. if limit < 1 || limit > 1000 {
  24. http.Error(w, "Limit must be between 1 and 1000", http.StatusBadRequest)
  25. return nil
  26. }
  27. page, err := strconv.Atoi(r.URL.Query().Get("page"))
  28. if err != nil {
  29. http.Error(w, "Page must be an integer", http.StatusBadRequest)
  30. return nil
  31. }
  32. if page < 0 {
  33. http.Error(w, "Page must be non-negative", http.StatusBadRequest)
  34. return nil
  35. }
  36. artists, more := services.GetArtists(limit, page)
  37. response, err := json.Marshal(ArtistsResponse{
  38. Artists: *artists,
  39. More: more,
  40. })
  41. if err != nil {
  42. return err
  43. }
  44. w.Write(response)
  45. return nil
  46. }
  47. type AlbumsResponse struct {
  48. Artist string `json:"artist"`
  49. Albums []string `json:"albums"`
  50. }
  51. func routeFetchAlbums(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
  52. artist := r.URL.Query().Get("artist")
  53. db := database.GetConnection()
  54. albums, err := repository.SelectAlbumsByArtist(db, artist)
  55. if err != nil {
  56. return err
  57. }
  58. response, err := json.Marshal(AlbumsResponse{
  59. Artist: artist,
  60. Albums: *albums,
  61. })
  62. if err != nil {
  63. return err
  64. }
  65. w.Write(response)
  66. return nil
  67. }
  68. type SongsResponse struct {
  69. Artist string `json:"artist"`
  70. Songs *[]*read.SongExternal `json:"songs"`
  71. }
  72. func routeFetchSongs(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
  73. artist := r.URL.Query().Get("artist")
  74. db := database.GetConnection()
  75. songs, err := repository.SelectSongsByArtist(db, artist)
  76. if err != nil {
  77. return err
  78. }
  79. response, err := json.Marshal(SongsResponse{
  80. Artist: artist,
  81. Songs: songs,
  82. })
  83. if err != nil {
  84. return err
  85. }
  86. w.Write(response)
  87. return nil
  88. }