]> git.example.dev Git - binbsis50-sm.git/commitdiff
updated: fixed the way some methods get parameters, as I forgot how I wanted to do...
author2weiEmu <saalbach.robert@outlook.de>
Wed, 20 May 2026 13:38:34 +0000 (15:38 +0200)
committer2weiEmu <saalbach.robert@outlook.de>
Wed, 20 May 2026 13:38:34 +0000 (15:38 +0200)
main.go
pkg/dbhandling/roomsapi.go
pkg/dbhandling/roomsapi_test.go
testing/dummy_testing_data.py

diff --git a/main.go b/main.go
index 6f5b5029c622ea105fa20148a43cecdb56d4b7c8..7f931b7d2b6eb480720917b7764f0eff0dacf5a3 100644 (file)
--- a/main.go
+++ b/main.go
@@ -58,9 +58,9 @@ func main() {
        mux.HandleFunc("DELETE /api/songs/delete/{id}", DBwrapper.SongsDeleteById)
 
        // api/rooms - api related to managaing rooms
-       mux.HandleFunc("GET /api/rooms/get", DBwrapper.RoomsGet)
-       mux.HandleFunc("POST /api/rooms/create/", DBwrapper.RoomsCreate)
-       mux.HandleFunc("PUT /api/rooms/add/{name}/{id}", DBwrapper.RoomsAddByNameAndId)
+       mux.HandleFunc("GET /api/rooms/get/{name}", DBwrapper.RoomsGetByName)
+       mux.HandleFunc("GET /api/rooms/get/all", DBwrapper.RoomsGet)
+       mux.HandleFunc("PUT /api/rooms/add/{name}", DBwrapper.RoomsAddByNameAndId)
        mux.HandleFunc("PUT /api/rooms/remove/{name}/{id}", DBwrapper.RoomsRemoveByNameAndId)
        mux.HandleFunc("DELETE /api/rooms/delete/{name}", DBwrapper.RoomsDeleteByName)
 
index ca8280c6088fe8567e1c02a2716e2065359d4403..63769bb7065ec5e22a38d4ac7c62f7873f3bfc07 100644 (file)
@@ -2,12 +2,13 @@ package dbhandling
 
 import (
        "encoding/json"
+       "fmt"
        "net/http"
 
        "github.com/redis/go-redis/v9"
 )
 
-/** TEST
+/**
  * Get all the names of all the rooms in the database
  **/
 func (dbw *DBWrapper) RoomsGet(w http.ResponseWriter, r *http.Request) {
@@ -42,21 +43,12 @@ func (dbw *DBWrapper) RoomsGet(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
 }
 
-/** 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:)
+ * 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
-       }
-
-       name := string(nameBytes)
+       name := r.PathValue("name")
 
        idList, err := dbw.RedisDb.ZRange(dbw.Ctx, name, 0, -1).Result()
        if err != nil {
@@ -75,26 +67,25 @@ func (dbw *DBWrapper) RoomsGetByName(w http.ResponseWriter, r *http.Request) {
 }
 
 
-/** 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:
- * {
- *     name: "NAME OF ROOM",
- *        assignedSongKeys: [1, 2, 3, 4, 5],
- * }
+ * The values given should just be a list of ids to be added to the room
  * If a room with that name does not exist, it will be created instead.
  **/
 func (dbw *DBWrapper) RoomsAddByNameAndId(w http.ResponseWriter, r *http.Request) {
+       name := r.PathValue("name")
+       var assignedSongKeys []int
 
-       room := DBRoom{}
-       err := json.NewDecoder(r.Body).Decode(&room)
+       err := json.NewDecoder(r.Body).Decode(&assignedSongKeys)
        if err != nil {
                w.WriteHeader(http.StatusInternalServerError)
+               fmt.Println("What went wrong,", err)
                return
        }
 
+       fmt.Println("Gotten keys:", assignedSongKeys) // TODO: replace with logging
 
-       c, err := dbw.RedisDb.ZCount(dbw.Ctx, room.Name, "-inf", "+inf").Result()
+       c, err := dbw.RedisDb.ZCount(dbw.Ctx, name, "-inf", "+inf").Result()
        if err != nil {
                w.WriteHeader(http.StatusInternalServerError)
                return
@@ -102,8 +93,8 @@ func (dbw *DBWrapper) RoomsAddByNameAndId(w http.ResponseWriter, r *http.Request
 
        // 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{
+       for i, k := range assignedSongKeys {
+               _, err = dbw.RedisDb.ZAdd(dbw.Ctx, name, redis.Z{
                        Score: float64(c + int64(i)),
                        Member: k,
                }).Result()
@@ -113,9 +104,7 @@ func (dbw *DBWrapper) RoomsAddByNameAndId(w http.ResponseWriter, r *http.Request
                        return
                }
        }
-
        w.WriteHeader(http.StatusOK)
-
 }
 
 /** TEST
index c92cf691e6428613bc158dd5b74e577e4b0a1fca..0e604448dab6198cd439bc245ff5df3e7263fb40 100644 (file)
 package dbhandling_test
 
 import (
+       "encoding/json"
+       "fmt"
        "net/http/httptest"
+       "slices"
        "songmanager/pkg/dbhandling"
+       "strconv"
        "testing"
 )
 
 
 func TestRoomsGet(t *testing.T) {
-
-       r := httptest.NewRequest("GET", "/api/rooms/get", &EmptyReader{})
+       r := httptest.NewRequest("GET", "/api/rooms/get/all", &EmptyReader{})
        wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs")
 
        // subtests
        t.Run("RoomsGet1", func(t *testing.T) {
                recorder := httptest.NewRecorder()
                wrapper.RoomsGet(recorder, r)
+
+               l := make([]string , 2)
+
+               err := json.Unmarshal(recorder.Body.Bytes(), &l)
+               if err != nil {
+                       t.Error("Failed to Unmarshal the Body")
+               }
+
+               if !slices.Contains(l, "hits") { t.Error("Did not contain 'hits'") }
+               if !slices.Contains(l, "rock") { t.Error("Did not contain 'rock'") }
+       })
+}
+
+func TestRoomsGetByName(t *testing.T) {
+       wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs")
+
+       // subtests
+       t.Run("RoomsGet1", func(t *testing.T) {
+               r := httptest.NewRequest("GET", "/api/rooms/get/hits", &EmptyReader{})
+               r.SetPathValue("name", "hits")
+               recorder := httptest.NewRecorder()
+               wrapper.RoomsGetByName(recorder, r)
+
+               rep := make([]string, 10)
+
+               err := json.Unmarshal(recorder.Body.Bytes(), &rep)
+               if err != nil { t.Error("Failed to Unmarshal the response") }
+
+               fmt.Println(rep)
+
+               var ids []int
+               for _, k := range rep {
+                       if k != "" {
+                               n, err := strconv.Atoi(k)
+                               if err != nil { t.Error("ID was not a valid number: ", k) }
+                               ids = append(ids, n)
+                       }
+               }
+
+               if len(ids) != 10 { t.Error("Not the expected amount of ids", ids) }
+
+       })
+
+       // subtests
+       t.Run("RoomsGet2", func(t *testing.T) {
+               r := httptest.NewRequest("GET", "/api/rooms/get/rock", &EmptyReader{})
+               r.SetPathValue("name", "rock")
+               recorder := httptest.NewRecorder()
+               wrapper.RoomsGetByName(recorder, r)
+
+               rep := make([]string, 10)
+
+               err := json.Unmarshal(recorder.Body.Bytes(), &rep)
+               if err != nil { t.Error("Failed to Unmarshal the response") }
+
+               fmt.Println(rep)
+
+               var ids []int
+               for _, k := range rep {
+                       if k != "" {
+                               n, err := strconv.Atoi(k)
+                               if err != nil { t.Error("ID was not a valid number: ", k) }
+                               ids = append(ids, n)
+                       }
+               }
+
+               if len(ids) != 10 { t.Error("Not the expected amount of ids", ids) }
+
+       })
+}
+
+type testAddReader struct {}
+var testAddSongIds = []int{10, 11, 12, 13}
+
+func (a *testAddReader) Read(p []byte) (int, error) {
+       bin, err := json.Marshal(testAddSongIds)
+       if err != nil {
+               return 0, err
+       }
+
+       if len(bin) > len(p) { return 0, err }
+
+       count := copy(p, bin)
+       return count, nil
+}
+
+func TestRoomsAddByNameAndId(t *testing.T) {
+       wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs")
+
+       t.Run("RoomsAddByNameAndId1", func(t *testing.T) {
+               r := httptest.NewRequest("GET", "/api/rooms/add/hits", &testAddReader{})
+               r.SetPathValue("name", "hits")
+               recorder := httptest.NewRecorder()
+               wrapper.RoomsAddByNameAndId(recorder, r)
+
+               r2 := httptest.NewRequest("GET", "/api/rooms/get/hits", &EmptyReader{})
+               r2.SetPathValue("name", "hits")
+
+               recorder2 := httptest.NewRecorder()
+               wrapper.RoomsGetByName(recorder2, r2)
+
+               rep := make([]string, 14)
+
+               err := json.Unmarshal(recorder2.Body.Bytes(), &rep)
+               if err != nil { t.Error("Failed to Unmarshal the response") }
+
+               fmt.Println(rep)
+
+               var ids []int
+               for _, k := range rep {
+                       if k != "" {
+                               n, err := strconv.Atoi(k)
+                               if err != nil { t.Error("ID was not a valid number: ", k) }
+                               ids = append(ids, n)
+                       }
+               }
+
+               if len(ids) != 14 { t.Error("Not the expected amount of ids", ids) }
        })
 }
index 6d41e51055df7052d42ffa14731933e606b054eb..6bbdb050d97e168dd86e5b74818098c6b2aa376a 100644 (file)
@@ -22,6 +22,7 @@ def main():
         f = open(f"./testing/songs/{line[4]}", "w+")
         f.close()
         r.zadd("hits", {i: i})
+        r.zadd("rock", {i: i})
 
     # 'song:' + i,
     # 'artistName',