fetch.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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/repository"
  9. "github.com/felamaslen/gmus-backend/pkg/types"
  10. "github.com/go-redis/redis"
  11. "github.com/jmoiron/sqlx"
  12. )
  13. type ArtistsResponse struct {
  14. Artists []string `json:"artists"`
  15. }
  16. func GetPagedArtists(limit int, page int) (artists *[]string, more bool) {
  17. db := database.GetConnection()
  18. artists, err := repository.SelectPagedArtists(db, limit, limit*page)
  19. if err != nil {
  20. panic(err)
  21. }
  22. total, err := repository.SelectArtistCount(db)
  23. if err != nil {
  24. panic(err)
  25. }
  26. more = limit*(1+page) < total
  27. return
  28. }
  29. func routeFetchArtists(l *logger.Logger, rdb redis.Cmdable, w http.ResponseWriter, r *http.Request) error {
  30. db := database.GetConnection()
  31. // This returns all artists for now.
  32. // TODO: add a query option which uses the above GetPagedArtists function to enable paging
  33. artists, err := repository.SelectAllArtists(db)
  34. if err != nil {
  35. return err
  36. }
  37. response, err := json.Marshal(ArtistsResponse{
  38. Artists: *artists,
  39. })
  40. if err != nil {
  41. return err
  42. }
  43. w.Write(response)
  44. return nil
  45. }
  46. type AlbumsResponse struct {
  47. Artist string `json:"artist"`
  48. Albums []string `json:"albums"`
  49. }
  50. func routeFetchAlbums(l *logger.Logger, rdb redis.Cmdable, w http.ResponseWriter, r *http.Request) error {
  51. artist := r.URL.Query().Get("artist")
  52. db := database.GetConnection()
  53. albums, err := repository.SelectAlbumsByArtist(db, artist)
  54. if err != nil {
  55. return err
  56. }
  57. response, err := json.Marshal(AlbumsResponse{
  58. Artist: artist,
  59. Albums: *albums,
  60. })
  61. if err != nil {
  62. return err
  63. }
  64. w.Write(response)
  65. return nil
  66. }
  67. type SongsResponse struct {
  68. Artist string `json:"artist"`
  69. Songs *[]*types.SongExternal `json:"songs"`
  70. }
  71. func routeFetchSongs(l *logger.Logger, rdb redis.Cmdable, w http.ResponseWriter, r *http.Request) error {
  72. artist := r.URL.Query().Get("artist")
  73. db := database.GetConnection()
  74. songs, err := repository.SelectSongsByArtist(db, artist)
  75. if err != nil {
  76. return err
  77. }
  78. response, err := json.Marshal(SongsResponse{
  79. Artist: artist,
  80. Songs: songs,
  81. })
  82. if err != nil {
  83. return err
  84. }
  85. w.Write(response)
  86. return nil
  87. }
  88. func validateSongId(w http.ResponseWriter, r *http.Request) (id int, err error) {
  89. idRaw := r.URL.Query().Get("id")
  90. id, err = strconv.Atoi(idRaw)
  91. if err != nil {
  92. http.Error(w, "Must provide a valid id", http.StatusBadRequest)
  93. } else if id < 1 {
  94. http.Error(w, "id must be non-negative", http.StatusBadRequest)
  95. }
  96. return
  97. }
  98. func routeFetchSongInfo(l *logger.Logger, rdb redis.Cmdable, w http.ResponseWriter, r *http.Request) error {
  99. id, err := validateSongId(w, r)
  100. if err != nil {
  101. return nil
  102. }
  103. db := database.GetConnection()
  104. songs, err := repository.SelectSong(db, []int{id})
  105. if err != nil {
  106. return err
  107. }
  108. if len(*songs) == 0 {
  109. http.Error(w, "Song not found", http.StatusNotFound)
  110. return nil
  111. }
  112. song := (*songs)[0]
  113. response, err := json.Marshal(types.SongExternal{
  114. Id: id,
  115. TrackNumber: song.TrackNumber,
  116. Title: song.Title,
  117. Artist: song.Artist,
  118. Album: song.Album,
  119. Duration: song.Duration,
  120. })
  121. if err != nil {
  122. return err
  123. }
  124. w.Write(response)
  125. return nil
  126. }
  127. func routeFetchMultiSongInfo(l *logger.Logger, rdb redis.Cmdable, w http.ResponseWriter, r *http.Request) error {
  128. idsArray := r.URL.Query()["ids"]
  129. if len(idsArray) == 0 {
  130. http.Error(w, "Must provide valid list of IDs", http.StatusBadRequest)
  131. return nil
  132. }
  133. var ids []int
  134. for _, id := range idsArray {
  135. idInt, err := strconv.Atoi(id)
  136. if err != nil {
  137. http.Error(w, "All IDs must be numeric", http.StatusBadRequest)
  138. return nil
  139. }
  140. if idInt < 1 {
  141. http.Error(w, "All IDs must be positive integers", http.StatusBadRequest)
  142. return nil
  143. }
  144. ids = append(ids, idInt)
  145. }
  146. songs, err := repository.SelectSong(database.GetConnection(), ids)
  147. if err != nil {
  148. return err
  149. }
  150. songsArray := []types.SongExternal{}
  151. for _, song := range *songs {
  152. songsArray = append(songsArray, types.SongExternal{
  153. Id: song.Id,
  154. TrackNumber: song.TrackNumber,
  155. Title: song.Title,
  156. Artist: song.Artist,
  157. Album: song.Album,
  158. Duration: song.Duration,
  159. })
  160. }
  161. response, err := json.Marshal(songsArray)
  162. if err != nil {
  163. return err
  164. }
  165. w.Write(response)
  166. return nil
  167. }
  168. type NullResponse struct {
  169. Id int `json:"id"`
  170. }
  171. func respondWithSongOrNull(db *sqlx.DB, w http.ResponseWriter, song *types.Song) error {
  172. if song.Id == 0 {
  173. response, _ := json.Marshal(NullResponse{})
  174. w.Write(response)
  175. return nil
  176. }
  177. response, err := json.Marshal(types.SongExternal{
  178. Id: song.Id,
  179. TrackNumber: song.TrackNumber,
  180. Title: song.Title,
  181. Artist: song.Artist,
  182. Album: song.Album,
  183. Duration: song.Duration,
  184. })
  185. if err != nil {
  186. return err
  187. }
  188. w.Write(response)
  189. return nil
  190. }
  191. func routeFetchNextSong(l *logger.Logger, rdb redis.Cmdable, w http.ResponseWriter, r *http.Request) error {
  192. id, err := validateSongId(w, r)
  193. if err != nil {
  194. return nil
  195. }
  196. db := database.GetConnection()
  197. nextSong, err := repository.GetNextSong(db, id)
  198. if err != nil {
  199. return err
  200. }
  201. if err := respondWithSongOrNull(db, w, nextSong); err != nil {
  202. return err
  203. }
  204. return nil
  205. }
  206. func routeFetchPrevSong(l *logger.Logger, rdb redis.Cmdable, w http.ResponseWriter, r *http.Request) error {
  207. id, err := validateSongId(w, r)
  208. if err != nil {
  209. return nil
  210. }
  211. db := database.GetConnection()
  212. prevSong, err := repository.GetPrevSong(db, id)
  213. if err != nil {
  214. return err
  215. }
  216. if err := respondWithSongOrNull(db, w, prevSong); err != nil {
  217. return err
  218. }
  219. return nil
  220. }