mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-11-29 14:10:41 +03:00
Added thread to API
This commit is contained in:
parent
6b446d1b57
commit
c22f72dd90
10 changed files with 143 additions and 70 deletions
17
APIDOCS.md
17
APIDOCS.md
|
@ -3,30 +3,27 @@
|
||||||
|
|
||||||
Akf-forum has got an API for other clients etc.
|
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
|
## Authorization
|
||||||
You need this headers for send request to API:
|
You need this headers for send request to API:
|
||||||
```jsonc
|
```json
|
||||||
{
|
{
|
||||||
"username": "testUser",
|
"username": "testUser",
|
||||||
"password": "testPassword"
|
"password": "testPassword"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## How to request?
|
## How to request?
|
||||||
|
|
||||||
### Request type:
|
### Request type:
|
||||||
`GET /api/action/id`
|
`GET /api/action`
|
||||||
|
|
||||||
### "action" types:
|
### "action" types:
|
||||||
- `message`
|
- GET `messages/:id`
|
||||||
|
- GET `users/:id`
|
||||||
(for now, only message.)
|
- GET `threads/:id`
|
||||||
|
- POST `messages`
|
||||||
### "id":
|
|
||||||
ID for action type.
|
|
||||||
|
|
||||||
|
|
||||||
### Example request:
|
### Example request:
|
||||||
|
|
10
README.md
10
README.md
|
@ -34,7 +34,7 @@ And, you can learn informations about API in `APIDOCS.md`.
|
||||||
- [ ] Singature & About me
|
- [ ] Singature & About me
|
||||||
- [ ] Edit user
|
- [ ] Edit user
|
||||||
- [ ] Messages
|
- [ ] Messages
|
||||||
- [x] Ratelimit for sending message
|
- [ ] Ratelimit for sending message
|
||||||
- [x] Send message
|
- [x] Send message
|
||||||
- [x] Delete message
|
- [x] Delete message
|
||||||
- [ ] Edit message
|
- [ ] Edit message
|
||||||
|
@ -44,13 +44,17 @@ And, you can learn informations about API in `APIDOCS.md`.
|
||||||
- [ ] Edit it!
|
- [ ] Edit it!
|
||||||
- [ ] Delete it!
|
- [ ] Delete it!
|
||||||
- [ ] Other
|
- [ ] Other
|
||||||
- [x] API
|
|
||||||
- [x] Other client for forum via API
|
|
||||||
- [x] Footer of the site
|
- [x] Footer of the site
|
||||||
- [ ] Multiple theme support
|
- [ ] Multiple theme support
|
||||||
- [ ] Search
|
- [ ] Search
|
||||||
- [x] New Thread theme, better render for messages
|
- [x] New Thread theme, better render for messages
|
||||||
- [ ] sending message etc. Will turn api model
|
- [ ] 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:
|
||||||
![image](https://user-images.githubusercontent.com/70021050/160255959-ef216cba-1348-4d4b-9347-fe67e21348e7.png)
|
![image](https://user-images.githubusercontent.com/70021050/160255959-ef216cba-1348-4d4b-9347-fe67e21348e7.png)
|
||||||
|
|
||||||
|
|
7
routes/api/ApiResponse.js
Normal file
7
routes/api/ApiResponse.js
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
class ApiResponse {
|
||||||
|
constructor(status, result) {
|
||||||
|
this.status = status;
|
||||||
|
this.result = result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
module.exports = ApiResponse;
|
|
@ -3,12 +3,6 @@ const db = require("quick.db");
|
||||||
const { Router } = require("express")
|
const { Router } = require("express")
|
||||||
const app = Router();
|
const app = Router();
|
||||||
|
|
||||||
class ApiResponse {
|
|
||||||
constructor(status, result) {
|
|
||||||
this.status = status;
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const { request, response } = require("express");
|
const { request, response } = require("express");
|
||||||
|
|
||||||
|
@ -59,55 +53,9 @@ app.use((req, res, next) => {
|
||||||
next();
|
next();
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get("/message/:id", (req, res) => {
|
app.use("/messages", require("./routes/message"))
|
||||||
|
app.use("/users", require("./routes/user"))
|
||||||
const error = (status, error) =>
|
app.use("/threads", require("./routes/threads"))
|
||||||
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.all("*", (req, res) => res.status(400).json(new ApiResponse(400, { error: "Bad request" })));
|
app.all("*", (req, res) => res.status(400).json(new ApiResponse(400, { error: "Bad request" })));
|
||||||
|
|
||||||
|
|
41
routes/api/routes/message.js
Normal file
41
routes/api/routes/message.js
Normal 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;
|
23
routes/api/routes/threads.js
Normal file
23
routes/api/routes/threads.js
Normal 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
22
routes/api/routes/user.js
Normal 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;
|
|
@ -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())
|
print(r.json())
|
||||||
|
|
31
tests/get_thread.py
Normal file
31
tests/get_thread.py
Normal 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
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,7 +12,7 @@ body = {
|
||||||
"threadID": 1
|
"threadID": 1
|
||||||
}
|
}
|
||||||
|
|
||||||
r = requests.post("http://localhost:3000/api/message/",
|
r = requests.post("http://localhost:3000/api/messages/",
|
||||||
headers=headers, data=body)
|
headers=headers, data=body)
|
||||||
|
|
||||||
print(r.json())
|
print(r.json())
|
Loading…
Reference in a new issue