p5.js-web-editor/server/controllers/project.controller/createProject.js
Andrew Nicolaou d2cad7438e
Test create project server code (prep for #1095)
* upgrades jest to latest version

* tests Project model with mockingoose

* extracts createProject controller to own file

* tests createProject controller with sinon-mongoose
2019-06-12 11:27:28 +02:00

32 lines
668 B
JavaScript

import Project from '../../models/project';
export default function createProject(req, res) {
let projectValues = {
user: req.user._id
};
projectValues = Object.assign(projectValues, req.body);
function sendFailure() {
res.json({ success: false });
}
function populateUserData(newProject) {
return Project.populate(
newProject,
{ path: 'user', select: 'username' },
(err, newProjectWithUser) => {
if (err) {
sendFailure();
return;
}
res.json(newProjectWithUser);
}
);
}
return Project.create(projectValues)
.then(populateUserData)
.catch(sendFailure);
}