c9551a3142
- List any user's collections - Create new collection - Modify collection metadata - Delete collection - Add/remove any project to/from a collection
47 lines
1 KiB
JavaScript
47 lines
1 KiB
JavaScript
import Collection from '../../models/collection';
|
|
|
|
export default function createCollection(req, res) {
|
|
const owner = req.user._id;
|
|
const { name, description, slug } = req.body;
|
|
|
|
const values = {
|
|
owner,
|
|
name,
|
|
description,
|
|
slug
|
|
};
|
|
|
|
function sendFailure({ code = 500, message = 'Something went wrong' }) {
|
|
res.status(code).json({ success: false, message });
|
|
}
|
|
|
|
function sendSuccess(newCollection) {
|
|
res.json(newCollection);
|
|
}
|
|
|
|
function populateReferences(newCollection) {
|
|
return Collection.populate(
|
|
newCollection,
|
|
[
|
|
{ path: 'owner', select: ['id', 'username'] },
|
|
{
|
|
path: 'items.project',
|
|
select: ['id', 'name', 'slug'],
|
|
populate: {
|
|
path: 'user', select: ['username']
|
|
}
|
|
}
|
|
]
|
|
);
|
|
}
|
|
|
|
if (owner == null) {
|
|
sendFailure({ code: 404, message: 'No user specified' });
|
|
return null;
|
|
}
|
|
|
|
return Collection.create(values)
|
|
.then(populateReferences)
|
|
.then(sendSuccess)
|
|
.catch(sendFailure);
|
|
}
|