Skip to content

Commit 4f06523

Browse files
committed
update apiCreateProject to async/await
1 parent 974b06f commit 4f06523

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

server/controllers/project.controller/createProject.js

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,33 @@ export default function createProject(req, res) {
3131
}
3232

3333
// TODO: What happens if you don't supply any files?
34-
export function apiCreateProject(req, res) {
34+
export async function apiCreateProject(req, res) {
3535
const params = Object.assign({ user: req.user._id }, req.body);
3636

37-
function sendValidationErrors(err, type, code = 422) {
37+
const sendValidationErrors = (err, type, code = 422) => {
3838
res.status(code).json({
3939
message: `${type} Validation Failed`,
4040
detail: err.message,
4141
errors: err.files
4242
});
43-
}
43+
};
4444

4545
// TODO: Error handling to match spec
46-
function sendFailure(err) {
46+
const sendFailure = (err) => {
4747
res.status(500).end();
48-
}
48+
};
4949

50-
function handleErrors(err) {
50+
const handleErrors = (err) => {
5151
if (err instanceof FileValidationError) {
5252
sendValidationErrors(err, 'File', err.code);
5353
} else if (err instanceof ProjectValidationError) {
5454
sendValidationErrors(err, 'Sketch', err.code);
5555
} else {
5656
sendFailure();
5757
}
58-
}
58+
};
5959

60-
function checkUserHasPermission() {
60+
const checkUserHasPermission = () => {
6161
if (req.user.username !== req.params.username) {
6262
console.log('no permission');
6363
const error = new ProjectValidationError(
@@ -67,35 +67,32 @@ export function apiCreateProject(req, res) {
6767

6868
throw error;
6969
}
70-
}
70+
};
7171

7272
try {
7373
checkUserHasPermission();
7474

75+
if (!params.files || typeof params.files !== 'object') {
76+
const error = new FileValidationError("'files' must be an object");
77+
throw error;
78+
}
79+
7580
const model = toModel(params);
7681

77-
return model
78-
.isSlugUnique()
79-
.then(({ isUnique, conflictingIds }) => {
80-
if (isUnique) {
81-
return model.save().then((newProject) => {
82-
res.status(201).json({ id: newProject.id });
83-
});
84-
}
85-
86-
const error = new ProjectValidationError(
87-
`Slug "${model.slug}" is not unique. Check ${conflictingIds.join(
88-
', '
89-
)}`
90-
);
91-
error.code = 409;
92-
93-
throw error;
94-
})
95-
.then(checkUserHasPermission)
96-
.catch(handleErrors);
82+
const { isUnique, conflictingIds } = await model.isSlugUnique();
83+
84+
if (!isUnique) {
85+
const error = new ProjectValidationError(
86+
`Slug "${model.slug}" is not unique. Check ${conflictingIds.join(', ')}`
87+
);
88+
error.code = 409;
89+
90+
throw error;
91+
}
92+
93+
const newProject = await model.save();
94+
res.status(201).json({ id: newProject.id });
9795
} catch (err) {
9896
handleErrors(err);
99-
return Promise.reject(err);
10097
}
10198
}

0 commit comments

Comments
 (0)