From b39fa45d68cae86e48c88a15fb947b97d96dd4f9 Mon Sep 17 00:00:00 2001 From: 2weiEmu Date: Fri, 1 May 2026 13:42:34 +0200 Subject: [PATCH] update: added update by ID method --- pkg/dbhandling/songsapi.go | 80 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/pkg/dbhandling/songsapi.go b/pkg/dbhandling/songsapi.go index 552a066..d1b2aa5 100644 --- a/pkg/dbhandling/songsapi.go +++ b/pkg/dbhandling/songsapi.go @@ -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) { } -- 2.54.0