diff --git a/src/controllers/surahs.controller.js b/src/controllers/surahs.controller.js index 5ed7764..8368f79 100644 --- a/src/controllers/surahs.controller.js +++ b/src/controllers/surahs.controller.js @@ -1,5 +1,7 @@ const { quranService } = require("../services"); +/* eslint no-console: ["error", { allow: ["log"] }] */ + const getSurahs = (req, res) => { const listSurahs = quranService.getListSurahs(); return res.send(listSurahs); @@ -13,7 +15,13 @@ const getSurah = (req, res) => { const getAyahs = (req, res) => { const { surahNumber } = req.params; - const ayahs = quranService.getAyahs(surahNumber); + let ayahs = quranService.getAyahs(surahNumber); + const { from, to } = req.query; + + if (from || to) { + ayahs = quranService.getAyahsRange(surahNumber, from, to); + } + return res.send(ayahs); }; diff --git a/src/services/quran.service.js b/src/services/quran.service.js index 7912f00..cdaa761 100644 --- a/src/services/quran.service.js +++ b/src/services/quran.service.js @@ -27,6 +27,20 @@ const getAyahs = (surahNumber) => { return ayahs; }; +const getAyahsRange = (surahNumber, from, to) => { + if (!from || !to) { + throw new ApiError(httpStatus.NOT_FOUND, "not found"); + } + + const ayahs = quran[Number(surahNumber) - 1]?.ayahs.slice(from - 1, to); + + if (!ayahs) { + throw new ApiError(httpStatus.NOT_FOUND, "not found"); + } + + return ayahs; +}; + const getAyah = (surahNumber, ayahNumber) => { const ayah = quran[Number(surahNumber) - 1]?.ayahs[Number(ayahNumber) - 1]; @@ -42,4 +56,11 @@ const getRandomSurah = () => { return surah.ayahs[getRandomInt(1, surah.ayahs.length) - 1]; }; -module.exports = { getListSurahs, getSurah, getAyahs, getAyah, getRandomSurah }; +module.exports = { + getListSurahs, + getSurah, + getAyahs, + getAyahsRange, + getAyah, + getRandomSurah, +};