| from fastapi import FastAPI, HTTPException |
| from pydantic import BaseModel |
| import aiosqlite, uvicorn, os |
| from typing import List, Optional |
|
|
|
|
| from fastapi import FastAPI, HTTPException |
| from fastapi.middleware.cors import CORSMiddleware |
| from pydantic import BaseModel |
| import aiosqlite |
| import uvicorn |
| import os |
| from pathlib import Path |
| from typing import List |
|
|
| app = FastAPI(title="Doctor-Patient API") |
|
|
| |
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_credentials=True, |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
| |
|
|
| |
| DB_PATH = str(Path(__file__).resolve().parent.parent / "core" / "daa.db") |
|
|
| |
| class Doctor(BaseModel): |
| id: int |
| doctor_name: str |
| category: str |
| visiting_days: str |
| visiting_time: str |
| visiting_money: int |
|
|
| class Patient(BaseModel): |
| id: int |
| doctor_name: str |
| doctor_category: str |
| patient_name: str |
| patient_age: str |
| patient_num: str |
| visiting_date: str |
| visiting_day: str | None = "" |
| visiting_time: str | None = "" |
| patient_mail: str |
|
|
| class AllDataResponse(BaseModel): |
| doctors: List[Doctor] |
| patients: List[Patient] |
|
|
|
|
| async def get_db(): |
| """Get database connection""" |
| conn = await aiosqlite.connect(DB_PATH) |
| conn.row_factory = aiosqlite.Row |
| return conn |
|
|
|
|
| @app.get("/api/all-data", response_model=AllDataResponse) |
| async def get_all_data(): |
| """Fetch all doctors and patients data""" |
| conn = None |
| try: |
| conn = await get_db() |
|
|
| |
| async with conn.execute("SELECT * FROM doctors") as cursor: |
| doctors_rows = await cursor.fetchall() |
| doctors = [dict(row) for row in doctors_rows] |
|
|
| |
| async with conn.execute("SELECT * FROM patients") as cursor: |
| patients_rows = await cursor.fetchall() |
| patients = [dict(row) for row in patients_rows] |
|
|
| return AllDataResponse( |
| doctors=doctors, |
| patients=patients |
| ) |
|
|
| except Exception as e: |
| raise HTTPException(status_code=500, detail=f"Database error: {str(e)}") |
| finally: |
| if conn: |
| await conn.close() |
|
|
|
|
| |
| @app.get("/api/doctors", response_model=List[Doctor]) |
| async def get_all_doctors(): |
| """Get all doctors only""" |
| conn = None |
| try: |
| conn = await get_db() |
| async with conn.execute("SELECT * FROM doctors") as cursor: |
| rows = await cursor.fetchall() |
| return [dict(row) for row in rows] |
| finally: |
| if conn: |
| await conn.close() |
|
|
|
|
| @app.get("/api/patients", response_model=List[Patient]) |
| async def get_all_patients(): |
| """Get all patients only""" |
| conn = None |
| try: |
| conn = await get_db() |
| async with conn.execute("SELECT * FROM patients") as cursor: |
| rows = await cursor.fetchall() |
| return [dict(row) for row in rows] |
| finally: |
| if conn: |
| await conn.close() |
| if __name__ == "__main__": |
| uvicorn.run("dbapi:app", host="127.0.0.1", port=8000, reload=True) |
|
|