Skip to content

Commit

Permalink
Add API for getting a quote by its ID (lukePeavey#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
marekdano authored and lukePeavey committed Sep 19, 2019
1 parent 926ac40 commit c541838
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ I originally built this for a freeCodeCamp project, and decided to publish for o

- [Get a random quote](#get-random-quote)
- [Search quotes](#search-quotes-beta)
- [Get Quote by ID](#get-quote-by-id)
- [Search authors](#search-authors-beta)
- [Get Author By ID](#get-author-by-id-beta)
- [Examples](#examples)
Expand Down Expand Up @@ -70,6 +71,26 @@ https://api.quotable.io/quotes
}
```

### Get Quote By ID

Get a quote by its ID

#### Request

```http
https://api.quotable.io/quotes/:id
```

#### Response

```ts
{
_id: string,
content: string,
author: string
}
```

### Search Authors (beta)

Search the database for authors using various filter/sorting options. All parameters are optional. By default, it returns all authors in alphabetical order.
Expand Down
19 changes: 19 additions & 0 deletions __tests__/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,22 @@ describe('GET /authors', () => {
expect(response.type).toBe('application/json')
})
})

describe('GET /quotes/:id', () => {
it('Request completed successfully', async () => {
const response = await request(app).get('/quotes/gcPBYtDU718')
expect(response.status).toBe(200)
expect(response.type).toBe('application/json')
expect(response.body).toEqual({
_id: expect.any(String),
author: expect.any(String),
content: expect.any(String),
})
})

it('Responds with error', async () => {
const response = await request(app).get('/quotes/fakeId')
expect(response.status).toBe(404)
expect(response.type).toBe('application/json')
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"start:dev": "NODE_ENV=development nodemon",
"start:debug": "NODE_ENV=development --inspect nodemon",
"lint": "./node_modules/.bin/eslint . --cache --color && echo \"eslint: no lint errors\"",
"test": "jest",
"test": "jest --watchAll",
"database:seed": "node scripts/seedDatabase.js",
"deploy:production": "heroku pipelines:promote -a quotable-api-staging"
},
Expand Down
18 changes: 18 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,22 @@ router.get('/author/:id', async (req, res, next) => {
})
})

/**
* Get a quote by its ID
*/
router.get('/quotes/:id', async (req, res, next) => {
const id = escape(req.params.id)

// Get the author by ID
const quote = await Quotes.findById(id).select('content author')
if (!quote) {
return next(createError(404, 'The requested resource could not be found'))
}
res.status(200).json({
_id: quote._id,
content: quote.content,
author: quote.author,
})
})

module.exports = router

0 comments on commit c541838

Please sign in to comment.