Updated routes to bypass duplicate ID errors

This commit is contained in:
Ceferino Patino 2022-08-01 22:56:13 -05:00
commit b1372c0ede
7 changed files with 37 additions and 24 deletions

View file

@ -8,9 +8,9 @@
"start": "npm start -w",
"start:server": "npm start -w server",
"start:client": "npm start -w client",
"start:prod": "npm run start:server",
"start:prod": "npm run prod -w server",
"prestart:prod": "npm run build",
"dev": "npm run dev -w server",
"dev": "npm start -workspaces",
"build": "npm run build -w client"
},
"repository": {

View file

@ -19,8 +19,8 @@
"superagent-throttle": "^1.0.1"
},
"scripts": {
"start": "pm2 start ./bin/www",
"dev": "nodemon ./bin/www"
"start": "nodemon ./bin/www",
"prod": "pm2 start ./bin/www"
},
"devDependencies": {
"jest": "^28.1.3",

View file

@ -8,27 +8,31 @@ async function GetUser(req, res) {
const cachedUser = await User.findOne({ username });
if (cachedUser) return res.send({
user: {
username: cachedUser.username,
email: cachedUser.email,
points: cachedUser.points,
level: getUserLevel(cachedUser.points),
}
});
if (cachedUser)
return res.send({
user: {
username: cachedUser.username,
email: cachedUser.email,
points: cachedUser.points,
level: getUserLevel(cachedUser.points),
},
});
const { success, user } = await createRequest('get', `/users/${username}`);
if (!success) return res.status(500).send(config.errorGeneric);
const newUser = await User.create({ ...user, id: user.user_id });
const newUser = await User.findByIdAndUpdate(user.user_id, user, {
upsert: true,
});
return res.send({
user: {
username: newUser.username,
email: newUser.email,
points: newUser.points,
level: getUserLevel(newUser.points),
}
},
});
}

View file

@ -4,8 +4,12 @@ const Answer = require('server/db/models/Answer');
async function getAnswer(question_id, answer_id) {
let cachedAnswer;
try { cachedAnswer = await Answer.findById(answer_id); }
catch { return; }
try {
cachedAnswer = await Answer.findById(answer_id);
} catch {
return;
}
// Retrieve uncached answer and patch to cache
if (!cachedAnswer) {
@ -14,7 +18,9 @@ async function getAnswer(question_id, answer_id) {
`/questions/${question_id}/answers/${answer_id}`
);
if (!success) return;
return Answer.create({ ...answer, id: answer.answer_id, question_id });
return Answer.findByIdAndUpdate(answer.answer_id, answer, {
upsert: true,
});
} else return cachedAnswer;
}

View file

@ -4,8 +4,11 @@ const Question = require('server/db/models/Question');
async function getQuestion(question_id) {
let cachedQuestion;
try { cachedQuestion = await Question.findById(question_id); }
catch { return; }
try {
cachedQuestion = await Question.findById(question_id);
} catch {
return;
}
// Retrieve uncached question and patch to cache
if (!cachedQuestion) {
@ -14,7 +17,9 @@ async function getQuestion(question_id) {
`/questions/${question_id}`
);
if (!success) return;
return Question.create({ ...question, id: question.question_id });
return Question.findByIdAndUpdate(question.question_id, question, {
upsert: true,
});
} else return cachedQuestion;
}

View file

@ -1,11 +1,9 @@
const getAnswer = require('./getAnswer');
const getComment = require('./getComment');
const getQuestion = require('./getQuestion');
const refreshQuestion = require('./refreshQuestion');
module.exports = {
getAnswer,
getComment,
getQuestion,
refreshQuestion
};
refreshQuestion,
};