Binrushd-Medical-Center commited on
Commit
a197203
ยท
verified ยท
1 Parent(s): 33608fe

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +56 -1
main.py CHANGED
@@ -704,7 +704,7 @@ def branch_details(branch_id: int):
704
 
705
  raise HTTPException(404, f"Branch {branch_id} not found")
706
 
707
- # ====== Blog (from local JSON files) ======
708
 
709
  @app.get("/api/blog")
710
  def list_blog_summary():
@@ -747,3 +747,58 @@ def blog_post_details(post_id: int):
747
  return {"message": "Post Fetched Successfully", "data": items[str(post_id)]}
748
 
749
  raise HTTPException(404, f"Post {post_id} not found")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
704
 
705
  raise HTTPException(404, f"Branch {branch_id} not found")
706
 
707
+ # ====== Blog ======
708
 
709
  @app.get("/api/blog")
710
  def list_blog_summary():
 
747
  return {"message": "Post Fetched Successfully", "data": items[str(post_id)]}
748
 
749
  raise HTTPException(404, f"Post {post_id} not found")
750
+
751
+ # ====== Offers ======
752
+
753
+ @app.get("/api/offers")
754
+ def list_offers():
755
+ """
756
+ ูŠุฑุฌู‘ุน ูƒู„ ุงู„ุนุฑูˆุถ (offers + campaigns) ูƒู…ุง ู‡ูŠ ู…ู† ู…ู„ู offers.json
757
+ """
758
+ path = BASE_DIR / "offers.json"
759
+ if not path.exists():
760
+ raise HTTPException(500, "offers.json not found")
761
+
762
+ try:
763
+ raw = json.loads(path.read_text(encoding="utf-8"))
764
+ except json.JSONDecodeError:
765
+ raise HTTPException(500, "offers.json contains invalid JSON")
766
+
767
+ # ู„ูˆ ุงู„ู…ู„ู ุจู†ูุณ ุงู„ุจู†ูŠุฉ ุงู„ู…ุฑุณู„ุฉ (message + data) ู†ุฑุฌู‘ุนู‡ ูƒู…ุง ู‡ูˆ:
768
+ if isinstance(raw, dict) and "data" in raw:
769
+ return raw
770
+
771
+ # fallback ุจุณูŠุท
772
+ return {"message": "Offers Fetched Successfully", "data": raw}
773
+
774
+
775
+ @app.get("/api/offers/{item_id}")
776
+ def get_offer_by_id(item_id: int):
777
+ """
778
+ ูŠุฑุฌู‘ุน ุนู†ุตุฑ ูˆุงุญุฏ ุจุงู„ู€ id โ€” ูŠุจุญุซ ุฃูˆู„ุงู‹ ููŠ campaigns ุซู… ููŠ offers.
779
+ """
780
+ path = BASE_DIR / "offers.json"
781
+ if not path.exists():
782
+ raise HTTPException(500, "offers.json not found")
783
+
784
+ try:
785
+ raw = json.loads(path.read_text(encoding="utf-8"))
786
+ except json.JSONDecodeError:
787
+ raise HTTPException(500, "offers.json contains invalid JSON")
788
+
789
+ payload = raw.get("data", raw) if isinstance(raw, dict) else {}
790
+ campaigns = payload.get("campaigns", []) if isinstance(payload, dict) else []
791
+ offers = payload.get("offers", []) if isinstance(payload, dict) else []
792
+
793
+ # ุฏูˆุฑ ููŠ ุงู„ู€ campaigns
794
+ for c in campaigns:
795
+ if c.get("id") == item_id:
796
+ return {"message": "Campaign Fetched Successfully", "data": c}
797
+
798
+ # ุฏูˆุฑ ููŠ ุงู„ู€ offers
799
+ for o in offers:
800
+ if o.get("id") == item_id:
801
+ return {"message": "Offer Fetched Successfully", "data": o}
802
+
803
+ raise HTTPException(404, f"Item with id={item_id} not found")
804
+