-
-
Notifications
You must be signed in to change notification settings - Fork 155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Quotes based on Specific Personality (#131) #324
base: main
Are you sure you want to change the base?
Changes from all commits
d8d23d8
472893b
6b736e6
4d51c0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useState, useCallback, useEffect } from "react"; | ||
|
||
import { serverUrl } from "../../components/Constants/urlConfig"; | ||
|
||
const useQuoteAuthors = () => { | ||
const originUrl = serverUrl; | ||
const [quoteAuthors, setQuoteAuthors] = useState([]); | ||
const [loadingQuoteAuthors, setLoadingQuoteAuthors] = useState(false); | ||
|
||
const fetchQuoteAuthors = useCallback(async () => { | ||
setLoadingQuoteAuthors(true); | ||
try { | ||
const response = await fetch(`${originUrl}/authors`); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
const data = await response.json(); | ||
if (data) { | ||
setQuoteAuthors(data); | ||
} | ||
} catch (error) { | ||
console.error("Failed to fetch quote authors:", error); | ||
} finally { | ||
setLoadingQuoteAuthors(false); | ||
} | ||
}, [originUrl]); | ||
|
||
useEffect(() => { | ||
fetchQuoteAuthors(); | ||
}, [fetchQuoteAuthors]); | ||
|
||
return { | ||
quoteAuthors, | ||
loadingQuoteAuthors | ||
}; | ||
} | ||
|
||
export default useQuoteAuthors; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
||
Comment on lines
+126
to
+128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add Swagger documentation for the /authors endpoint While the route implementation looks good, it's missing Swagger documentation. This documentation is crucial for API consistency and client integration. Add the following Swagger documentation above the route: /**
* @swagger
* /authors:
* get:
* tags:
* - Authors
* description: Returns a list of quote authors
* parameters:
* - name: category
* in: query
* schema:
* $ref: "#/components/schemas/quoteCategory"
* description: Filter authors by quote category
* responses:
* 200:
* description: List of authors
* content:
* application/json:
* schema:
* type: array
* items:
* type: string
* 500:
* description: Server error
*/ |
||
} | ||
|
||
module.exports = defineRoutes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add error handling UI and improve accessibility.
The author selection implementation needs improvements in error handling and accessibility:
Apply these changes: