mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-11-26 05:10:41 +03:00
Better syntax
This commit is contained in:
parent
1e80da99a8
commit
6beeddd17e
1 changed files with 51 additions and 44 deletions
|
@ -1,12 +1,8 @@
|
||||||
const { User, Message, Thread } = require("../../classes");
|
const { User, Message, Thread } = require("../../classes");
|
||||||
const db = require("quick.db");
|
const db = require("quick.db");
|
||||||
|
|
||||||
|
|
||||||
const { Router } = require("express")
|
const { Router } = require("express")
|
||||||
|
|
||||||
const app = Router();
|
const app = Router();
|
||||||
|
|
||||||
|
|
||||||
class ApiResponse {
|
class ApiResponse {
|
||||||
constructor(status, result) {
|
constructor(status, result) {
|
||||||
this.status = status;
|
this.status = status;
|
||||||
|
@ -16,18 +12,6 @@ class ApiResponse {
|
||||||
|
|
||||||
const { request, response } = require("express");
|
const { request, response } = require("express");
|
||||||
|
|
||||||
/**
|
|
||||||
* For intellisense
|
|
||||||
* @param {request} req
|
|
||||||
* @param {response} res
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
app.get("/:action/:id", (req, res) => {
|
|
||||||
|
|
||||||
const error = (status, error) =>
|
|
||||||
res.status(status).json(new ApiResponse(status, { error }))
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AUTH TYPE:
|
* AUTH TYPE:
|
||||||
|
@ -40,6 +24,26 @@ app.get("/:action/:id", (req, res) => {
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* REQUEST TYPE:
|
||||||
|
* GET /api/action/id
|
||||||
|
*
|
||||||
|
* @example message action:
|
||||||
|
* GET /api/message/0
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For intellisense
|
||||||
|
* @param {request} req
|
||||||
|
* @param {response} res
|
||||||
|
*/
|
||||||
|
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
const error = (status, error) =>
|
||||||
|
res.status(status).json(new ApiResponse(status, { error }))
|
||||||
|
|
||||||
const { username = null, password = null } = req.headers;
|
const { username = null, password = null } = req.headers;
|
||||||
|
|
||||||
if (!username || !password)
|
if (!username || !password)
|
||||||
|
@ -52,21 +56,16 @@ app.get("/:action/:id", (req, res) => {
|
||||||
|
|
||||||
if (user.key !== password)
|
if (user.key !== password)
|
||||||
return error(401, 'Incorrect Password!')
|
return error(401, 'Incorrect Password!')
|
||||||
|
next();
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get("/message/:id", (req, res) => {
|
||||||
|
|
||||||
|
const error = (status, error) =>
|
||||||
|
res.status(status).json(new ApiResponse(status, { error }));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* REQUEST TYPE:
|
|
||||||
* GET /api/action/id
|
|
||||||
*
|
|
||||||
* @example message action:
|
|
||||||
* GET /api/message/0
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
const { action } = req.params;
|
|
||||||
|
|
||||||
if (action === "message") {
|
|
||||||
|
|
||||||
const { id = null } = req.params;
|
const { id = null } = req.params;
|
||||||
if (!id) return error(400, "Missing id in query")
|
if (!id) return error(400, "Missing id in query")
|
||||||
const message = new Message().getId(id);
|
const message = new Message().getId(id);
|
||||||
|
@ -74,18 +73,26 @@ app.get("/:action/:id", (req, res) => {
|
||||||
if (!message || message.deleted) return error(404, "We have not got any message declared as this 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));
|
res.status(200).json(new ApiResponse(200, message));
|
||||||
} else if (action === "user") {
|
|
||||||
|
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get("/user/:id", (req, res) => {
|
||||||
|
|
||||||
|
const error = (status, error) =>
|
||||||
|
res.status(status).json(new ApiResponse(status, { error }))
|
||||||
|
|
||||||
|
|
||||||
const { id = null } = req.params;
|
const { id = null } = req.params;
|
||||||
if (!id) return error(400, "Missing id in query")
|
if (!id) return error(400, "Missing id in query")
|
||||||
const member = new User().getId(id);
|
const member = new User().getId(id);
|
||||||
if (!member || member.deleted) return error(404, "We have not got any user declared as this 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));
|
res.status(200).json(new ApiResponse(200, member));
|
||||||
}
|
|
||||||
else
|
|
||||||
return error(400, "Missing/undefined param in action section: " + action);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.all("*", (req, res) => res.status(400).json(new ApiResponse(400, { error: "Bad request" })));
|
||||||
|
|
||||||
module.exports = app;
|
module.exports = app;
|
Loading…
Reference in a new issue