mirror of
https://github.com/Akif9748/akf-forum.git
synced 2024-11-25 21:10:40 +03:00
Added category to API
This commit is contained in:
parent
03f52fed3b
commit
09ac68ee70
10 changed files with 107 additions and 29 deletions
39
APIDOCS.md
39
APIDOCS.md
|
@ -16,19 +16,31 @@ But in front end, the API will works with session.
|
||||||
## How to request?
|
## How to request?
|
||||||
|
|
||||||
### Request types:
|
### Request types:
|
||||||
- GET `/api/search/users?q=query` find users.
|
|
||||||
- GET `/api/search/threads?q=query&authorID=not_required` find threads.
|
|
||||||
- GET `/api/search/messages?q=query&authorID=not_required` find messages.
|
|
||||||
|
|
||||||
- GET `/api/bans/` fetch all bans.
|
- GET `/api/bans/` fetch all bans.
|
||||||
- GET `/api/bans/:id` fetch a ban.
|
- GET `/api/bans/:id` fetch a ban.
|
||||||
- DELETE `/api/bans/:id` for unban an IP adress.
|
- DELETE `/api/bans/:id` for unban an IP adress.
|
||||||
- POST `/api/bans?reason=flood` for ban an IP adress.
|
- POST `/api/bans?reason=flood` for ban an IP adress.
|
||||||
|
|
||||||
- GET `/api/users/:id` for fetch user.
|
|
||||||
- DELETE `/api/users/:id/` for delete user.
|
- GET `/api/categories/` fetch all categories.
|
||||||
- PATCH `/api/users/:id/` for edit user.
|
- GET `/api/categories/:id` fetch a category
|
||||||
- PUT `/api/users/:id/` for add profile photo to user.
|
- PATCH `/api/categories/:id` for update a category.
|
||||||
|
- DELETE `/api/categories/:id` for delete a category.
|
||||||
|
- POST `/api/categories` for create a category.
|
||||||
|
|
||||||
|
|
||||||
|
- GET `/api/messages/:id` for fetch message.
|
||||||
|
- DELETE `/api/messages/:id/` for delete message.
|
||||||
|
- PATCH `/api/messages/:id/` for edit message.
|
||||||
|
- POST `/api/messages/:id/undelete` for undelete message.
|
||||||
|
- POST `/api/messages/:id/react/:type` for react to a message.
|
||||||
|
- POST `/api/messages` for create message.
|
||||||
|
|
||||||
|
|
||||||
|
- GET `/api/search/users?q=query` find users.
|
||||||
|
- GET `/api/search/threads?q=query&authorID=not_required` find threads.
|
||||||
|
- GET `/api/search/messages?q=query&authorID=not_required` find messages.
|
||||||
|
|
||||||
|
|
||||||
- GET `/api/threads/:id` for fetch thread.
|
- GET `/api/threads/:id` for fetch thread.
|
||||||
- DELETE `/api/threads/:id/` for delete thread.
|
- DELETE `/api/threads/:id/` for delete thread.
|
||||||
|
@ -37,12 +49,11 @@ But in front end, the API will works with session.
|
||||||
- GET `/api/threads/:id/messages?skip=0&limit=10` for fetch messages in thread.
|
- GET `/api/threads/:id/messages?skip=0&limit=10` for fetch messages in thread.
|
||||||
- POST `/api/threads` for create thread.
|
- POST `/api/threads` for create thread.
|
||||||
|
|
||||||
- GET `/api/messages/:id` for fetch message.
|
|
||||||
- DELETE `/api/messages/:id/` for delete message.
|
- GET `/api/users/:id` for fetch user.
|
||||||
- PATCH `/api/messages/:id/` for edit message.
|
- DELETE `/api/users/:id/` for delete user.
|
||||||
- POST `/api/messages/:id/undelete` for undelete message.
|
- PATCH `/api/users/:id/` for edit user.
|
||||||
- POST `/api/messages/:id/react/:type` for react to a message.
|
- PUT `/api/users/:id/` for add profile photo to user.
|
||||||
- POST `/api/messages` for create message.
|
|
||||||
|
|
||||||
### Example request:
|
### Example request:
|
||||||
GET ```/api/messages/0```
|
GET ```/api/messages/0```
|
||||||
|
|
|
@ -41,6 +41,10 @@ Akf-forum has got an API for AJAX (fetch), other clients etc. And, you can learn
|
||||||
| Better Auth | 🔴 | MEDIUM |
|
| Better Auth | 🔴 | MEDIUM |
|
||||||
- Fix footer, theme
|
- Fix footer, theme
|
||||||
- Navbar manuel select
|
- Navbar manuel select
|
||||||
|
- Version info to footer
|
||||||
|
- upload other photos, model for it
|
||||||
|
- black theme is broken
|
||||||
|
|
||||||
## Major Version History
|
## Major Version History
|
||||||
- V4: Caching
|
- V4: Caching
|
||||||
- V3: New Theme
|
- V3: New Theme
|
||||||
|
|
16
models/Category.js
Normal file
16
models/Category.js
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
const mongoose = require("mongoose")
|
||||||
|
|
||||||
|
const schema = new mongoose.Schema({
|
||||||
|
name: { type: String, unique: true },
|
||||||
|
desp: String, position: Number,
|
||||||
|
id: { type: String, unique: true },
|
||||||
|
authorID: { type: String }
|
||||||
|
|
||||||
|
});
|
||||||
|
schema.methods.takeId = async function () {
|
||||||
|
this.id = String(await model.count() || 0);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
const model = mongoose.model('category', schema);
|
||||||
|
|
||||||
|
module.exports = model;
|
|
@ -4,6 +4,7 @@ const MessageModel = require("./Message");
|
||||||
const schema = new mongoose.Schema({
|
const schema = new mongoose.Schema({
|
||||||
id: { type: String, unique: true },
|
id: { type: String, unique: true },
|
||||||
|
|
||||||
|
categoryID: String,
|
||||||
authorID: String,
|
authorID: String,
|
||||||
author: Object,
|
author: Object,
|
||||||
|
|
||||||
|
@ -19,7 +20,9 @@ const schema = new mongoose.Schema({
|
||||||
|
|
||||||
|
|
||||||
schema.methods.get_author = cache.getAuthor;
|
schema.methods.get_author = cache.getAuthor;
|
||||||
|
schema.methods.get_category = () => async function () {
|
||||||
|
return await require("./Category").findOne({ id: this.categoryID }) || {id: this.categoryID, name: "Unknown"} ;
|
||||||
|
}
|
||||||
schema.methods.messageCount = async function (admin = false) {
|
schema.methods.messageCount = async function (admin = false) {
|
||||||
const query = { threadID: this.id };
|
const query = { threadID: this.id };
|
||||||
if (!admin) query.deleted = false;
|
if (!admin) query.deleted = false;
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
const UserModel = require("./User"),
|
const CategoryModel = require("./Category"),
|
||||||
MessageModel = require("./Message"),
|
MessageModel = require("./Message"),
|
||||||
ThreadModel = require("./Thread"),
|
ThreadModel = require("./Thread"),
|
||||||
SecretModel = require("./Secret"),
|
SecretModel = require("./Secret"),
|
||||||
BanModel = require("./Ban");
|
UserModel = require("./User"),
|
||||||
|
BanModel = require("./Ban");
|
||||||
|
|
||||||
module.exports = { UserModel, MessageModel, ThreadModel, SecretModel, BanModel };
|
module.exports = { CategoryModel, MessageModel, ThreadModel, SecretModel, UserModel, BanModel };
|
|
@ -4,7 +4,7 @@ const { Router } = require("express")
|
||||||
const app = Router();
|
const app = Router();
|
||||||
|
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
if (!req.user || !req.user.admin) return res.error(403, "You have not got permission for this.");
|
if (!req.user.admin) return res.error(403, "You have not got permission for this.");
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
49
routes/api/routes/categories.js
Normal file
49
routes/api/routes/categories.js
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
const { CategoryModel } = require("../../../models");
|
||||||
|
const { Router } = require("express")
|
||||||
|
|
||||||
|
const app = Router();
|
||||||
|
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
if (!req.user.admin) return res.error(403, "You have not got permission for this.");
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
app.param("id", async (req, res, next, id) => {
|
||||||
|
req.category = await CategoryModel.findOne({ id });
|
||||||
|
if (!req.category) return res.error(404, `We don't have any category with id ${id}.`);
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/", async (req, res) => {
|
||||||
|
const categories = await CategoryModel.find({});
|
||||||
|
res.complate(categories);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/:id", async (req, res) => {
|
||||||
|
const {category} = req;
|
||||||
|
res.complate(category);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.patch("/:id", async (req, res) => {
|
||||||
|
const {category} = req;
|
||||||
|
if (req.body.name) category.name = req.body.name;
|
||||||
|
if (req.body.desp) category.name = req.body.name;
|
||||||
|
res.complate(await category.save());
|
||||||
|
});
|
||||||
|
|
||||||
|
app.delete("/:id", async (req, res) => {
|
||||||
|
res.complate(await CategoryModel.deleteOne({ id: req.params.id }));
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post("/", async (req, res) => {
|
||||||
|
const {name,desp} = req.body;
|
||||||
|
if (!name) return res.error(400, "You have to give a name for the category.");
|
||||||
|
|
||||||
|
if (await CategoryModel.exists({ name })) return res.error(400, "This category is already opened.");
|
||||||
|
|
||||||
|
res.complate(await CategoryModel.create({ name, desp, authorID: req.user.id }).then(c => c.takeId()));
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = app;
|
|
@ -17,11 +17,8 @@ app.param("id", async (req, res, next, id) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
app.get("/:id", async (req, res) => {
|
app.get("/:id", async (req, res) => res.complate(req.message));
|
||||||
|
|
||||||
res.complate(message);
|
|
||||||
|
|
||||||
})
|
|
||||||
app.patch("/:id/", async (req, res) => {
|
app.patch("/:id/", async (req, res) => {
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -73,7 +73,6 @@ app.delete("/:id/", async (req, res) => {
|
||||||
if (thread.deleted) return res.error(403, "This thread is already deleted.");
|
if (thread.deleted) return res.error(403, "This thread is already deleted.");
|
||||||
thread.deleted = true;
|
thread.deleted = true;
|
||||||
await thread.save();
|
await thread.save();
|
||||||
console.log(thread)
|
|
||||||
|
|
||||||
await MessageModel.updateMany({ threadID: thread.id }, { deleted: true });
|
await MessageModel.updateMany({ threadID: thread.id }, { deleted: true });
|
||||||
res.complate(thread);
|
res.complate(thread);
|
||||||
|
|
|
@ -43,9 +43,9 @@ app.post("/:id/undelete/", async (req, res) => {
|
||||||
if (!member.deleted) return res.error(404, "This user is not deleted, first, delete it.");
|
if (!member.deleted) return res.error(404, "This user is not deleted, first, delete it.");
|
||||||
|
|
||||||
member.deleted = false;
|
member.deleted = false;
|
||||||
await member.save();
|
;
|
||||||
|
|
||||||
res.complate(member);
|
res.complate(await member.save());
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -74,9 +74,7 @@ app.patch("/:id/", async (req, res) => {
|
||||||
if (deleted === false) member.deleted = false;
|
if (deleted === false) member.deleted = false;
|
||||||
member.edited = true;
|
member.edited = true;
|
||||||
|
|
||||||
await member.save();
|
res.complate(await member.save());
|
||||||
|
|
||||||
res.complate(member);
|
|
||||||
|
|
||||||
})
|
})
|
||||||
const storage = multer.diskStorage({
|
const storage = multer.diskStorage({
|
||||||
|
|
Loading…
Reference in a new issue