2016-06-24 00:29:55 +02:00
|
|
|
import Project from '../models/project';
|
2016-06-17 20:11:52 +02:00
|
|
|
|
|
|
|
export function createProject(req, res) {
|
2016-07-15 19:36:33 +02:00
|
|
|
let projectValues = {
|
2016-07-08 20:57:22 +02:00
|
|
|
user: req.user ? req.user._id : undefined // eslint-disable-line no-underscore-dangle
|
2016-06-24 00:29:55 +02:00
|
|
|
};
|
2016-06-20 22:29:08 +02:00
|
|
|
|
2016-07-15 19:36:33 +02:00
|
|
|
projectValues = Object.assign(projectValues, req.body);
|
2016-06-24 00:29:55 +02:00
|
|
|
|
|
|
|
Project.create(projectValues, (err, newProject) => {
|
|
|
|
if (err) { return res.json({ success: false }); }
|
2016-07-15 17:54:47 +02:00
|
|
|
Project.populate(newProject,
|
|
|
|
{path: 'user', select: 'username'},
|
|
|
|
(innerErr, newProjectWithUser) => {
|
|
|
|
if (innerErr) { return res.json({ success: false }); }
|
|
|
|
return res.json(newProjectWithUser);
|
|
|
|
});
|
2016-06-24 00:29:55 +02:00
|
|
|
});
|
2016-06-19 00:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function updateProject(req, res) {
|
2016-06-29 01:35:56 +02:00
|
|
|
Project.findByIdAndUpdate(req.params.project_id,
|
2016-06-24 00:29:55 +02:00
|
|
|
{
|
|
|
|
$set: req.body
|
2016-07-15 17:54:47 +02:00
|
|
|
})
|
|
|
|
.populate('user', 'username')
|
|
|
|
.exec((err, updatedProject) => {
|
2016-06-24 00:29:55 +02:00
|
|
|
if (err) { return res.json({ success: false }); }
|
2016-07-07 19:50:52 +02:00
|
|
|
return res.json(updatedProject);
|
2016-06-24 00:29:55 +02:00
|
|
|
});
|
2016-06-19 00:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getProject(req, res) {
|
2016-07-15 17:54:47 +02:00
|
|
|
Project.findById(req.params.project_id)
|
|
|
|
.populate('user', 'username')
|
|
|
|
.exec((err, project) => {
|
|
|
|
if (err) {
|
|
|
|
return res.status(404).send({ message: 'Project with that id does not exist' });
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json(project);
|
|
|
|
});
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
2016-07-01 17:30:40 +02:00
|
|
|
|
|
|
|
export function getProjects(req, res) {
|
|
|
|
if (req.user) {
|
|
|
|
Project.find({user: req.user._id}) // eslint-disable-line no-underscore-dangle
|
|
|
|
.sort('-createdAt')
|
2016-07-15 17:54:47 +02:00
|
|
|
.select('name files _id createdAt updatedAt')
|
2016-07-01 17:30:40 +02:00
|
|
|
.exec((err, projects) => {
|
|
|
|
res.json(projects);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// could just move this to client side
|
|
|
|
return res.json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|