import (
"encoding/json"
"net/http"
+
+ "github.com/redis/go-redis/v9"
)
/** TEST
/** 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)
}