From 55f8fed42d405aca1123a781747dda00e54daffa Mon Sep 17 00:00:00 2001 From: 2weiEmu Date: Tue, 19 May 2026 17:08:31 +0200 Subject: [PATCH] updated: more api stuff, gonna do room testing soon --- pkg/dbhandling/dbhandling.go | 2 + pkg/dbhandling/roomsapi.go | 112 ++++++++++++++++++++++++++++---- pkg/dbhandling/roomsapi_test.go | 20 ++++++ pkg/dbhandling/songsapi.go | 2 +- 4 files changed, 123 insertions(+), 13 deletions(-) create mode 100644 pkg/dbhandling/roomsapi_test.go diff --git a/pkg/dbhandling/dbhandling.go b/pkg/dbhandling/dbhandling.go index 334ecbe..f45c69e 100644 --- a/pkg/dbhandling/dbhandling.go +++ b/pkg/dbhandling/dbhandling.go @@ -10,6 +10,8 @@ import ( "github.com/redis/go-redis/v9" ) + + type DBSong struct { Key string ArtistName string diff --git a/pkg/dbhandling/roomsapi.go b/pkg/dbhandling/roomsapi.go index c651803..ca8280c 100644 --- a/pkg/dbhandling/roomsapi.go +++ b/pkg/dbhandling/roomsapi.go @@ -3,6 +3,8 @@ package dbhandling import ( "encoding/json" "net/http" + + "github.com/redis/go-redis/v9" ) /** TEST @@ -43,39 +45,125 @@ func (dbw *DBWrapper) RoomsGet(w http.ResponseWriter, r *http.Request) { /** TEST * Get the id's of the songs present in a room using the name of the room * the sent JSON should just be a string with the name of the room +* what will be returned is just a list of ids in the body, in the form [1, 2, 3, 4, 5] (without song:) **/ func (dbw *DBWrapper) RoomsGetByName(w http.ResponseWriter, r *http.Request) { // the body should just be a string that is the name of the room -} + nameBytes := make([]byte, 256) + _, err := r.Body.Read(nameBytes) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } -/** TODO TEST - * Create a new room with the given name - * Just a string value as the name - * May be rejected if a room with that name already exists - **/ -func (dbw *DBWrapper) RoomsCreate(w http.ResponseWriter, r *http.Request) { + name := string(nameBytes) + + idList, err := dbw.RedisDb.ZRange(dbw.Ctx, name, 0, -1).Result() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + b, err := json.Marshal(idList) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + w.Write(b) + w.WriteHeader(http.StatusOK) } -/** TODO TEST + +/** TEST * Add songs to a room using the ID of the song and the NAME of the room * The values should be given in the following way: * { - * roomName: "NAME OF ROOM", - * songIds: [1, 2, 3, 4, 5], + * name: "NAME OF ROOM", + * assignedSongKeys: [1, 2, 3, 4, 5], * } + * If a room with that name does not exist, it will be created instead. **/ func (dbw *DBWrapper) RoomsAddByNameAndId(w http.ResponseWriter, r *http.Request) { + + room := DBRoom{} + err := json.NewDecoder(r.Body).Decode(&room) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + + c, err := dbw.RedisDb.ZCount(dbw.Ctx, room.Name, "-inf", "+inf").Result() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + // TODO: we can probably make all of this execute as like one command ideally? + // so that there is less I/O? + for i, k := range room.AssignedSongKeys { + _, err = dbw.RedisDb.ZAdd(dbw.Ctx, room.Name, redis.Z{ + Score: float64(c + int64(i)), + Member: k, + }).Result() + + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + } + + w.WriteHeader(http.StatusOK) + } -/** TODO TEST +/** TEST * The same as adding, except that the IDs mentioned in the list will be removed from the room * (if they are present) **/ func (dbw *DBWrapper) RoomsRemoveByNameAndId(w http.ResponseWriter, r *http.Request) { + + room := DBRoom{} + err := json.NewDecoder(r.Body).Decode(&room) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + // TODO: again, is there some way roll this into one command? + for _, k := range room.AssignedSongKeys { + _, err = dbw.RedisDb.ZRem(dbw.Ctx, room.Name, k).Result() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + } + + w.WriteHeader(http.StatusOK) } -/** TODO TEST +/** TEST * Delete a room by giving the name as a string, this just means the room won't exist anymore **/ func (dbw *DBWrapper) RoomsDeleteByName(w http.ResponseWriter, r *http.Request) { + + + // the body should just be a string that is the name of the room + nameBytes := make([]byte, 256) + _, err := r.Body.Read(nameBytes) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + name := string(nameBytes) + + _, err = dbw.RedisDb.Del(dbw.Ctx, name).Result() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) } diff --git a/pkg/dbhandling/roomsapi_test.go b/pkg/dbhandling/roomsapi_test.go new file mode 100644 index 0000000..c92cf69 --- /dev/null +++ b/pkg/dbhandling/roomsapi_test.go @@ -0,0 +1,20 @@ +package dbhandling_test + +import ( + "net/http/httptest" + "songmanager/pkg/dbhandling" + "testing" +) + + +func TestRoomsGet(t *testing.T) { + + r := httptest.NewRequest("GET", "/api/rooms/get", &EmptyReader{}) + wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") + + // subtests + t.Run("RoomsGet1", func(t *testing.T) { + recorder := httptest.NewRecorder() + wrapper.RoomsGet(recorder, r) + }) +} diff --git a/pkg/dbhandling/songsapi.go b/pkg/dbhandling/songsapi.go index 4817c87..60e2ee0 100644 --- a/pkg/dbhandling/songsapi.go +++ b/pkg/dbhandling/songsapi.go @@ -6,7 +6,7 @@ import ( "strconv" ) -var START_COUNT = 400 // WARNING: jank +var START_COUNT = 2000 // WARNING: jank /** TEST * An internal method to get the total number of songs as that is just something we need sometimes -- 2.54.0