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) {
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 {
}
-/** 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
// 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()
return
}
}
-
w.WriteHeader(http.StatusOK)
-
}
/** TEST
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) }
})
}