Skip to content
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

feat: Add API to fetch qur'an data by page #55

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/handlers/juz.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const juzData = require('../lib/juz.js');
const getData = require('../lib/get-data');

class JuzHandler {
static getJuz(req, res) {
const { juz } = req.params;
const data = juzData(parseInt(juz));
const data = getData({ input: parseInt(juz), mode: 'juz' });

if (!data) {
return res.status(404).send({
Expand Down
26 changes: 26 additions & 0 deletions api/handlers/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const getData = require('../lib/get-data');

class PageHandler {
static getPage(req, res) {
const { page } = req.params;
const data = getData({ input: parseInt(page), mode: 'page' });

if (!data) {
return res.status(404).send({
code: 404,
status: 'Not Found.',
message: `page "${page}" is not found.`,
data: {}
});
}

return res.status(200).send({
code: 200,
status: 'OK.',
message: 'Success fetching page.',
data
});
}
}

module.exports = PageHandler;
57 changes: 57 additions & 0 deletions api/lib/get-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { data: juz } = require('../../data/juz.json');
const { data: page } = require('../../data/page.json');
const { data: quran } = require('../../data/quran.json');

/**
* Retrieves data based on the input and mode.
*
* @param {Object} options - The options object.
* @param {number} options.input - The input value.
* @param {string} options.mode - The mode value ('juz' or 'page').
* @returns {Object|null} - The data object containing the retrieved data, or null if no data is found.
*/
const getData = ({ input, mode }) => {
const inputData = mode === 'juz' ? juz[input - 1] : page[input - 1];

if (!inputData) return null;

const startSurah = inputData.start.index - 1;
const startAyah = inputData.start.verse - 1;
const endSurah = inputData.end.index - 1;
const endAyah = inputData.end.verse;
let ayah, _firstSurah, _middle, _middleSurah, _lastSurah;

if (startSurah === endSurah) {
ayah = quran[startSurah].verses.slice(startAyah, endAyah);
} else if (endSurah - startSurah > 1) {
_firstSurah = quran[startSurah].verses.slice(startAyah);
_middle = quran.slice(startSurah + 1, endSurah);
_middleSurah = [];
_middle.map((items) => {
items.verses.map((item) => {
_middleSurah.push(item);
});
});
_lastSurah = quran[endSurah].verses.slice(0, endAyah);
ayah = [..._firstSurah, ..._middleSurah, ..._lastSurah];
} else {
_firstSurah = quran[startSurah].verses.slice(startAyah);
_lastSurah = quran[endSurah].verses.slice(0, endAyah);
ayah = [..._firstSurah, ..._lastSurah];
}

const startSurahName = quran[startSurah].name.transliteration.id;
const endSurahName = quran[endSurah].name.transliteration.id;
const data = {
[mode]: input,
[`${mode}StartSurahNumber`]: inputData.start.index,
[`${mode}EndSurahNumber`]: inputData.end.index,
[`${mode}StartInfo`]: `${startSurahName} - ${inputData.start.verse}`,
[`${mode}EndInfo`]: `${endSurahName} - ${inputData.end.verse}`,
totalVerses: ayah.length,
verses: ayah
};
return data;
};

module.exports = getData;
49 changes: 0 additions & 49 deletions api/lib/juz.js

This file was deleted.

6 changes: 6 additions & 0 deletions api/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { Router } = require('express');
const { caching } = require('./middlewares');
const SurahHandler = require('./handlers/surah');
const JuzHandler = require('./handlers/juz');
const PageHandler = require('./handlers/page');

const router = Router();

Expand All @@ -25,6 +26,10 @@ router.get('/', (req, res) => res.status(200).send({
spesificJuz: {
pattern: '/juz/{juz}',
example: '/juz/30'
},
spesificPage: {
pattern: '/page/{page}',
example: '/page/252'
}
},
maintaner: 'Sutan Gading Fadhillah Nasution <contact@gading.dev>',
Expand All @@ -36,6 +41,7 @@ router.get('/surah', caching, SurahHandler.getAllSurah);
router.get('/surah/:surah', caching, SurahHandler.getSurah);
router.get('/surah/:surah/:ayah', caching, SurahHandler.getAyahFromSurah);
router.get('/juz/:juz', caching, JuzHandler.getJuz);
router.get('/page/:page', caching, PageHandler.getPage);

// fallback router
router.all('*', (req, res) => res.status(404).send({
Expand Down
Loading