Skip to content
This repository has been archived by the owner on Aug 28, 2020. It is now read-only.

Commit

Permalink
JSON view
Browse files Browse the repository at this point in the history
  • Loading branch information
ukatama committed Apr 4, 2016
1 parent 52be218 commit be06a96
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/containers/character.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {Character as Component} from '../components/character';
export const Character = connect(
(state) => ({
...state.character,
data: state.character.data && JSON.parse(state.character.data),
types: state.types,
}),
(dispatch) => ({
Expand Down
2 changes: 1 addition & 1 deletion src/server/__tests__/test-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('routes', () => {

const get = app.get.mock.calls.map((call) => call[0]);
expect(get).toContain('/');
expect(get).toContain('/:id');
expect(get).toContain('/:id([0-9a-f]+)');

const post = app.post.mock.calls.map((call) => call[0]);
expect(post).toContain('/');
Expand Down
44 changes: 31 additions & 13 deletions src/server/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,41 @@ export const router = (app) => {
}))
.catch(next)
);
app.get('/:id', (req, res, next) =>

const getCharacter = (id) =>
knex('characters')
.whereNull('deleted')
.where('id', req.params.id)
.where('id', id)
.first()
.then((character) => character || Promise.reject('Not Found'))
.then((character) => res.render('index', {
script,
route: {
route: 'Character',
params: req.params,
},
state: {
types: config.types,
character,
},
}))
.then((character) => ({
...character,
data: character.data && JSON.parse(character.data),
}));
app.get('/:id([0-9a-f]+).json', (req, res, next) =>
getCharacter(req.params.id)
.then((character) => res.send(character))
.catch(next)
);
app.get('/:id([0-9a-f]+)', (req, res, next) =>
getCharacter(req.params.id)
.then((character) => {
if (!req.accepts('html') && req.accepts('json')) {
return res.send(character);
}

return res.render('index', {
script,
route: {
route: 'Character',
params: req.params,
},
state: {
types: config.types,
character,
},
});
})
.catch(next)
);

Expand Down

0 comments on commit be06a96

Please sign in to comment.