Untitled
unknown
plain_text
3 years ago
2.5 kB
3
Indexable
const express = require("express");
const { setProperties, getUserProp, emptyProps, getUserById } = require("../functions/users");
const { getGame } = require('../functions/game');
const user = express.Router();
// Return the info of the user.
user.get("/:userID", async (req, res) => {
try {
const userID = req.params.userID;
const result = await getUserProp(userID);
if (result.length <= 0) {
throw new Error("no_properties")
}
let games = {};
for (let i = 0; i < result.length; i++) {
let gameName = result[i].gameName;
let property = result[i].property;
try {
property = JSON.parse(property);
} catch (e) {
}
if (games[gameName] === undefined) {
games[gameName] = { "gameName": gameName, "property": [property] };
} else {
games[gameName].property.push(property);
}
}
let all = [];
for (let gameName in games) {
let game = games[gameName];
all.push(game);
}
const user = result.map(u => {
delete u.password;
delete u.creationDate;
delete u.gameID;
delete u.gameName;
delete u.property;
return u;
})
res.status(200).send({ status: true, user: user[0], games: all })
}
catch (err) {
if (err.message === "no_properties") {
const userID = req.params.userID;
const user = await getUserById(userID);
const result = {
username: user.username,
userID: user.userID,
email: user.email,
userDescription: user.userDescription
}
res.status(500).send({ status: false, user: result })
} else {
console.log(err)
}
}
}
);
// Update the properties of the user.
user.post("/:userID", async (req, res) => {
try {
const userID = req.params.userID;
const newProps = req.body.properties;
const gameID = req.body.gameID;
const userDB = await getUserById(userID);
/* const gameDB = await getGame("gameName", gameName); */
if (newProps.length > 0) {
const set = await setProperties(userDB.userID, gameID, newProps)
} else if (newProps.length === 0) {
const set2 = await emptyProps(userDB.userID, gameID)
}
res.status(200).send({ status: true });
}
catch (err) {
console.log(err)
res.status(500).send({ status: false });
}
});
module.exports = user;
Editor is loading...