diff --git a/controllers/apiController.js b/controllers/apiController.js index f3c8ecd..a481bd0 100644 --- a/controllers/apiController.js +++ b/controllers/apiController.js @@ -8,21 +8,14 @@ import getAnswers from '../fetchers/getAnswers.js'; // EXPORTS //////////////////////////////////////////////////////// export const about = (req, res, next) => { - res - .status(200) - .json({ - status: 'success', - message: - "make a request. available endpoints are: '/some-slug', '/unanswered/some-slug'", - }); + res.status(200).json({ + status: 'success', + message: + "make a request. available endpoints are: '/some-slug', '/unanswered/some-slug'", + }); }; -export const unansweredQuestion = catchAsyncErrors(async (req, res, next) => { - const data = await getAnswers(`/unanswered/${req.params.slug}`); - res.status(200).json({ status: 'success', data }); -}); - -export const answeredQuestion = catchAsyncErrors(async (req, res, next) => { +export const answers = catchAsyncErrors(async (req, res, next) => { const data = await getAnswers(req.params.slug); res.status(200).json({ status: 'success', data }); }); diff --git a/controllers/viewController.js b/controllers/viewController.js index ad722af..c242a72 100644 --- a/controllers/viewController.js +++ b/controllers/viewController.js @@ -16,24 +16,16 @@ export const privacy = (req, res, next) => { res.render('privacy', { title: 'Privacy' }); }; -export const unansweredQuestion = catchAsyncErrors(async (req, res, next) => { - const answers = await getAnswers(`/unanswered/${req.params.slug}`); - res.status(200).render('answers', { - title: answers.question.text.spans.map(span => span.text).join(''), - data: answers, - }); -}); - -export const answeredQuestion = catchAsyncErrors(async (req, res, next) => { +export const answers = catchAsyncErrors(async (req, res, next) => { const { slug } = req.params; // added this so that a request by browser to get favicon doesn't end up being interpreted as a slug if (nonSlugRoutes.includes(slug)) return next(); - const answers = await getAnswers(slug); + const answersData = await getAnswers(slug); res.status(200).render('answers', { - title: answers.question.text.spans.map(span => span.text).join(''), - data: answers, + title: answersData.question.text.spans.map(span => span.text).join(''), + data: answersData, }); }); diff --git a/routes/apiRoutes.js b/routes/apiRoutes.js index 800cbc4..7f6128b 100644 --- a/routes/apiRoutes.js +++ b/routes/apiRoutes.js @@ -1,10 +1,5 @@ import express from 'express'; -import { - about, - unimplemented, - unansweredQuestion, - answeredQuestion, -} from '../controllers/apiController.js'; +import { about, unimplemented, answers } from '../controllers/apiController.js'; const apiRouter = express.Router(); @@ -12,7 +7,7 @@ apiRouter.get('/', about); apiRouter.get('/search', unimplemented); apiRouter.get('/profile/:name', unimplemented); apiRouter.get('/topic/:name', unimplemented); -apiRouter.get('/unanswered/:slug', unansweredQuestion); -apiRouter.get('/:slug', answeredQuestion); +apiRouter.get('/unanswered/:slug', answers); +apiRouter.get('/:slug', answers); export default apiRouter; diff --git a/routes/viewRoutes.js b/routes/viewRoutes.js index 3611348..5b37103 100644 --- a/routes/viewRoutes.js +++ b/routes/viewRoutes.js @@ -1,9 +1,8 @@ import express from 'express'; import { about, - answeredQuestion, + answers, privacy, - unansweredQuestion, unimplemented, } from '../controllers/viewController.js'; @@ -14,7 +13,7 @@ viewRouter.get('/privacy', privacy); viewRouter.get('/search', unimplemented); viewRouter.get('/profile/:name', unimplemented); viewRouter.get('/topic/:name', unimplemented); -viewRouter.get('/unanswered/:slug', unansweredQuestion); -viewRouter.get('/:slug', answeredQuestion); +viewRouter.get('/unanswered/:slug', answers); +viewRouter.get('/:slug', answers); export default viewRouter;