refactor: DRY some code related to answers

as there are two routes for answers(/unanswered/:slug and /:slug), and both contained similar code, reduced controllers(for the routes) in api as well as in views to just one controller handling the logic to fetch data.
This commit is contained in:
zyachel 2022-05-21 12:56:12 +05:30
parent 56994854d7
commit d8515d5edd
4 changed files with 16 additions and 37 deletions

View file

@ -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 });
});

View file

@ -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,
});
});

View file

@ -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;

View file

@ -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;