fetch.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. }
  89. func routeFetchSongInfo(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
  90. idRaw := r.URL.Query().Get("id")
  91. idInt, err := strconv.Atoi(idRaw)
  92. if err != nil {
  93. http.Error(w, "Must provide a valid id", http.StatusBadRequest)
  94. return nil
  95. }
  96. if idInt < 1 {
  97. http.Error(w, "id must be non-negative", http.StatusBadRequest)
  98. return nil
  99. }
  100. db := database.GetConnection()
  101. song, err := repository.SelectSong(db, idInt)
  102. if err != nil {
  103. if err.Error() == "No such ID" {
  104. http.Error(w, "Song not found", http.StatusNotFound)
  105. return nil
  106. }
  107. return err
  108. }
  109. response, err := json.Marshal(read.SongExternal{
  110. Id: idInt,
  111. TrackNumber: song.TrackNumber,
  112. Title: song.Title,
  113. Artist: song.Artist,
  114. Album: song.Album,
  115. Duration: song.Duration,
  116. })
  117. if err != nil {
  118. return err
  119. }
  120. w.Write(response)
  121. return nil
  122. }