Added category to API

This commit is contained in:
Akif9748 2022-09-09 17:13:37 +03:00
parent 03f52fed3b
commit 09ac68ee70
10 changed files with 107 additions and 29 deletions

View file

@ -16,19 +16,31 @@ But in front end, the API will works with session.
## How to request?
### 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/:id` fetch a ban.
- DELETE `/api/bans/:id` for unban 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.
- PATCH `/api/users/:id/` for edit user.
- PUT `/api/users/:id/` for add profile photo to user.
- GET `/api/categories/` fetch all categories.
- GET `/api/categories/:id` fetch a category
- 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.
- 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.
- POST `/api/threads` for create thread.
- 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/users/:id` for fetch user.
- DELETE `/api/users/:id/` for delete user.
- PATCH `/api/users/:id/` for edit user.
- PUT `/api/users/:id/` for add profile photo to user.
### Example request:
GET ```/api/messages/0```

View file

@ -41,6 +41,10 @@ Akf-forum has got an API for AJAX (fetch), other clients etc. And, you can learn
| Better Auth | 🔴 | MEDIUM |
- Fix footer, theme
- Navbar manuel select
- Version info to footer
- upload other photos, model for it
- black theme is broken
## Major Version History
- V4: Caching
- V3: New Theme

16
models/Category.js Normal file
View 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;

View file

@ -4,6 +4,7 @@ const MessageModel = require("./Message");
const schema = new mongoose.Schema({
id: { type: String, unique: true },
categoryID: String,
authorID: String,
author: Object,
@ -19,7 +20,9 @@ const schema = new mongoose.Schema({
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) {
const query = { threadID: this.id };
if (!admin) query.deleted = false;

View file

@ -1,7 +1,8 @@
const UserModel = require("./User"),
const CategoryModel = require("./Category"),
MessageModel = require("./Message"),
ThreadModel = require("./Thread"),
SecretModel = require("./Secret"),
UserModel = require("./User"),
BanModel = require("./Ban");
module.exports = { UserModel, MessageModel, ThreadModel, SecretModel, BanModel };
module.exports = { CategoryModel, MessageModel, ThreadModel, SecretModel, UserModel, BanModel };

View file

@ -4,7 +4,7 @@ const { Router } = require("express")
const app = Router();
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();
});

View 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;

View file

@ -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) => {

View file

@ -73,7 +73,6 @@ app.delete("/:id/", async (req, res) => {
if (thread.deleted) return res.error(403, "This thread is already deleted.");
thread.deleted = true;
await thread.save();
console.log(thread)
await MessageModel.updateMany({ threadID: thread.id }, { deleted: true });
res.complate(thread);

View file

@ -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.");
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;
member.edited = true;
await member.save();
res.complate(member);
res.complate(await member.save());
})
const storage = multer.diskStorage({