Skip to content

Commit ff5983a

Browse files
committed
Authors endpoint
1 parent 09cc4ab commit ff5983a

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

src/authors/authors.routes.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const slug = require('slug')
2+
3+
const db = require('../connectors/db')
4+
const schema = require('./authors.schema')
5+
6+
module.exports = async (fastify, options) => {
7+
fastify.get('/authors', schema, async (req, reply) => {
8+
const all = await db.getAll('author')
9+
return all
10+
})
11+
fastify.post('/authors', async (req, reply) => {
12+
const authorToSave = Object.assign({}, req.body, {
13+
id: slug(req.body.display_name, { lower: true })
14+
})
15+
await db.saveOne('author', authorToSave)
16+
reply.code(201)
17+
return {}
18+
})
19+
}

src/authors/authors.schema.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
schema: {
3+
response: {
4+
200: {
5+
type: 'array',
6+
items: {
7+
type: 'object',
8+
properties: {
9+
id: { type: 'string' },
10+
display_name: { type: 'string' },
11+
email: { type: 'string', format: 'email' }
12+
}
13+
}
14+
}
15+
}
16+
}
17+
}

src/authors/authors.spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const { test } = require('ava')
2+
const request = require('axios')
3+
const moment = require('moment')
4+
5+
const server = require('../server')
6+
7+
test.beforeEach(async t => {
8+
// This runs after each test and other test hooks, even if they failed
9+
const rdb = require('rethinkdbdash')({
10+
servers: [{ host: process.env.RDB_HOST, port: process.env.RDB_PORT }]
11+
})
12+
const tableList = await rdb.tableList().run()
13+
if (tableList.includes('author')) {
14+
await rdb.tableDrop('author').run()
15+
}
16+
})
17+
18+
// Run the server
19+
server.start({ port: 0 }, (err, fastify) => {
20+
test.serial(
21+
'The authors endpoint should return an empty array when no entries are saved',
22+
async t => {
23+
if (err) t.fail()
24+
const response = await request.get(
25+
`http://localhost:${fastify.server.address().port}/authors`
26+
)
27+
t.is(response.status, 200)
28+
t.deepEqual(response.data, [])
29+
}
30+
)
31+
32+
test.serial(
33+
'The authors endpoint should accept a POST call with a new author',
34+
async t => {
35+
if (err) t.fail()
36+
const input = {
37+
display_name: 'David Guijarro',
38+
39+
}
40+
const output = {
41+
id: 'david-guijarro',
42+
display_name: 'David Guijarro',
43+
44+
}
45+
46+
const postResponse = await request.post(
47+
`http://localhost:${fastify.server.address().port}/authors`,
48+
input
49+
)
50+
t.is(postResponse.status, 201)
51+
const getResponse = await request.get(
52+
`http://localhost:${fastify.server.address().port}/authors`
53+
)
54+
t.is(getResponse.status, 200)
55+
t.deepEqual(getResponse.data, [output])
56+
}
57+
)
58+
})

src/entries/entries.routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = async (fastify, options) => {
2020
// }
2121
// ])
2222
})
23-
fastify.post('/entries', schema, async (req, reply) => {
23+
fastify.post('/entries', async (req, reply) => {
2424
const entryToSave = Object.assign({}, req.body, {
2525
id: slug(req.body.title, { lower: true }),
2626
cheers: 0,

src/server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const helmet = require('fastify-helmet')
55
fastify.register(helmet)
66
fastify.register([
77
require('./status/status.routes'),
8-
require('./entries/entries.routes')
8+
require('./entries/entries.routes'),
9+
require('./authors/authors.routes')
910
])
1011

1112
const start = (opts, callback) => {

0 commit comments

Comments
 (0)