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 range for ayahs endpoint #121

Open
wants to merge 1 commit 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
10 changes: 9 additions & 1 deletion src/controllers/surahs.controller.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
};

Expand Down
23 changes: 22 additions & 1 deletion src/services/quran.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

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