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