From d036a970eb4984e30c7f95ecc9afecf4eb8902bb Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 11 Feb 2023 08:57:42 +0100 Subject: [PATCH] added chats --- modules/scylladb/models.go | 13 +++++++++++++ modules/structs/chat.go | 18 ++++++++++++++++++ routers/api/v1/chats/chats.go | 35 +++++++++++++++++++++++++++++++++++ routers/router/router.go | 4 ++++ 4 files changed, 70 insertions(+) create mode 100644 modules/structs/chat.go create mode 100644 routers/api/v1/chats/chats.go diff --git a/modules/scylladb/models.go b/modules/scylladb/models.go index 1a033b3..f83565d 100644 --- a/modules/scylladb/models.go +++ b/modules/scylladb/models.go @@ -56,4 +56,17 @@ var ( }, PartKey: []string{"x_token"}, }) + + Chats = table.New(table.Metadata{ + Name: "chats", + Columns: []string{ + "user_first_id", + "user_second_id", + "sync_count", + "last_messages", + "blocked_state", + "created_at", + }, + PartKey: []string{"user_first_id", "user_second_id"}, + }) ) diff --git a/modules/structs/chat.go b/modules/structs/chat.go new file mode 100644 index 0000000..6a2e7a9 --- /dev/null +++ b/modules/structs/chat.go @@ -0,0 +1,18 @@ +package structs + +// TABLE chats +type Chat struct { + UserFirstId string + UserSecondId string + // Represents the current number of message activities in a chat. This includes reactions to messages, replying to messages, deleting messages, and more + SyncCount int + // Contains number X of last message activities. Needed e.g. for web + LastMessages []string + BlockedState uint8 + CreatedAt int64 +} + +// swagger:model ChatsResponse +type ChatsResponse struct { + Chats []Chat +} diff --git a/routers/api/v1/chats/chats.go b/routers/api/v1/chats/chats.go new file mode 100644 index 0000000..aff93ee --- /dev/null +++ b/routers/api/v1/chats/chats.go @@ -0,0 +1,35 @@ +package chats + +import ( + "clickandjoin.app/managementsystem/modules/scylladb" + "clickandjoin.app/managementsystem/modules/structs" + "github.com/gofiber/fiber/v2" + "github.com/sirupsen/logrus" +) + +func GetAllChats(c *fiber.Ctx) error { + // swagger:operation GET /chats chats chatsGetAllChats + // --- + // summary: List of chats + // consumes: + // - application/json + // produces: + // - application/json + // responses: + // '200': + // description: List of chats + // schema: + // "$ref": "#/definitions/ChatsResponse" + // '500': + // description: Internal server error + + var chats []structs.Chat + + q := scylladb.Session.Query(scylladb.Chats.SelectAll()) + + if err := q.SelectRelease(&chats); err != nil { + logrus.Errorln("Failed to get chats, err:", err) + } + + return c.JSON(structs.ChatsResponse{Chats: chats}) +} diff --git a/routers/router/router.go b/routers/router/router.go index 76650c2..ed9e62c 100644 --- a/routers/router/router.go +++ b/routers/router/router.go @@ -2,6 +2,7 @@ package router import ( "clickandjoin.app/managementsystem/modules/config" + "clickandjoin.app/managementsystem/routers/api/v1/chats" "clickandjoin.app/managementsystem/routers/api/v1/users" "clickandjoin.app/managementsystem/routers/api/v1/wssessions" "github.com/gofiber/fiber/v2" @@ -16,6 +17,9 @@ func SetupRoutes(app *fiber.App) { wss := v1.Group("/wssessions") wss.Get("/", ApiKeyValidation, wssessions.GetAllWsSessions) + + c := v1.Group("/chats") + c.Get("/", ApiKeyValidation, chats.GetAllChats) } func ApiKeyValidation(c *fiber.Ctx) error {