Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | 4x 4x 2x 3x 3x 1x 1x 2x 4x 6x 5x 5x 3x 2x 2x 7x 1x 1x 1x 1x 6x 6x 5x 2x 2x 1x 1x 1x 1x 1x 4x 4x | import Project from '../../models/project';
import { toModel, FileValidationError, ProjectValidationError } from '../../domain-objects/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);
}
// TODO: What happens if you don't supply any files?
export function apiCreateProject(req, res) {
const params = Object.assign({ user: req.user._id }, req.body);
function sendValidationErrors(err, type, code = 422) {
res.status(code).json({
message: `${type} Validation Failed`,
detail: err.message,
errors: err.files,
});
}
// TODO: Error handling to match spec
function sendFailure(err) {
res.status(500).end();
}
function handleErrors(err) {
if (err instanceof FileValidationError) {
sendValidationErrors(err, 'File', err.code);
} else Eif (err instanceof ProjectValidationError) {
sendValidationErrors(err, 'Sketch', err.code);
} else {
sendFailure();
}
}
function checkUserHasPermission() {
if (req.user.username !== req.params.username) {
console.log('no permission');
const error = new ProjectValidationError(`'${req.user.username}' does not have permission to create for '${req.params.username}'`);
error.code = 401;
throw error;
}
}
try {
checkUserHasPermission();
const model = toModel(params);
return model.isSlugUnique()
.then(({ isUnique, conflictingIds }) => {
if (isUnique) {
return model.save()
.then((newProject) => {
res.status(201).json({ id: newProject.id });
});
}
const error = new ProjectValidationError(`Slug "${model.slug}" is not unique. Check ${conflictingIds.join(', ')}`);
error.code = 409;
throw error;
})
.then(checkUserHasPermission)
.catch(handleErrors);
} catch (err) {
handleErrors(err);
return Promise.reject(err);
}
}
|