Skip to content

Commit

Permalink
feat(route): 🚚 add search endpoint
Browse files Browse the repository at this point in the history
supported query params are `asin` for exact match and `name` for regex search
  • Loading branch information
djdembeck committed Oct 3, 2021
1 parent 8cc40cf commit 867804a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/config/routes/authors/search/show.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import SharedHelper from '../../../../helpers/shared'
import Author from '../../../models/Author'

async function routes (fastify, options) {
fastify.get('/authors', async (request, reply) => {
const asin = request.query.asin
const name = request.query.name

if (!name && !asin) {
throw new Error('No search params provided')
}

if (asin) {
// First, check ASIN validity
const commonHelpers = new SharedHelper()
if (!commonHelpers.checkAsinValidity(asin)) {
throw new Error('Bad ASIN')
}
return Promise.resolve(Author.findOne({ asin: asin }))
}

if (name) {
const searchObject = { name: { $regex: name, $options: 'i' } }
return Promise.resolve(Author.find(searchObject))
}
})
}

export default routes
2 changes: 2 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import deleteBook from './config/routes/books/delete'
import showChapter from './config/routes/books/chapters/show'
// Author routes
import showAuthor from './config/routes/authors/show'
import searchAuthor from './config/routes/authors/search/show'
// System imports
import { fastify } from 'fastify'
import { connect, disconnect } from './config/papr'
Expand All @@ -24,6 +25,7 @@ server.register(showChapter)
server.register(deleteBook)
// Register author routes
server.register(showAuthor)
server.register(searchAuthor)

server.register(require('fastify-redis'), { url: REDIS_URL })

Expand Down

0 comments on commit 867804a

Please sign in to comment.