fetch.go 6.1 KB

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