-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
supported query params are `asin` for exact match and `name` for regex search
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters