Skip to content

Commit

Permalink
feat(route): ✨ automatically seed authors from requested book
Browse files Browse the repository at this point in the history
New query param, `seedAuthors` can be set to `0` to disable this on call
  • Loading branch information
djdembeck committed Oct 2, 2021
1 parent ac22686 commit e99a52d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"actions",
"parser",
"scraper",
"chapters"
"chapters",
"route"
]
}
22 changes: 22 additions & 0 deletions src/config/routes/books/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import Book from '../../models/Book'
import ScrapeHelper from '../../../helpers/books/audibleScrape'
import SharedHelper from '../../../helpers/shared'
import StitchHelper from '../../../helpers/books/audibleStitch'
import fetch from 'isomorphic-fetch'

/**
* Calls authors endpoint in the background with ASIN supplied
* @param {string} ASIN
*/
async function seedAuthors (ASIN: string) {
fetch('http://localhost:3000/authors/' + ASIN)
}

async function routes (fastify, options) {
fastify.get('/books/:asin', async (request, reply) => {
Expand Down Expand Up @@ -41,9 +50,22 @@ async function routes (fastify, options) {
stitch.htmlRes = parseScraper
}

// Run stitcher and wait for promise to resolve
const stichedData = await Promise.resolve(stitch.process())
// Insert stitched data into DB
const newDbItem = await Promise.resolve(Book.insertOne(stichedData))
redis.set(`book-${request.params.asin}`, JSON.stringify(newDbItem, null, 2))

// Seed authors in the background
if (request.query.seedAuthors !== '0' && newDbItem.authors) {
try {
newDbItem.authors.map((author, index): any => {
return seedAuthors(author!.asin!)
})
} catch (err) {
console.error(err)
}
}
return newDbItem
}
})
Expand Down

0 comments on commit e99a52d

Please sign in to comment.