Added thread to API

This commit is contained in:
Akif9748 2022-04-03 22:03:47 +03:00
parent 6b446d1b57
commit c22f72dd90
10 changed files with 143 additions and 70 deletions

View file

@ -3,30 +3,27 @@
Akf-forum has got an API for other clients etc.
You can find an example in apitest.py.
You can find examples in `/tests` folder.
## Authorization
You need this headers for send request to API:
```jsonc
```json
{
"username": "testUser",
"password": "testPassword"
}
```
## How to request?
### Request type:
`GET /api/action/id`
`GET /api/action`
### "action" types:
- `message`
(for now, only message.)
### "id":
ID for action type.
- GET `messages/:id`
- GET `users/:id`
- GET `threads/:id`
- POST `messages`
### Example request:

View file

@ -34,7 +34,7 @@ And, you can learn informations about API in `APIDOCS.md`.
- [ ] Singature & About me
- [ ] Edit user
- [ ] Messages
- [x] Ratelimit for sending message
- [ ] Ratelimit for sending message
- [x] Send message
- [x] Delete message
- [ ] Edit message
@ -44,13 +44,17 @@ And, you can learn informations about API in `APIDOCS.md`.
- [ ] Edit it!
- [ ] Delete it!
- [ ] Other
- [x] API
- [x] Other client for forum via API
- [x] Footer of the site
- [ ] Multiple theme support
- [ ] Search
- [x] New Thread theme, better render for messages
- [ ] sending message etc. Will turn api model
- [ ] API
- [x] Other client for forum via API
- [ ] Deleting message
- [x] Sending message
- [ ] Open thread
- [x] Get Thread info
## Image:
![image](https://user-images.githubusercontent.com/70021050/160255959-ef216cba-1348-4d4b-9347-fe67e21348e7.png)

View file

@ -0,0 +1,7 @@
class ApiResponse {
constructor(status, result) {
this.status = status;
this.result = result;
}
}
module.exports = ApiResponse;

View file

@ -3,12 +3,6 @@ const db = require("quick.db");
const { Router } = require("express")
const app = Router();
class ApiResponse {
constructor(status, result) {
this.status = status;
this.result = result;
}
}
const { request, response } = require("express");
@ -59,55 +53,9 @@ app.use((req, res, next) => {
next();
})
app.get("/message/:id", (req, res) => {
const error = (status, error) =>
res.status(status).json(new ApiResponse(status, { error }));
const { id = null } = req.params;
if (!id) return error(400, "Missing id in query")
const message = new Message().getId(id);
if (!message || message.deleted) return error(404, "We have not got any message declared as this id.");
res.status(200).json(new ApiResponse(200, message));
})
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) =>
res.status(status).json(new ApiResponse(status, { error }))
const { id = null } = req.params;
if (!id) return error(400, "Missing id in query")
const member = new User().getId(id);
if (!member || member.deleted) return error(404, "We have not got any user declared as this id.");
res.status(200).json(new ApiResponse(200, member));
});
app.use("/messages", require("./routes/message"))
app.use("/users", require("./routes/user"))
app.use("/threads", require("./routes/threads"))
app.all("*", (req, res) => res.status(400).json(new ApiResponse(400, { error: "Bad request" })));

View file

@ -0,0 +1,41 @@
const { User, Message, Thread } = require("../../../classes");
const ApiResponse = require("../ApiResponse");
const { Router } = require("express")
const app = Router();
app.get("/:id", (req, res) => {
const error = (status, error) =>
res.status(status).json(new ApiResponse(status, { error }));
const { id = null } = req.params;
if (!id) return error(400, "Missing id in query")
const message = new Message().getId(id);
if (!message || message.deleted) return error(404, "We have not got any message declared as this id.");
res.status(200).json(new ApiResponse(200, message));
})
app.post("/", (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 (!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));
})
module.exports = app;

View file

@ -0,0 +1,23 @@
const { Thread } = require("../../../classes");
const ApiResponse = require("../ApiResponse");
const { Router } = require("express")
const app = Router();
app.get("/:id", (req, res) => {
const error = (status, error) =>
res.status(status).json(new ApiResponse(status, { error }))
const { id = null } = req.params;
if (!id) return error(400, "Missing id in query")
const thread = new Thread().getId(id);
if (!thread || thread.deleted) return error(404, "We have not got any thread declared as this id.");
res.status(200).json(new ApiResponse(200, thread));
});
module.exports = app;

22
routes/api/routes/user.js Normal file
View file

@ -0,0 +1,22 @@
const { User } = require("../../../classes");
const ApiResponse = require("../ApiResponse");
const { Router } = require("express")
const app = Router();
app.get("/:id", (req, res) => {
const error = (status, error) =>
res.status(status).json(new ApiResponse(status, { error }))
const { id = null } = req.params;
if (!id) return error(400, "Missing id in query")
const member = new User().getId(id);
if (!member || member.deleted) return error(404, "We have not got any user declared as this id.");
res.status(200).json(new ApiResponse(200, member));
});
module.exports = app;

View file

@ -7,7 +7,7 @@ headers = {
}
r = requests.get("http://localhost:3000/api/message/1/", headers=headers)
r = requests.get("http://localhost:3000/api/messages/1/", headers=headers)
print(r.json())

31
tests/get_thread.py Normal file
View file

@ -0,0 +1,31 @@
import requests
# Headers for login to Akf-forum
headers = {
"username": "testUser",
"password": "testPassword"
}
r = requests.get("http://localhost:3000/api/threads/0", headers=headers)
print(r.json())
example_response = {
"status": 200,
"result": {
"author": {
"name": "Akif9748",
"avatar": "https://www.technopat.net/sosyal/data/avatars/o/298/298223.jpg?1644694020",
"time": 1647895891332,
"admin": False,
"deleted": False,
"id": 0
},
"title": "First Thread",
"messages": [0, 1, 2, 3],
"time": 1647895907054,
"deleted": False,
"id": 0
}
}

View file

@ -12,7 +12,7 @@ body = {
"threadID": 1
}
r = requests.post("http://localhost:3000/api/message/",
r = requests.post("http://localhost:3000/api/messages/",
headers=headers, data=body)
print(r.json())