fetch.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package server
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/felamaslen/gmus-backend/pkg/database"
  7. "github.com/felamaslen/gmus-backend/pkg/logger"
  8. "github.com/felamaslen/gmus-backend/pkg/read"
  9. "github.com/felamaslen/gmus-backend/pkg/repository"
  10. "github.com/go-redis/redis/v7"
  11. )
  12. type ArtistsResponse struct {
  13. Artists []string `json:"artists"`
  14. }
  15. func routeFetchArtists(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
  16. db := database.GetConnection()
  17. artists, err := repository.SelectAllArtists(db)
  18. if err != nil {
  19. return err
  20. }
  21. response, err := json.Marshal(ArtistsResponse{
  22. Artists: *artists,
  23. })
  24. if err != nil {
  25. return err
  26. }
  27. w.Write(response)
  28. return nil
  29. }
  30. type AlbumsResponse struct {
  31. Artist string `json:"artist"`
  32. Albums []string `json:"albums"`
  33. }
  34. func routeFetchAlbums(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
  35. artist := r.URL.Query().Get("artist")
  36. db := database.GetConnection()
  37. albums, err := repository.SelectAlbumsByArtist(db, artist)
  38. if err != nil {
  39. return err
  40. }
  41. response, err := json.Marshal(AlbumsResponse{
  42. Artist: artist,
  43. Albums: *albums,
  44. })
  45. if err != nil {
  46. return err
  47. }
  48. w.Write(response)
  49. return nil
  50. }
  51. type SongsResponse struct {
  52. Artist string `json:"artist"`
  53. Songs *[]*read.SongExternal `json:"songs"`
  54. }
  55. func routeFetchSongs(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
  56. artist := r.URL.Query().Get("artist")
  57. db := database.GetConnection()
  58. songs, err := repository.SelectSongsByArtist(db, artist)
  59. if err != nil {
  60. return err
  61. }
  62. response, err := json.Marshal(SongsResponse{
  63. Artist: artist,
  64. Songs: songs,
  65. })
  66. if err != nil {
  67. return err
  68. }
  69. w.Write(response)
  70. return nil
  71. }
  72. func routeFetchSongInfo(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
  73. idRaw := r.URL.Query().Get("id")
  74. idInt, err := strconv.Atoi(idRaw)
  75. if err != nil {
  76. http.Error(w, "Must provide a valid id", http.StatusBadRequest)
  77. return nil
  78. }
  79. if idInt < 1 {
  80. http.Error(w, "id must be non-negative", http.StatusBadRequest)
  81. return nil
  82. }
  83. db := database.GetConnection()
  84. song, err := repository.SelectSong(db, idInt)
  85. if err != nil {
  86. if err.Error() == "No such ID" {
  87. http.Error(w, "Song not found", http.StatusNotFound)
  88. return nil
  89. }
  90. return err
  91. }
  92. response, err := json.Marshal(read.SongExternal{
  93. Id: idInt,
  94. TrackNumber: song.TrackNumber,
  95. Title: song.Title,
  96. Artist: song.Artist,
  97. Album: song.Album,
  98. Duration: song.Duration,
  99. })
  100. if err != nil {
  101. return err
  102. }
  103. w.Write(response)
  104. return nil
  105. }