From 07a4b6ea76e7eac94f9e435d22279912952d4e4c Mon Sep 17 00:00:00 2001 From: Linda Paiste Date: Wed, 14 Feb 2024 21:10:51 -0600 Subject: [PATCH 1/2] Convert project.controller to async/await syntax. --- server/controllers/project.controller.js | 216 ++++++++++------------- 1 file changed, 95 insertions(+), 121 deletions(-) diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index 817384f82d..bf355262bd 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -20,26 +20,27 @@ export { apiGetProjectsForUser } from './project.controller/getProjectsForUser'; -export function updateProject(req, res) { - Project.findById(req.params.project_id, (findProjectErr, project) => { - if (!project.user.equals(req.user._id)) { - res.status(403).send({ - success: false, - message: 'Session does not match owner of project.' - }); - return; - } - if ( - req.body.updatedAt && - isAfter(new Date(project.updatedAt), new Date(req.body.updatedAt)) - ) { - res.status(409).send({ - success: false, - message: 'Attempted to save stale version of project.' - }); - return; - } - Project.findByIdAndUpdate( +export async function updateProject(req, res) { + const project = await Project.findById(req.params.project_id).exec(); + if (!project.user.equals(req.user._id)) { + res.status(403).send({ + success: false, + message: 'Session does not match owner of project.' + }); + return; + } + if ( + req.body.updatedAt && + isAfter(new Date(project.updatedAt), new Date(req.body.updatedAt)) + ) { + res.status(409).send({ + success: false, + message: 'Attempted to save stale version of project.' + }); + return; + } + try { + const updatedProject = await Project.findByIdAndUpdate( req.params.project_id, { $set: req.body @@ -50,119 +51,90 @@ export function updateProject(req, res) { } ) .populate('user', 'username') - .exec((updateProjectErr, updatedProject) => { - if (updateProjectErr) { - console.log(updateProjectErr); - res.status(400).json({ success: false }); - return; - } - if ( - req.body.files && - updatedProject.files.length !== req.body.files.length - ) { - const oldFileIds = updatedProject.files.map((file) => file.id); - const newFileIds = req.body.files.map((file) => file.id); - const staleIds = oldFileIds.filter( - (id) => newFileIds.indexOf(id) === -1 - ); - staleIds.forEach((staleId) => { - updatedProject.files.id(staleId).remove(); - }); - updatedProject.save((innerErr, savedProject) => { - if (innerErr) { - console.log(innerErr); - res.status(400).json({ success: false }); - return; - } - res.json(savedProject); - }); - } else { - res.json(updatedProject); - } + .exec(); + if ( + req.body.files && + updatedProject.files.length !== req.body.files.length + ) { + const oldFileIds = updatedProject.files.map((file) => file.id); + const newFileIds = req.body.files.map((file) => file.id); + const staleIds = oldFileIds.filter((id) => newFileIds.indexOf(id) === -1); + staleIds.forEach((staleId) => { + updatedProject.files.id(staleId).remove(); }); - }); + const savedProject = await updatedProject.save(); + res.json(savedProject); + } else { + res.json(updatedProject); + } + } catch (error) { + res.status(400).json({ success: false }); + } } -export function getProject(req, res) { +export async function getProject(req, res) { const { project_id: projectId, username } = req.params; - User.findByUsername(username, (err, user) => { // eslint-disable-line - if (!user) { - return res - .status(404) - .send({ message: 'Project with that username does not exist' }); - } - Project.findOne({ - user: user._id, - $or: [{ _id: projectId }, { slug: projectId }] - }) - .populate('user', 'username') - .exec((err, project) => { // eslint-disable-line - if (err) { - console.log(err); - return res - .status(404) - .send({ message: 'Project with that id does not exist' }); - } - return res.json(project); - }); - }); + const user = await User.findByUsername(username); + if (!user) { + return res + .status(404) + .send({ message: 'User with that username does not exist' }); + } + const project = await Project.findOne({ + user: user._id, + $or: [{ _id: projectId }, { slug: projectId }] + }).populate('user', 'username'); + if (!project) { + return res + .status(404) + .send({ message: 'Project with that id does not exist' }); + } + return res.json(project); } export function getProjectsForUserId(userId) { - return new Promise((resolve, reject) => { - Project.find({ user: userId }) - .sort('-createdAt') - .select('name files id createdAt updatedAt') - .exec((err, projects) => { - if (err) { - console.log(err); - } - resolve(projects); - }); - }); + return Project.find({ user: userId }) + .sort('-createdAt') + .select('name files id createdAt updatedAt') + .exec(); } -export function getProjectAsset(req, res) { +export async function getProjectAsset(req, res) { const projectId = req.params.project_id; - Project.findOne({ $or: [{ _id: projectId }, { slug: projectId }] }) + const project = await Project.findOne({ + $or: [{ _id: projectId }, { slug: projectId }] + }) .populate('user', 'username') - .exec(async (err, project) => { // eslint-disable-line - if (err) { - return res - .status(404) - .send({ message: 'Project with that id does not exist' }); - } - if (!project) { - return res - .status(404) - .send({ message: 'Project with that id does not exist' }); - } + .exec(); + if (!project) { + return res + .status(404) + .send({ message: 'Project with that id does not exist' }); + } - const filePath = req.params[0]; - const resolvedFile = resolvePathToFile(filePath, project.files); - if (!resolvedFile) { - return res.status(404).send({ message: 'Asset does not exist' }); - } - if (!resolvedFile.url) { - return res.send(resolvedFile.content); - } + const filePath = req.params[0]; + const resolvedFile = resolvePathToFile(filePath, project.files); + if (!resolvedFile) { + return res.status(404).send({ message: 'Asset does not exist' }); + } + if (!resolvedFile.url) { + return res.send(resolvedFile.content); + } - try { - const { data } = await axios.get(resolvedFile.url, { - responseType: 'arraybuffer' - }); - res.send(data); - } catch (error) { - res.status(404).send({ message: 'Asset does not exist' }); - } + try { + const { data } = await axios.get(resolvedFile.url, { + responseType: 'arraybuffer' }); + return res.send(data); + } catch (error) { + return res.status(404).send({ message: 'Asset does not exist' }); + } } -export function getProjects(req, res) { +export async function getProjects(req, res) { if (req.user) { - getProjectsForUserId(req.user._id).then((projects) => { - res.json(projects); - }); + const projects = await getProjectsForUserId(req.user._id); + res.json(projects); } else { // could just move this to client side res.json([]); @@ -264,9 +236,11 @@ async function buildZip(project, req, res) { } } -export function downloadProjectAsZip(req, res) { - Project.findById(req.params.project_id, (err, project) => { - // save project to some path - buildZip(project, req, res); - }); +export async function downloadProjectAsZip(req, res) { + const project = await Project.findById(req.params.project_id); + if (!project) { + res.status(404).send({ message: 'Project with that id does not exist' }); + } + // save project to some path + buildZip(project, req, res); } From 36d1ab04b649f04673a118c313812666ca3177f8 Mon Sep 17 00:00:00 2001 From: Linda Paiste Date: Sat, 17 Feb 2024 15:34:28 -0600 Subject: [PATCH 2/2] Adjustments to error handling, additional comments. --- server/controllers/project.controller.js | 66 ++++++++++++++++-------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index bf355262bd..86c340ea40 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -21,25 +21,32 @@ export { } from './project.controller/getProjectsForUser'; export async function updateProject(req, res) { - const project = await Project.findById(req.params.project_id).exec(); - if (!project.user.equals(req.user._id)) { - res.status(403).send({ - success: false, - message: 'Session does not match owner of project.' - }); - return; - } - if ( - req.body.updatedAt && - isAfter(new Date(project.updatedAt), new Date(req.body.updatedAt)) - ) { - res.status(409).send({ - success: false, - message: 'Attempted to save stale version of project.' - }); - return; - } try { + const project = await Project.findById(req.params.project_id).exec(); + if (!project) { + res.status(404).send({ + success: false, + message: 'Project with that id does not exist.' + }); + return; + } + if (!project.user.equals(req.user._id)) { + res.status(403).send({ + success: false, + message: 'Session does not match owner of project.' + }); + return; + } + if ( + req.body.updatedAt && + isAfter(new Date(project.updatedAt), new Date(req.body.updatedAt)) + ) { + res.status(409).send({ + success: false, + message: 'Attempted to save stale version of project.' + }); + return; + } const updatedProject = await Project.findByIdAndUpdate( req.params.project_id, { @@ -68,7 +75,8 @@ export async function updateProject(req, res) { res.json(updatedProject); } } catch (error) { - res.status(400).json({ success: false }); + console.error(error); + res.status(500).json({ success: false }); } } @@ -152,7 +160,7 @@ export async function projectExists(projectId) { /** * @param {string} username - * @param {string} projectId + * @param {string} projectId - the database id or the slug or the project * @return {Promise} */ export async function projectForUserExists(username, projectId) { @@ -165,12 +173,18 @@ export async function projectForUserExists(username, projectId) { return project != null; } +/** + * Adds URLs referenced in