From 1c62f8006447f9275ff6ab1d2b8515cd2c03622e Mon Sep 17 00:00:00 2001 From: efim Date: Sun, 29 Oct 2023 14:49:19 +0000 Subject: [PATCH] feat: room model, encode json, sample redis write --- main.go | 4 ++ rooms/rooms_manager.go | 83 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 rooms/rooms_manager.go diff --git a/main.go b/main.go index 3ccc211..f072140 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "github.com/redis/go-redis/v9" + "sunshine.industries/some-automoderation/rooms" "sunshine.industries/some-automoderation/routes" "sunshine.industries/some-automoderation/sessions" ) @@ -30,6 +31,9 @@ func main() { fmt.Printf("Server will start on port %d\n", port) + rooms := rooms.RedisRM { Rdb: rdb, } + rooms.Test() + sessions := sessions.RedisSM{ Rdb: rdb, } routes.RegisterRoutes(sessions) diff --git a/rooms/rooms_manager.go b/rooms/rooms_manager.go new file mode 100644 index 0000000..576c7ed --- /dev/null +++ b/rooms/rooms_manager.go @@ -0,0 +1,83 @@ +package rooms + +import ( + "context" + "encoding/json" + "log" + + "github.com/redis/go-redis/v9" +) + +type Person struct { + PersonId int64 + Name string + PasswordHash string +} + +type Room struct { + RoomId int64 + AdminIds []int64 + Name string + PasswordHash string + Paricipants []Person + // TODO hands, for each type of hand? + // i guess participants order fixed for now? + // and i'll still need 'current' for each hand level +} + +// well, it seems that i'd better do marshalling into bytes then +// see https://github.com/redis/go-redis/issues/2512 +func (r *Room) MarshalBinary() (data []byte, err error) { + return json.Marshal(r) +} +func (r *Room) UnmarshalBinary(data []byte) error { + return json.Unmarshal(data, r) +} + + +// let's check whether it will be possible to save nested structs + +var ctx = context.Background() + +type RedisRM struct { + Rdb *redis.Client +} + +func (redisRM RedisRM) Test() { + testRoom := Room{ + RoomId: 1234, + AdminIds: []int64{1111, 2222}, + Name: "test room", + PasswordHash: "abcde", + Paricipants: []Person{ + {PersonId: 1111, + Name: "admin1", + PasswordHash: "abcde1", + }, + { + PersonId: 2222, + Name: "admin2", + PasswordHash: "abcdeee", + }, + { + PersonId: 3333, + Name: "nonadmin yay", + PasswordHash: "abcdee123", + }, + }, + } + err := redisRM.Rdb.Set(ctx, "room:my-room-1", &testRoom, 0).Err() + if err != nil { + log.Printf("error writing room: %s", err) + } else { + log.Printf("successfully written room %d", testRoom.RoomId) + } + + var readRoom Room + err = redisRM.Rdb.Get(ctx, "room:my-room-1").Scan(&readRoom) + if err != nil { + log.Printf("error reading room %s", err) + } else { + log.Printf("successfully read room %+v", readRoom) + } +}