comments and replies work now
This commit is contained in:
parent
11147482d2
commit
9e5989825f
6 changed files with 102 additions and 10 deletions
46
routes/commenter.js
Normal file
46
routes/commenter.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
import { Router } from "express";
|
||||
import { apiStat } from "../lib.js";
|
||||
import { initDb } from "../db.js";
|
||||
import { randomBytes } from 'node:crypto';
|
||||
|
||||
let db = await initDb();
|
||||
|
||||
const router = Router();
|
||||
const minChar = 1;
|
||||
const maxChar = 32;
|
||||
|
||||
|
||||
router.post('/', async (req, res, next) => {
|
||||
let { username } = res.auth;
|
||||
let { post, type, id } = req.body;
|
||||
|
||||
if (!username || !post || !id || !type) {
|
||||
apiStat(res, next, 'Fields are missing.')
|
||||
return;
|
||||
}
|
||||
if (post.length < minChar || post.length > maxChar) {
|
||||
apiStat(res, next, `Post length isn't ${minChar} to ${maxChar} characters.`)
|
||||
return;
|
||||
}
|
||||
|
||||
let token = randomBytes(12).toString('hex');
|
||||
|
||||
await db.run('INSERT INTO comment (username, date, content, id) VALUES (?,?,?,?)', [
|
||||
username,
|
||||
(+new Date),
|
||||
post,
|
||||
token
|
||||
]);
|
||||
|
||||
await db.run('INSERT INTO feeder (parentType, parentId , childType , childId , sortId ) VALUES(?,?,?,?,?)', [
|
||||
type,
|
||||
id,
|
||||
'comment',
|
||||
token,
|
||||
(+new Date)
|
||||
]);
|
||||
|
||||
apiStat(res, next, `Comment submitted.`, '/comment/' + token)
|
||||
})
|
||||
|
||||
export default router;
|
Loading…
Add table
Add a link
Reference in a new issue