Skip to content

Commit

Permalink
refactor(editor): Extract getEditorTitleJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
naiveai committed Dec 29, 2017
1 parent 019f28b commit 2535c2d
Showing 1 changed file with 27 additions and 67 deletions.
94 changes: 27 additions & 67 deletions src/server/routes/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ router.post('/edit/handler', auth.isAuthenticatedForHandler, (req, res) => {
handler.sendPromiseResult(res, editorJSONPromise);
});

function getEditorTitleJSON(editorJSON, TitleUnlock) {
let editorTitleJSON;
if (editorJSON.titleUnlockId === null) {
editorTitleJSON = Promise.resolve(editorJSON);
}
else {
editorTitleJSON = new TitleUnlock({
id: editorJSON.titleUnlockId
})
.fetch({
withRelated: ['title']
})
.then((unlock) => {
if (unlock !== null) {
editorJSON.title =
unlock.relations.title.attributes;
}
return editorJSON;
});
}
return editorTitleJSON;
}

router.get('/:id', (req, res, next) => {
const {AchievementUnlock, Editor, TitleUnlock} = req.app.locals.orm;
const userId = parseInt(req.params.id, 10);
Expand All @@ -155,28 +178,7 @@ router.get('/:id', (req, res, next) => {

return editorJSON;
})
.then((editorJSON) => {
let editorTitleJSON;
if (editorJSON.titleUnlockId === null) {
editorTitleJSON = Promise.resolve(editorJSON);
}
else {
editorTitleJSON = new TitleUnlock({
id: editorJSON.titleUnlockId
})
.fetch({
withRelated: ['title']
})
.then((unlock) => {
if (unlock !== null) {
editorJSON.title =
unlock.relations.title.attributes;
}
return editorJSON;
});
}
return editorTitleJSON;
})
.then(getEditorTitleJSON, TitleUnlock)
.catch(Editor.NotFoundError, () => {
throw new error.NotFoundError('Editor not found', req);
})
Expand Down Expand Up @@ -240,29 +242,7 @@ router.get('/:id/revisions', (req, res, next) => {
}
}
})
.then((editor) => {
const editorJSON = editor.toJSON();
let editorTitleJSON;
if (editorJSON.titleUnlockId === null) {
editorTitleJSON = Promise.resolve(editorJSON);
}
else {
editorTitleJSON = new TitleUnlock({
id: editorJSON.titleUnlockId
})
.fetch({
withRelated: ['title']
})
.then((unlock) => {
if (unlock !== null) {
editorJSON.title =
unlock.relations.title.attributes;
}
return editorJSON;
});
}
return editorTitleJSON;
})
.then(getEditorTitleJSON, TitleUnlock)
.then((editorJSON) => {
const props = generateProps(req, res, {
editor: editorJSON,
Expand Down Expand Up @@ -331,28 +311,7 @@ router.get('/:id/achievements', (req, res, next) => {

return editorJSON;
})
.then((editorJSON) => {
let editorTitleJSON;
if (editorJSON.titleUnlockId === null) {
editorTitleJSON = Promise.resolve(editorJSON);
}
else {
editorTitleJSON = new TitleUnlock({
id: editorJSON.titleUnlockId
})
.fetch({
withRelated: ['title']
})
.then((unlock) => {
if (unlock !== null) {
editorJSON.title =
unlock.relations.title.attributes;
}
return editorJSON;
});
}
return editorTitleJSON;
})
.then(getEditorTitleJSON, TitleUnlock)
.catch(Editor.NotFoundError, () => {
throw new error.NotFoundError('Editor not found', req);
})
Expand Down Expand Up @@ -431,6 +390,7 @@ function rankUpdate(orm, editorId, bodyRank, rank) {
});
}


router.post('/:id/achievements/', auth.isAuthenticated, (req, res) => {
const {orm} = req.app.locals;
const {Editor} = orm;
Expand Down

0 comments on commit 2535c2d

Please sign in to comment.