From 6b736e64c04cf082ffcd7d30428855181859f6a3 Mon Sep 17 00:00:00 2001 From: Marcelo Amorim Date: Sun, 13 Oct 2024 19:15:41 -0300 Subject: [PATCH] Implement endpoint to retrieve a sorted list of unique authors. Added support for filtering by quoteCategory. (#131) --- src/api/controllers/authorsController.js | 39 +++++++++++++++++++++++ src/api/routes/quotes-router.js | 4 +++ src/api/services/quotesService.js | 40 ++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 src/api/controllers/authorsController.js diff --git a/src/api/controllers/authorsController.js b/src/api/controllers/authorsController.js new file mode 100644 index 0000000..0798851 --- /dev/null +++ b/src/api/controllers/authorsController.js @@ -0,0 +1,39 @@ +const quoteService = require("../services/quotesService"); + +const authorsController = async (req, res, next) => { + + try { + + let quotesUrl = req.query.quotesUrl || ''; + + let quoteCategory = req.query.quoteCategory || ''; + + let quoteObject = { + quotesUrl, + quoteCategory, + } + + const authors = await quoteService.getAuthors(quoteObject); + + res.setHeader("Content-Type", "application/json"); + + res.header( + "Cache-Control", + "no-cache,max-age=0,no-store,s-maxage=0,proxy-revalidate" + ); + res.header("Pragma", "no-cache"); + res.header("Expires", "-1"); + res.json(authors); + + } catch (error) { + console.error(error); + res.send({ + name: error.name, + message: error.message, + }); + } +}; + +module.exports = { + authorsController +}; diff --git a/src/api/routes/quotes-router.js b/src/api/routes/quotes-router.js index 22d6770..b0a35eb 100644 --- a/src/api/routes/quotes-router.js +++ b/src/api/routes/quotes-router.js @@ -116,12 +116,16 @@ const controllers = require('../controllers/quotesController'); +const authorsController = require('../controllers/authorsController'); const defineRoutes = (app) => { // get a quote app.get('/quote', controllers.quoteController); + // get authors + app.get('/authors', authorsController.authorsController); + } module.exports = defineRoutes; diff --git a/src/api/services/quotesService.js b/src/api/services/quotesService.js index 63b8250..b27491a 100644 --- a/src/api/services/quotesService.js +++ b/src/api/services/quotesService.js @@ -79,6 +79,46 @@ const getQuote = async (quoteObj) => { } }; +const getAuthors = async (quoteObj) => { + try { + let {quotesUrl, quoteCategory} = quoteObj; + let apiResponse; + let { customQuotesUrl, isValidUrl } = await getValidUrl(quotesUrl); + let isCustomQuote = false; + + if (isValidUrl) { + //url from params is valid, proceed to verfiy the data + apiResponse = await requestApi(customQuotesUrl); + + if (apiResponse.length > 0) { + if (apiResponse[0].author) { + isCustomQuote = true; + } + } + } + else if (quoteCategory) { + apiResponse = quoteFromCategory[quoteCategory]; + isCustomQuote = true; + } + + if(!isCustomQuote) { + apiResponse = await requestApi(url); + } + + if (!apiResponse || apiResponse.length === 0) { + return []; + } + + // Get array of authors in alphabetical order and without duplicates + const authors = [...new Set(apiResponse.map(quote => quote.author).sort())]; + + return authors; + } catch (error) { + throw error; + } +} + module.exports = { getQuote, + getAuthors };