Added game creation and deletion
This commit is contained in:
2
index.js
2
index.js
@@ -10,6 +10,7 @@ import { initAuth } from './src/auth.js';
|
||||
import { initDbConnection, db } from './src/db.js';
|
||||
import { initUsers } from './src/user.js';
|
||||
import { initCdn } from './src/cdn.js';
|
||||
import { initGames } from './src/games.js';
|
||||
const app = express();
|
||||
const appWs = expressWs(app);
|
||||
const port = 12345;
|
||||
@@ -25,6 +26,7 @@ initAuth(app, db);
|
||||
initUsers(app, db);
|
||||
initWebsocket(app);
|
||||
initCdn(app, db);
|
||||
initGames(app, db);
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Listening on port ${port}`);
|
||||
|
||||
113
src/games.js
Normal file
113
src/games.js
Normal file
@@ -0,0 +1,113 @@
|
||||
import { Collection, Db, ObjectId } from 'mongodb';
|
||||
import { checkStringProp } from './util.js';
|
||||
|
||||
/**
|
||||
* @type {Collection}
|
||||
*/
|
||||
let games;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} app
|
||||
* @param {Db} db
|
||||
*/
|
||||
export function initGames(app, db) {
|
||||
games = db.collection('games');
|
||||
app.get('/game', fetchGame);
|
||||
app.post('/game', createGame);
|
||||
app.delete('/game/:gameid', deleteGame);
|
||||
app.get('/games', fetchGames);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async function createGame(req, res) {
|
||||
if (!checkStringProp(req.body, 'name')) {
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
const name = req.body.name;
|
||||
|
||||
games
|
||||
.insertOne({
|
||||
name,
|
||||
walls: [],
|
||||
owner: req.user._id,
|
||||
})
|
||||
.then(() => {
|
||||
res.sendStatus(200);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
res.sendStatus(500);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async function fetchGames(req, res) {
|
||||
let list = games.find({
|
||||
owner: req.user._id,
|
||||
});
|
||||
|
||||
res.status(200).send(await list.toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async function fetchGame(req, res) {
|
||||
if (req.query.id === undefined || req.query.id.length <= 0) {
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
const id = new ObjectId(req.query.id);
|
||||
|
||||
let game = await games.findOne({
|
||||
_id: id,
|
||||
owner: req.user._id,
|
||||
});
|
||||
|
||||
if (game) {
|
||||
res.status(200).send(game);
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @param {import('express').Response} res
|
||||
*/
|
||||
async function deleteGame(req, res) {
|
||||
let game = await games.findOne({
|
||||
owner: req.user._id,
|
||||
_id: new ObjectId(req.params.gameid),
|
||||
});
|
||||
|
||||
if (!game) {
|
||||
res.sendStatus(404);
|
||||
return;
|
||||
}
|
||||
|
||||
games
|
||||
.deleteOne({ _id: game._id })
|
||||
.then(() => {
|
||||
res.sendStatus(200);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
res.sendStatus(500);
|
||||
});
|
||||
}
|
||||
13
src/util.js
Normal file
13
src/util.js
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
*
|
||||
* @param {any} body
|
||||
* @param {string} property
|
||||
*/
|
||||
export function checkStringProp(body, property) {
|
||||
if (body === undefined) return false;
|
||||
if (Object.hasOwn(body, property)) {
|
||||
if (typeof body[property] === 'string') {
|
||||
return body[property].length > 0;
|
||||
} else return false;
|
||||
} else return false;
|
||||
}
|
||||
Reference in New Issue
Block a user