From 6b446d1b57affec929111eeeed055b9a7bc11011 Mon Sep 17 00:00:00 2001
From: Akif9748 <akif9748@gmail.com>
Date: Sun, 3 Apr 2022 21:39:26 +0300
Subject: [PATCH] Added message post to api

---
 routes/api/index.js               | 16 ++++++++++
 routes/register.js                |  2 +-
 routes/threads.js                 | 17 ++++++-----
 apitest.py => tests/getmessage.py |  0
 tests/postmessage.py              | 50 +++++++++++++++++++++++++++++++
 5 files changed, 76 insertions(+), 9 deletions(-)
 rename apitest.py => tests/getmessage.py (100%)
 create mode 100644 tests/postmessage.py

diff --git a/routes/api/index.js b/routes/api/index.js
index 48efb07..7af832f 100644
--- a/routes/api/index.js
+++ b/routes/api/index.js
@@ -78,6 +78,22 @@ app.get("/message/:id", (req, res) => {
 
 })
 
+app.post("/message/", (req, res) => {
+
+    const error = (status, error) =>
+        res.status(status).json(new ApiResponse(status, { error }));
+    const { threadID = null, content = null } = req.body;
+    const thread = new Thread().getId(threadID);
+    if (!req.body.content) return error(400, "Missing message content in request body.");
+    if (!thread) return error(404, "We have not got this thread.");
+
+
+    const message = new Message(content, new User().getName(req.headers.username), thread).takeId().write();
+    thread.push(message.id).write();
+   
+    res.status(200).json(new ApiResponse(200, message));
+
+})
 app.get("/user/:id", (req, res) => {
 
     const error = (status, error) =>
diff --git a/routes/register.js b/routes/register.js
index 2a23017..823d4a7 100644
--- a/routes/register.js
+++ b/routes/register.js
@@ -22,7 +22,7 @@ app.post("/", (req, res) => {
             error(res, 404, `We have got an user named ${username}!`)
 
         else {
-            const user2 = new User(req.body.username, req.body.avatar).takeId()
+            const user2 = new User(req.body.username, req.body.avatar ?? null).takeId()
             db.set("secret." + username, { id: user2.id, key: password })
             req.session.loggedin = true;
             req.session.username = username;
diff --git a/routes/threads.js b/routes/threads.js
index 3bdc51f..1d78488 100644
--- a/routes/threads.js
+++ b/routes/threads.js
@@ -19,6 +19,13 @@ app.get("/", (req, res) => {
 });
 
 
+app.get("/open*", (req, res) => {
+
+    const user = new User().getId(req.session.userid)
+    res.render("openThread", { user })
+
+});
+
 app.get("/:id", (req, res) => {
 
     const { id } = req.params;
@@ -34,17 +41,11 @@ app.get("/:id", (req, res) => {
 });
 
 
+
+
 app.use(require("../middlewares/login"));
 
 
-app.get("/open*", (req, res) => {
-
-    const user = new User().getId(req.session.userid)
-    res.render("openThread", { user })
-
-});
-
-
 app.post("/", (req, res) => {
 
     const user = new User().getId(req.session.userid);
diff --git a/apitest.py b/tests/getmessage.py
similarity index 100%
rename from apitest.py
rename to tests/getmessage.py
diff --git a/tests/postmessage.py b/tests/postmessage.py
new file mode 100644
index 0000000..e11c811
--- /dev/null
+++ b/tests/postmessage.py
@@ -0,0 +1,50 @@
+import requests
+
+# Headers for login to Akf-forum
+headers = {
+    "username": "testUser",
+    "password": "testPassword"
+}
+
+# Body for message parameters
+body = {
+    "content": "This message sent via API",
+    "threadID": 1
+}
+
+r = requests.post("http://localhost:3000/api/message/",
+                  headers=headers, data=body)
+
+print(r.json())
+
+example_response = {
+    'status': 200,
+    'result': {
+        'content': 'This message sent via API',
+        'author': {
+            'name': 'testUser',
+            'avatar': '',
+            'time': 1649009854217,
+            'admin': False,
+            'deleted': False,
+            'id': 2
+        },
+        'time': 1649010863471,
+        'thread':  {
+            'author': {
+                'name': 'Akif9748',
+                'avatar': 'https://www.technopat.net/sosyal/data/avatars/o/298/298223.jpg?1644694020',
+                'time': 1647895891332, 'admin': True, 'deleted': False, 'id': 0
+            },
+            'title': 'API TEST',
+            'messages': [4, 6],
+            'time': 1649010834064,
+            'deleted': False,
+            'id': 1
+        },
+        'deleted': False,
+        'edited': False,
+        'react': {},
+        'id': 6
+    }
+}