]> git.example.dev Git - binbsis50-sm.git/commitdiff
update: added update by ID method
author2weiEmu <saalbach.robert@outlook.de>
Fri, 1 May 2026 11:42:34 +0000 (13:42 +0200)
committer2weiEmu <saalbach.robert@outlook.de>
Fri, 1 May 2026 11:42:34 +0000 (13:42 +0200)
pkg/dbhandling/songsapi.go

index 552a06678690100f64b20a4f56d5b28bf5660c56..d1b2aa510c75f6862e8cd82e74aafb34790bd4aa 100644 (file)
@@ -17,6 +17,25 @@ func (dbw *DBWrapper) getSongCount() (int, error) {
        return len(keys), nil
 }
 
+func (dbw *DBWrapper) getSongByKey(key string) (DBSong, error) {
+       interf, err := dbw.RedisDb.HMGet(dbw.Ctx, key,  "artistName", "trackName", "trackViewUrl", "previewUrl", "artworkUrl60", "artworkUrl100", "displayArtistNames", "displayTrackName").Result()
+       if err != nil {
+               return DBSong{}, err
+       }
+
+       return DBSong{
+               Key: key,
+               ArtistName: interf[0].(string),
+               TrackName: interf[1].(string),
+               TrackViewURL: interf[2].(string),
+               PreviewURL: interf[3].(string),
+               ArtworkURL60: interf[4].(string),
+               ArtworkURL100: interf[5].(string),
+               DisplayArtistNames: interf[6].(string),
+               DisplayTrackName: interf[7].(string),
+       }, nil
+}
+
 /**
  * Get all songs, and all their assosciated information from the db
  **/
@@ -94,14 +113,67 @@ func (dbw *DBWrapper) SongsCreate(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
 }
 
-/** TODO
- * Create a new song in the database (only in the main list don't add to anything)
+/** TEST
+ * Update the information of a song (with non-empty values in request)
+ * note htat for now, i still make you set the value, but just to an empty string
+ * TODO:
  **/
 func (dbw *DBWrapper) SongsUpdateById(w http.ResponseWriter, r *http.Request) {
+       // the ID should be as 'song:XX' in the KEY section of DB song
+       // then all the non-zero / non-empty fields will be updated on that song
+       newSong := DBSong{}
+       err := json.NewDecoder(r.Body).Decode(&newSong)
+       if err != nil {
+               w.WriteHeader(http.StatusInternalServerError)
+               return
+       }
+
+       oldSong, err := dbw.getSongByKey(newSong.Key)
+       if err != nil {
+               w.WriteHeader(http.StatusInternalServerError)
+               return
+       }
+
+       // can't not send the fields, will have to just set the others to empty strings
+       // or at least I don't entirely understand how it would behave yet
+       if newSong.ArtistName == "" { newSong.ArtistName = oldSong.ArtistName }
+       if newSong.TrackName == "" { newSong.TrackName = oldSong.TrackName }
+       if newSong.TrackViewURL == "" { newSong.TrackViewURL = oldSong.TrackViewURL }
+       if newSong.PreviewURL == "" { newSong.PreviewURL = oldSong.PreviewURL }
+       if newSong.ArtworkURL60 == "" { newSong.ArtworkURL60 = oldSong.ArtworkURL60 }
+       if newSong.ArtworkURL100 == "" { newSong.ArtworkURL100 = oldSong.ArtworkURL100 }
+       if newSong.DisplayArtistNames == "" { newSong.DisplayArtistNames = oldSong.DisplayArtistNames }
+       if newSong.DisplayTrackName == "" { newSong.DisplayTrackName = oldSong.DisplayTrackName }
+
+       cmd := dbw.RedisDb.HSet(dbw.Ctx, newSong.Key, 
+               "artistName", newSong.ArtistName,
+               "trackName", newSong.TrackName,
+               "trackViewUrl", newSong.TrackViewURL,
+               "previewUrl", newSong.PreviewURL,
+               "artworkUrl60", newSong.ArtworkURL60,
+               "artworkUrl100", newSong.ArtworkURL100,
+               "displayArtistNames", newSong.DisplayArtistNames,
+               "displayTrackName", newSong.DisplayTrackName,
+               )
+
+       _, err = cmd.Result()
+       if err != nil {
+               w.WriteHeader(http.StatusInternalServerError)
+               return
+       }
+
+       w.WriteHeader(http.StatusOK)
 }
 
-/** TODO
- * Create a new song in the database (only in the main list don't add to anything)
+/** TODO TEST
+ * 
+ **/
+func (dbw *DBWrapper) SongsGetById(w http.ResponseWriter, r *http.Request) {
+
+}
+
+/** TODO TEST
+ * 
  **/
 func (dbw *DBWrapper) SongsDeleteById(w http.ResponseWriter, r *http.Request) {
 }