Skip to content

Find hadiths stored under a combined number (#614) - #3993

Open
wakqasahmed wants to merge 1 commit into
sunnah-com:masterfrom
wakqasahmed:fix/issue-614-combined-hadith-numbers
Open

wakqasahmed wants to merge 1 commit into
sunnah-com:masterfrom
wakqasahmed:fix/issue-614-combined-hadith-numbers

Conversation

@wakqasahmed

Copy link
Copy Markdown
Contributor

Some hadiths are stored under a combined number like "6924, 6925", so /v1/collections/bukhari/hadiths/6924 and .../6925 both returned 404 because every lookup was an exact match on hadithNumber.

I followed what the website already does in Util::getURNByNumber: try the exact match first, and only if that finds nothing, do a LIKE '%<n>%' search and keep the rows where the number equals one of the trimmed parts after splitting on , or -. The split/match bit lives in a small hadith_number.py so it can be unit tested without a database (main.py can't be imported in tests because models.py reflects against MySQL). The LIKE uses a bound param with autoescape, so % or _ in the URL don't act as wildcards. I used the same fallback for GET /v1/hadiths?hadithNumber= since it's the same one-line swap. I left /v1/hadiths/refs alone because it batches all refs into a single OR query and would need a separate second pass, and I didn't touch the Muslim letter-suffix handling (#1456).

To check it, I ran the repo's docker compose stack and added one copy of Bukhari 1 to the sample DB with hadithNumber = '6924, 6925'.

Before:

GET /v1/collections/bukhari/hadiths/6924                  -> 404
GET /v1/collections/bukhari/hadiths/6925                  -> 404
GET /v1/hadiths?collection=bukhari&hadithNumber=6925      -> 200, total 0

After:

GET /v1/collections/bukhari/hadiths/6924                  -> 200, hadithNumber "6924, 6925"
GET /v1/collections/bukhari/hadiths/6925                  -> 200, hadithNumber "6924, 6925"
GET /v1/hadiths?collection=bukhari&hadithNumber=6925      -> 200, total 1
GET /v1/collections/bukhari/hadiths/692                   -> 404 (partial number, still no match)
GET /v1/collections/bukhari/hadiths/9999                  -> 404
GET /v1/collections/bukhari/hadiths/%25                   -> 404
GET /v1/collections/bukhari/hadiths/5                     -> 200, unchanged

I also added tests/test_hadith_number.py. python3 -m unittest tests.test_hadith_number tests.test_text_transform passes (10 tests), and black/flake8 are clean on the lines I changed.

Fixes #614

Some hadiths are stored with a combined hadithNumber such as "6924, 6925",
so looking up 6924 or 6925 on its own returned 404. When the exact match
finds nothing, fall back to a LIKE search and keep rows where the number
is one of the comma or dash separated parts, as sunnah.com does in
Util::getURNByNumber.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle Comma Cases in fetch hadith with hadith number route

1 participant