Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(authors): add authors api endpoints #23

Merged
merged 8 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"core-js": "^3.6.5",
"dotenv": "^8.2.0",
"faker": "^5.5.3",
"jsonapi-serializer": "^3.6.7",
"koa": "^2.13.0",
"koa-body": "^4.2.0",
"koa-ejs": "^4.3.0",
Expand Down
30 changes: 21 additions & 9 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const assets = require('./assets');
const mailer = require('./mailers');
const routes = require('./routes');
const orm = require('./models');
const api = require('./routes/api');

// App constructor
const app = new Koa();
Expand All @@ -22,7 +23,7 @@ app.keys = [
'these secret keys are used to sign HTTP cookies',
'to make sure only this app can generate a valid one',
'and thus preventing someone just writing a cookie',
'saying he is logged in when it\'s really not',
"saying he is logged in when it's really not",
];

// expose ORM through context's prototype
Expand Down Expand Up @@ -55,21 +56,31 @@ if (developmentMode) {
app.use(koaStatic(path.join(__dirname, '..', 'build'), {}));

// expose a session hash to store information across requests from same client
app.use(session({
maxAge: 14 * 24 * 60 * 60 * 1000, // 2 weeks
}, app));
app.use(
session(
{
maxAge: 14 * 24 * 60 * 60 * 1000, // 2 weeks
},
app,
),
);

// flash messages support
app.use(koaFlashMessage);

// parse request body
app.use(koaBody({
multipart: true,
keepExtensions: true,
}));
app.use(
koaBody({
multipart: true,
keepExtensions: true,
}),
);

app.use((ctx, next) => {
ctx.request.method = override.call(ctx, ctx.request.body.fields || ctx.request.body);
ctx.request.method = override.call(
ctx,
ctx.request.body.fields || ctx.request.body,
);
return next();
});

Expand All @@ -85,5 +96,6 @@ mailer(app);

// Routing middleware
app.use(routes.routes());
app.use(api.routes());

module.exports = app;
30 changes: 30 additions & 0 deletions src/routes/api/authors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const KoaRouter = require('koa-router');
const JSONAPISerializer = require('jsonapi-serializer').Serializer;

const AuthorSerializer = new JSONAPISerializer('authors', {
attributes: ['firstName', 'lastName', 'birthDate'],
keyForAttribute: 'camelCase',
});

const router = new KoaRouter();

router.get('api.authors.show', '/:id', async (ctx) => {
const author = await ctx.orm.author.findByPk(ctx.params.id);
if (!author) {
ctx.throw(404, "The author you are looking for doesn't exist");
}
ctx.body = AuthorSerializer.serialize(author);
});

router.post('api.authors.create', '/', async (ctx) => {
try {
const author = ctx.orm.author.build(ctx.request.body);
await author.save({ fields: ['firstName', 'lastName', 'birthDate'] });
ctx.status = 201;
ctx.body = AuthorSerializer.serialize(author);
} catch (ValidationError) {
ctx.throw(400, 'Bad request');
}
});

module.exports = router;
8 changes: 8 additions & 0 deletions src/routes/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const KoaRouter = require('koa-router');
const authors = require('./authors');

const router = new KoaRouter({ prefix: '/api' });

router.use('/authors', authors.routes());

module.exports = router;
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4573,6 +4573,11 @@ inflation@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/inflation/-/inflation-2.0.0.tgz"

inflected@^1.1.6:
version "1.1.7"
resolved "https://registry.yarnpkg.com/inflected/-/inflected-1.1.7.tgz#c393df6e28472d0d77b3082ec3aa2091f4bc96f9"
integrity sha1-w5PfbihHLQ13swguw6ogkfS8lvk=

[email protected]:
version "1.12.0"
resolved "https://registry.yarnpkg.com/inflection/-/inflection-1.12.0.tgz"
Expand Down Expand Up @@ -5539,6 +5544,14 @@ json5@^2.1.2:
dependencies:
minimist "^1.2.5"

jsonapi-serializer@^3.6.7:
version "3.6.7"
resolved "https://registry.yarnpkg.com/jsonapi-serializer/-/jsonapi-serializer-3.6.7.tgz#18299122e7624d962fadaba16c320da6a7c405c0"
integrity sha512-usScb34T6mB0QK/AxOpXTpE9iB7JW4/CMhREitzeKv9rV5CdZhdVA8F9zIc7Xz9rjbSv98vOXkuO1HRtM0paSg==
dependencies:
inflected "^1.1.6"
lodash "^4.16.3"

jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz"
Expand Down Expand Up @@ -5865,6 +5878,11 @@ lodash.sortby@^4.7.0:
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz"
integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==

lodash@^4.16.3:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

log-symbols@^2.1.0, log-symbols@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-2.2.0.tgz"
Expand Down