From 832ff99a7bfe89f249ade6795c35e30f73f370e3 Mon Sep 17 00:00:00 2001 From: Jonas Kappa Date: Fri, 2 Jan 2026 12:35:59 +0100 Subject: [PATCH] Added rename of Game, Wall and Category --- src/games.js | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/src/games.js b/src/games.js index 1e2aad8..c3a207d 100644 --- a/src/games.js +++ b/src/games.js @@ -83,13 +83,16 @@ export function initGames(app, db) { app.post('/game', createGame); app.delete('/game/:gameid', deleteGameRoute); app.get('/games', fetchGames); + app.post('/game/rename', renameGame); app.get('/wall', fetchWall); app.get('/walls/:gameid', fetchWalls); app.post('/wall', createWall); app.delete('/wall/:wallid', deleteWallRoute); + app.post('/wall/rename', renameWall); app.get('/category', fetchCategory); + app.post('/category/rename', renameCategory); app.get('/question', fetchQuestion); } @@ -122,6 +125,54 @@ async function createGame(req, res) { }); } +/** + * + * @param {import('express').Request} req + * @param {import('express').Response} res + */ +function renameGame(req, res) { + if (!checkStringProp(req.body, 'name')) { + res.sendStatus(400); + return; + } + if (!checkStringProp(req.body, 'gameid')) { + res.sendStatus(400); + return; + } + /** + * @type {string} + */ + let gameid = req.body.gameid; + let _id = new ObjectId(gameid); + let name = req.body.name; + + cGames + .updateOne( + { + _id, + owner: req.user._id, + }, + { + $set: { + name, + }, + }, + ) + .then((result) => { + if (result.modifiedCount === 1) res.sendStatus(200); + else { + console.error( + `Failed to modify exactly one Document. Instead modified: ${result.modifiedCount}`, + ); + res.sendStatus(400); + } + }) + .catch((err) => { + console.error(err); + res.sendStatus(500); + }); +} + /** * * @param {import('express').Request} req @@ -260,6 +311,54 @@ async function fetchCategory(req, res) { } } +/** + * + * @param {import('express').Request} req + * @param {import('express').Response} res + */ +function renameCategory(req, res) { + if (!checkStringProp(req.body, 'name')) { + res.sendStatus(400); + return; + } + if (!checkStringProp(req.body, 'categoryid')) { + res.sendStatus(400); + return; + } + /** + * @type {string} + */ + let categoryid = req.body.categoryid; + let _id = new ObjectId(categoryid); + let name = req.body.name; + + cCategories + .updateOne( + { + _id, + owner: req.user._id, + }, + { + $set: { + name, + }, + }, + ) + .then((result) => { + if (result.modifiedCount === 1) res.sendStatus(200); + else { + console.error( + `Failed to modify exactly one Document. Instead modified: ${result.modifiedCount}`, + ); + res.sendStatus(400); + } + }) + .catch((err) => { + console.error(err); + res.sendStatus(500); + }); +} + /** * * @param {import('express').Request} req @@ -310,6 +409,54 @@ async function fetchWalls(req, res) { res.status(200).send(await fetchedWalls.toArray()); } +/** + * + * @param {import('express').Request} req + * @param {import('express').Response} res + */ +function renameWall(req, res) { + if (!checkStringProp(req.body, 'name')) { + res.sendStatus(400); + return; + } + if (!checkStringProp(req.body, 'wallid')) { + res.sendStatus(400); + return; + } + /** + * @type {string} + */ + let wallid = req.body.wallid; + let _id = new ObjectId(wallid); + let name = req.body.name; + + cWalls + .updateOne( + { + _id, + owner: req.user._id, + }, + { + $set: { + name, + }, + }, + ) + .then((result) => { + if (result.modifiedCount === 1) res.sendStatus(200); + else { + console.error( + `Failed to modify exactly one Document. Instead modified: ${result.modifiedCount}`, + ); + res.sendStatus(400); + } + }) + .catch((err) => { + console.error(err); + res.sendStatus(500); + }); +} + /** * * @param {import('express').Request} req