err := json.NewDecoder(r.Body).Decode(&assignedSongKeys)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
- fmt.Println("What went wrong,", err)
return
}
w.WriteHeader(http.StatusOK)
}
-/** 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)
+ var assignedSongKeys []int
+ err := json.NewDecoder(r.Body).Decode(&assignedSongKeys)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
+ name := r.PathValue("name")
+
// 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()
+ for _, k := range assignedSongKeys {
+ _, err = dbw.RedisDb.ZRem(dbw.Ctx, name, k).Result()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
w.WriteHeader(http.StatusOK)
}
-/** TEST
+/**
* Delete a room by giving the name as a string, this just means the room won't exist anymore
+ * the ids should be given as a list in the body of the request
**/
func (dbw *DBWrapper) RoomsDeleteByName(w http.ResponseWriter, r *http.Request) {
+ name := r.PathValue("name")
-
- // 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()
+ _, err := dbw.RedisDb.Del(dbw.Ctx, name).Result()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
import (
"encoding/json"
"fmt"
+ "net/http"
"net/http/httptest"
"slices"
"songmanager/pkg/dbhandling"
wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs")
t.Run("RoomsAddByNameAndId1", func(t *testing.T) {
- r := httptest.NewRequest("GET", "/api/rooms/add/hits", &testAddReader{})
+ r := httptest.NewRequest(http.MethodPut, "/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 := httptest.NewRequest(http.MethodGet, "/api/rooms/get/hits", &EmptyReader{})
r2.SetPathValue("name", "hits")
recorder2 := httptest.NewRecorder()
if len(ids) != 14 { t.Error("Not the expected amount of ids", ids) }
})
}
+
+type testRemoveReader struct {}
+
+var testRemoveSongsId = []int{0, 1, 2, 3}
+
+func (r *testRemoveReader) Read(p []byte) (int, error) {
+ bin, err := json.Marshal(testRemoveSongsId)
+ if err != nil {
+ return 0, err
+ }
+
+ if len(bin) > len(p) { return 0, err }
+
+ count := copy(p, bin)
+ return count, nil
+}
+
+func TestRoomsRemoveByNameAndId(t *testing.T) {
+ wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs")
+
+ t.Run("RoomsRemoveByNameAndId1", func(t *testing.T) {
+ r := httptest.NewRequest(http.MethodPut, "/api/rooms/remove/hits", &testRemoveReader{})
+ r.SetPathValue("name", "hits")
+ recorder := httptest.NewRecorder()
+
+ wrapper.RoomsRemoveByNameAndId(recorder, r)
+
+ r2 := httptest.NewRequest(http.MethodGet, "/api/rooms/get/hits", &EmptyReader{})
+ r2.SetPathValue("name", "hits")
+
+ recorder2 := httptest.NewRecorder()
+ wrapper.RoomsGetByName(recorder2, r2)
+
+ rep := make([]string, 10)
+
+ 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) != 10 { t.Error("Not the expected amount of ids", ids) }
+ })
+}
+
+func TestRoomsDeleteByName(t *testing.T) {
+ wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs")
+
+ t.Run("RoomsDeleteByName-1", func(t *testing.T) {
+ r := httptest.NewRequest(http.MethodDelete, "/api/rooms/delete/rock", &EmptyReader{})
+ r.SetPathValue("name", "rock")
+ recorder := httptest.NewRecorder()
+
+ wrapper.RoomsDeleteByName(recorder, r)
+
+ if recorder.Code != 200 { t.Error("Incorrect HTTP return code") }
+
+ r2 := httptest.NewRequest(http.MethodGet, "/api/rooms/get/all", &EmptyReader{})
+ recorder2 := httptest.NewRecorder()
+ wrapper.RoomsGet(recorder2, r2)
+
+ l := make([]string , 1)
+
+ err := json.Unmarshal(recorder2.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 contain 'rock'") }
+ })
+}