tuned some things and added some to user route

This commit is contained in:
2024-01-24 16:28:02 +01:00
parent 49cd0b1953
commit 421ab338fc
8 changed files with 500 additions and 46 deletions
+3 -2
View File
@@ -4,7 +4,7 @@ const router = require('express').Router();
router.post('/', async (req, res) => {
try{
const username = req.user;
const {title, content} = req.body;
const {title, content, category} = req.body;
if(title.length > process.env.MAXTITLELENGTH) return res.status(400).json({error: "Title is too long"});
if(content.length > process.env.MAXCONTENTLENGTH) return res.status(400).json({error: "Content is too long"});
@@ -12,13 +12,14 @@ router.post('/', async (req, res) => {
if(title.length < process.env.MINTITLELENGTH) return res.status(400).json({error: "Title is too short"});
if(content.length < process.env.MINCONTENTLENGTH) return res.status(400).json({error: "Content is too short"});
if(!username) return res.status(400).json({error: "Missing username"});
if(!category) return res.status(400).json({error: "Missing category"});
const currentDate = new Date();
const formattedDateAndTime = currentDate.toLocaleTimeString('en-gb', { timeStyle: 'short' });
const formattedDate = currentDate.toLocaleDateString('en-gb');
const date = formattedDateAndTime + " " + formattedDate;
const newPost = await db.create('posts', {title: title, content: content, author: username, date: date});
const newPost = await db.create('posts', {category: category, title: title, content: content, author: username, date: date});
postId = (newPost[0].id).slice(6);
const update = await db.query(`UPDATE users SET posts += "${postId}" WHERE username = string::lowercase("${username}")`)