|
|
@@ -106,3 +106,42 @@ func routeFetchSongs(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter,
|
|
|
w.Write(response)
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+func routeFetchSongInfo(l *logger.Logger, rdb *redis.Client, w http.ResponseWriter, r *http.Request) error {
|
|
|
+ idRaw := r.URL.Query().Get("id")
|
|
|
+ idInt, err := strconv.Atoi(idRaw)
|
|
|
+ if err != nil {
|
|
|
+ http.Error(w, "Must provide a valid id", http.StatusBadRequest)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ if idInt < 1 {
|
|
|
+ http.Error(w, "id must be non-negative", http.StatusBadRequest)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ db := database.GetConnection()
|
|
|
+
|
|
|
+ song, err := repository.SelectSong(db, idInt)
|
|
|
+ if err != nil {
|
|
|
+ if err.Error() == "No such ID" {
|
|
|
+ http.Error(w, "Song not found", http.StatusNotFound)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ response, err := json.Marshal(read.SongExternal{
|
|
|
+ Id: idInt,
|
|
|
+ TrackNumber: song.TrackNumber,
|
|
|
+ Title: song.Title,
|
|
|
+ Artist: song.Artist,
|
|
|
+ Album: song.Album,
|
|
|
+ Duration: song.Duration,
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ w.Write(response)
|
|
|
+ return nil
|
|
|
+}
|