diff --git a/backend/src/index.ts b/backend/src/index.ts index af98f24..068cc71 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -42,6 +42,38 @@ app.use("/user", userRouter); app.use("/file", fileRouter); app.use("/email", emailRouter); +// 要添加的新接口 +app.post("/sendMessage", async (req, res) => { + // 从 Hasura Action 的请求体中获取参数 + const { user_uuid, room_uuid, content } = req.body.input; + + // 输入验证 + if (!content || content.trim() === "") { + return res.status(400).json({ + message: "Content cannot be empty", + }); + } + + // 调用 Hasura GraphQL API 来插入数据 + try { + const data = await sdk.addMessage({ + user_uuid: user_uuid, + room_uuid: room_uuid, + content: content, + }); + + // 返回成功响应 + return res.json({ + uuid: data.insert_message_one?.uuid, + }); + } catch (err) { + console.error(err); + return res.status(500).json({ + message: "Failed to send message", + }); + } +}); + app.listen(port, () => { console.log(`Server running at ${address}:${port}/`); }); diff --git a/backend/src/user.ts b/backend/src/user.ts index a93bd91..237b6a5 100644 --- a/backend/src/user.ts +++ b/backend/src/user.ts @@ -14,6 +14,15 @@ const router = express.Router(); router.post("/login", async (req, res) => { const { username, password } = req.body; + if (username.length < 3 || username.length > 20) { + return res.status(400).json({ + message: "Username must be between 3 and 20 characters", + extensions: { + code: "USER_INPUT_ERROR", + path: "$.args.username" + } + }); + } if (!username || !password) { return res.status(422).send("422 Unprocessable Entity: Missing username or password"); } diff --git a/database/graphql/message.graphql b/database/graphql/message.graphql index 994647c..3b24b6a 100644 --- a/database/graphql/message.graphql +++ b/database/graphql/message.graphql @@ -15,3 +15,14 @@ subscription getMessagesByRoom($room_uuid: uuid!) { created_at } } + +query getMessagesByUser($user_uuid: uuid!) { + message(where: {user_uuid: {_eq: $user_uuid}}, order_by: {created_at: desc}) { + uuid + content + created_at + room { + name + } + } +} diff --git a/database/graphql/room.graphql b/database/graphql/room.graphql index 4f37de0..bc9cfb5 100644 --- a/database/graphql/room.graphql +++ b/database/graphql/room.graphql @@ -28,3 +28,14 @@ mutation joinRoom($user_uuid: uuid!, $room_uuid: uuid!) { room_uuid } } + +mutation quitRoom($user_uuid: uuid!, $room_uuid: uuid!) { + delete_user_room( + where: {user_uuid: {_eq: $user_uuid}, room_uuid: {_eq: $room_uuid}} + ) { + returning { + user_uuid + room_uuid + } + } +} diff --git a/database/graphql/user.graphql b/database/graphql/user.graphql index d7780cd..3391efa 100644 --- a/database/graphql/user.graphql +++ b/database/graphql/user.graphql @@ -10,3 +10,13 @@ query getUsersByUsername($username: String!) { password } } + +mutation deleteUser($uuid: uuid!) { + delete_user(where: {uuid: {_eq: $uuid}}) { + returning { + uuid + username + } + } +} + diff --git a/database/hasura/docker-compose.yaml b/database/hasura/docker-compose.yaml new file mode 100644 index 0000000..39be6ca --- /dev/null +++ b/database/hasura/docker-compose.yaml @@ -0,0 +1,24 @@ +version: "3.6" +services: + postgres: + image: postgres:latest + container_name: postgres + restart: always + volumes: + - ~/data/postgres:/var/lib/postgresql/data + ports: + - "5432:5432" + environment: + POSTGRES_PASSWORD: postgrespassword + graphql-engine: + image: hasura/graphql-engine:latest + container_name: hasura + ports: + - "23333:8080" + depends_on: + - "postgres" + restart: always + environment: + HASURA_GRAPHQL_DATABASE_URL: postgres://postgres:postgrespassword@postgres:5432/postgres + HASURA_GRAPHQL_ENABLE_CONSOLE: "true" + HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log \ No newline at end of file diff --git a/frontend/public/about-me.html b/frontend/public/about-me.html new file mode 100644 index 0000000..36b6dfe --- /dev/null +++ b/frontend/public/about-me.html @@ -0,0 +1,89 @@ + + +
+ +你好!我叫懒人,是电子系一名学生。
+ +前天有个会要开,完全忘记了!
+ +想回首页?点这里
+ + +加载中…
+点击这里查看 关于我
+