Skip to content

Commit 0480137

Browse files
authored
Merge pull request #1 from sunnah-com/master
Add hadiths list endpoint with filtering support (#1811)
2 parents 35756a5 + b12e636 commit 0480137

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

main.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,32 @@ def api_collection_book_chapter(collection_name, bookNumber, chapterId):
107107
return Chapter.query.filter_by(collection=collection_name, arabicBookID=book_id, babID=chapterId)
108108

109109

110+
@app.route("/v1/hadiths", methods=["GET"])
111+
@paginate_results
112+
def api_hadiths():
113+
query = Hadith.query
114+
115+
# Apply filters based on query parameters
116+
collection = request.args.get("collection")
117+
if collection:
118+
query = query.filter_by(collection=collection)
119+
120+
book_number = request.args.get("bookNumber")
121+
if book_number:
122+
query = query.filter_by(bookNumber=book_number)
123+
124+
chapter_id = request.args.get("chapterId")
125+
if chapter_id:
126+
query = query.filter_by(babID=float(chapter_id))
127+
128+
hadith_number = request.args.get("hadithNumber")
129+
if hadith_number:
130+
query = query.filter_by(hadithNumber=hadith_number)
131+
132+
# Order by URN for consistent results
133+
return query.order_by(Hadith.englishURN)
134+
135+
110136
@app.route("/v1/hadiths/<int:urn>", methods=["GET"])
111137
@single_resource
112138
def api_hadith(urn):

spec.v1.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,48 @@ paths:
227227
required: true
228228
schema:
229229
type: string
230+
/hadiths:
231+
get:
232+
summary: Get a list of hadiths
233+
description: ""
234+
responses:
235+
"200":
236+
description: Paginated list of hadiths
237+
content:
238+
application/json:
239+
schema:
240+
type: object
241+
allOf:
242+
- properties:
243+
data:
244+
type: array
245+
items:
246+
$ref: "#/components/schemas/Hadith"
247+
- $ref: "#/components/schemas/PaginatedResponse"
248+
parameters:
249+
- in: query
250+
name: collection
251+
description: Name of the collection
252+
schema:
253+
type: string
254+
- in: query
255+
name: bookNumber
256+
description: Number of the book
257+
schema:
258+
type: string
259+
- in: query
260+
name: chapterId
261+
description: ID of the chapter
262+
schema:
263+
type: number
264+
format: float
265+
- in: query
266+
name: hadithNumber
267+
description: Hadith number
268+
schema:
269+
type: string
270+
- $ref: "#/components/parameters/limit"
271+
- $ref: "#/components/parameters/page"
230272
"/hadiths/{urn}":
231273
get:
232274
summary: Get a hadith by its URN

0 commit comments

Comments
 (0)