Search now has limits and pages!

This commit is contained in:
Akif9748 2022-09-09 18:27:41 +03:00
parent feb64e2a21
commit 3c153ae64f
4 changed files with 50 additions and 21 deletions

View file

@ -37,6 +37,7 @@ But in front end, the API will works with session.
- POST `/api/messages` for create message. - POST `/api/messages` for create message.
**use ?limit&skip for skip and limit**
- GET `/api/search/users?q=query` find users. - GET `/api/search/users?q=query` find users.
- GET `/api/search/threads?q=query&authorID=not_required` find threads. - GET `/api/search/threads?q=query&authorID=not_required` find threads.
- GET `/api/search/messages?q=query&authorID=not_required` find messages. - GET `/api/search/messages?q=query&authorID=not_required` find messages.

View file

@ -32,14 +32,13 @@ Akf-forum has got an API for AJAX (fetch), other clients etc. And, you can learn
## TO-DO list ## TO-DO list
| To do | Is done? | Priority | | To do | Is done? | Priority |
| ----- | -------- | -------- | | ----- | -------- | -------- |
| Category | 🟡 | MEDIUM | | Category | 🟢 | MEDIUM |
| Page support for search | 🟡 | LOW | | Page support for search | 🟢 | LOW |
| IPs of users will add SecretModel | 🔴 | MEDIUM | | Profile Message | 🟡 | LOW |
| Profile Message | 🔴 | LOW | | Last seen, last seen info | 🟢 | LOW |
| Last seen, last seen info | 🔴 | LOW | | IPs of users will add SecretModel | ⚪ | MEDIUM |
| Better Auth | 🔴 | MEDIUM | | Better Auth | ⚪ | MEDIUM |
- Fix footer, theme - Fix footer, theme, sonra
- Navbar manuel select
- Version info to footer - Version info to footer
- upload other photos, model for it - upload other photos, model for it

View file

@ -2,26 +2,36 @@ const { Router } = require("express")
const { MessageModel, ThreadModel, UserModel } = require("../../../models"); const { MessageModel, ThreadModel, UserModel } = require("../../../models");
const app = Router(); const app = Router();
app.use((req, res, next) => {
req.sq = {}
req.so = {}
const limit = Number(req.query.limit);
const skip = Number(req.query.skip);
if (!req.user.admin) req.sq.deleted = false;
if (limit) req.so.limit = limit;
if (skip) req.so.skip = skip;
next();
})
app.get("/users", async (req, res) => { app.get("/users", async (req, res) => {
if (!req.query.q) return res.error(400, "Missing query parameter 'q' in request body."); if (!req.query.q) return res.error(400, "Missing query parameter 'q' in request body.");
const results = await UserModel.find({ name: { $regex: req.query.q, $options: "i" } }).limit(10); const results = await UserModel.find({ ...req.sq, name: { $regex: req.query.q, $options: "i" } }, null, req.so);
res.complate(results); res.complate(results);
}); });
app.get("/messages", async (req, res) => { app.get("/messages", async (req, res) => {
if (!Object.values(req.query).length) return res.error(400, "Missing query parameters in request body."); if (!Object.values(req.query).length) return res.error(400, "Missing query parameters in request body.");
const query = {}; const query = { ...req.sq };
if (req.query.q) query.content = { $regex: req.query.q, $options: "i" }; if (req.query.q) query.content = { $regex: req.query.q, $options: "i" };
if (req.query.authorID) query.authorID = req.query.authorID; if (req.query.authorID) query.authorID = req.query.authorID;
const results = await MessageModel.find(query).limit(10); const results = await MessageModel.find(query, null, req.so)
res.complate(results); res.complate(results);
}); });
app.get("/threads", async (req, res) => { app.get("/threads", async (req, res) => {
if (!Object.values(req.query).length) return res.error(400, "Missing query parameters in request body."); if (!Object.values(req.query).length) return res.error(400, "Missing query parameters in request body.");
const query = {}; const query = { ...req.sq };
if (req.query.q) query.title = { $regex: req.query.q, $options: "i" }; if (req.query.q) query.title = { $regex: req.query.q, $options: "i" };
if (req.query.authorID) query.authorID = req.query.authorID; if (req.query.authorID) query.authorID = req.query.authorID;
const results = await ThreadModel.find(query).limit(10); const results = await ThreadModel.find(query, null, req.so)
res.complate(results); res.complate(results);
}); });

View file

@ -5,28 +5,47 @@ const app = Router();
app.get("/", (req, res) => res.reply("search")); app.get("/", (req, res) => res.reply("search"));
app.use(async (req, res, next) => {
req.sq = {}
req.page = Number(req.query.page) || 0;
req.so = { limit: 10, skip: req.page * 10 }
if (!req.user?.admin) req.sq.deleted = false;
next();
});
app.get("/users", async (req, res) => { app.get("/users", async (req, res) => {
if (!req.query.q) return res.error(400, "Missing query parameter 'q' in request body."); if (!req.query.q) return res.error(400, "Missing query parameter 'q' in request body.");
const users = await UserModel.find({ name: { $regex: req.query.q, $options: "i" } }).limit(10); const users = await UserModel.find({ ...req.sq, name: { $regex: req.query.q, $options: "i" } }, null, req.so)
res.reply("users", { users, page: null }); res.reply("users", {
users, page: req.page,
pages: Math.ceil(await UserModel.count(req.sq) / 10)
});
}); });
app.get("/messages", async (req, res) => { app.get("/messages", async (req, res) => {
if (!Object.values(req.query).length) return res.error(400, "Missing query parameters in request body."); if (!Object.values(req.query).length) return res.error(400, "Missing query parameters in request body.");
const query = {}; const query = { ...req.sq };
if (req.query.q) query.content = { $regex: req.query.q, $options: "i" }; if (req.query.q) query.content = { $regex: req.query.q, $options: "i" };
if (req.query.authorID) query.authorID = req.query.authorID; if (req.query.authorID) query.authorID = req.query.authorID;
const messages = await MessageModel.find(query).limit(10); const messages = await MessageModel.find(query, null, req.so)
res.reply("messages",{messages}); res.reply("messages", {
messages, page: req.page,
pages: Math.ceil(await MessageModel.count(query) / 10)
});
}); });
app.get("/threads", async (req, res) => { app.get("/threads", async (req, res) => {
if (!Object.values(req.query).length) return res.error(400, "Missing query parameters in request body."); if (!Object.values(req.query).length) return res.error(400, "Missing query parameters in request body.");
const query = {}; const query = { ...req.sq };
if (req.query.q) query.title = { $regex: req.query.q, $options: "i" }; if (req.query.q) query.title = { $regex: req.query.q, $options: "i" };
if (req.query.authorID) query.authorID = req.query.authorID; if (req.query.authorID) query.authorID = req.query.authorID;
const threads = await ThreadModel.find(query).limit(10); const threads = await ThreadModel.find(query, null, req.so)
res.reply("threads", { threads, page: null }); res.reply("threads", {
threads,
page: null, page: req.page,
pages: Math.ceil(await ThreadModel.count(query) / 10)
});
}); });
module.exports = app; module.exports = app;