]> git.example.dev Git - binbsis50-sm.git/commitdiff
updated: more api stuff, gonna do room testing soon
author2weiEmu <saalbach.robert@outlook.de>
Tue, 19 May 2026 15:08:31 +0000 (17:08 +0200)
committer2weiEmu <saalbach.robert@outlook.de>
Tue, 19 May 2026 15:08:31 +0000 (17:08 +0200)
pkg/dbhandling/dbhandling.go
pkg/dbhandling/roomsapi.go
pkg/dbhandling/roomsapi_test.go [new file with mode: 0644]
pkg/dbhandling/songsapi.go

index 334ecbea017bb16f725b359480571d4a6903d8d9..f45c69e68da6e61516c228d97afe28935e39c1ac 100644 (file)
@@ -10,6 +10,8 @@ import (
        "github.com/redis/go-redis/v9"
 )
 
+
+
 type DBSong struct {
        Key string
        ArtistName string
index c651803c5c48d88475aa6a1b8ea303e55990e39d..ca8280c6088fe8567e1c02a2716e2065359d4403 100644 (file)
@@ -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 (file)
index 0000000..c92cf69
--- /dev/null
@@ -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)
+       })
+}
index 4817c87f87f0fc3a2e034e2c5982b34a0c1c415f..60e2ee0a220c1314ec3a33a38c677a8ba53aedfb 100644 (file)
@@ -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