Spaces:
Sleeping
Sleeping
Update api_server.py
Browse files- api_server.py +124 -1
api_server.py
CHANGED
|
@@ -124,4 +124,127 @@ def predict_completion(request: PredictionRequest):
|
|
| 124 |
if __name__ == "__main__":
|
| 125 |
import uvicorn
|
| 126 |
# host 0.0.0.0 允許外部訪問,port 8000 與前端設定一致
|
| 127 |
-
uvicorn.run("api_server:app", host="0.0.0.0", port=8000, reload=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
if __name__ == "__main__":
|
| 125 |
import uvicorn
|
| 126 |
# host 0.0.0.0 允許外部訪問,port 8000 與前端設定一致
|
| 127 |
+
uvicorn.run("api_server:app", host="0.0.0.0", port=8000, reload=True)
|
| 128 |
+
|
| 129 |
+
# =================================================================
|
| 130 |
+
# 4. 資料庫設定(SQLite + SQLAlchemy)
|
| 131 |
+
# =================================================================
|
| 132 |
+
from sqlalchemy import create_engine
|
| 133 |
+
from sqlalchemy.orm import sessionmaker, Session
|
| 134 |
+
from models import Base, Patient, Nurse, Record
|
| 135 |
+
|
| 136 |
+
DATABASE_URL = "sqlite:///./nursing.db"
|
| 137 |
+
|
| 138 |
+
engine = create_engine(
|
| 139 |
+
DATABASE_URL, connect_args={"check_same_thread": False}
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 143 |
+
|
| 144 |
+
# 建立資料表
|
| 145 |
+
Base.metadata.create_all(bind=engine)
|
| 146 |
+
|
| 147 |
+
def get_db():
|
| 148 |
+
db = SessionLocal()
|
| 149 |
+
try:
|
| 150 |
+
yield db
|
| 151 |
+
finally:
|
| 152 |
+
db.close()
|
| 153 |
+
|
| 154 |
+
# =================================================================
|
| 155 |
+
# 5. Schemas
|
| 156 |
+
# =================================================================
|
| 157 |
+
from pydantic import BaseModel
|
| 158 |
+
|
| 159 |
+
# 病患
|
| 160 |
+
class PatientCreate(BaseModel):
|
| 161 |
+
name: str
|
| 162 |
+
birth: str | None = None
|
| 163 |
+
gender: str | None = None
|
| 164 |
+
phone: str | None = None
|
| 165 |
+
email: str | None = None
|
| 166 |
+
|
| 167 |
+
emg_name: str | None = None
|
| 168 |
+
emg_phone: str | None = None
|
| 169 |
+
emg_relation: str | None = None
|
| 170 |
+
|
| 171 |
+
room: str
|
| 172 |
+
department: str
|
| 173 |
+
doctor: str | None = None
|
| 174 |
+
diagnosis: str
|
| 175 |
+
risk: str
|
| 176 |
+
admit_date: str
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
# 護士
|
| 180 |
+
class NurseCreate(BaseModel):
|
| 181 |
+
name: str
|
| 182 |
+
staff_id: str
|
| 183 |
+
department: str | None = None
|
| 184 |
+
position: str | None = None
|
| 185 |
+
phone: str | None = None
|
| 186 |
+
email: str | None = None
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
# 護理紀錄
|
| 190 |
+
class RecordCreate(BaseModel):
|
| 191 |
+
patient_id: int
|
| 192 |
+
nurse_id: int
|
| 193 |
+
content: str
|
| 194 |
+
created_at: str
|
| 195 |
+
|
| 196 |
+
# =================================================================
|
| 197 |
+
# 6. 病患 API
|
| 198 |
+
# =================================================================
|
| 199 |
+
@app.post("/patients")
|
| 200 |
+
def create_patient(data: PatientCreate, db: Session = next(get_db())):
|
| 201 |
+
patient = Patient(**data.dict())
|
| 202 |
+
db.add(patient)
|
| 203 |
+
db.commit()
|
| 204 |
+
db.refresh(patient)
|
| 205 |
+
return patient
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
@app.get("/patients")
|
| 209 |
+
def list_patients(db: Session = next(get_db())):
|
| 210 |
+
return db.query(Patient).all()
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
@app.get("/patients/{patient_id}")
|
| 214 |
+
def get_patient(patient_id: int, db: Session = next(get_db())):
|
| 215 |
+
patient = db.query(Patient).filter(Patient.id == patient_id).first()
|
| 216 |
+
if not patient:
|
| 217 |
+
raise HTTPException(status_code=404, detail="找不到病患")
|
| 218 |
+
return patient
|
| 219 |
+
|
| 220 |
+
# =================================================================
|
| 221 |
+
# 7. 護士 API
|
| 222 |
+
# =================================================================
|
| 223 |
+
@app.post("/nurses")
|
| 224 |
+
def create_nurse(data: NurseCreate, db: Session = next(get_db())):
|
| 225 |
+
nurse = Nurse(**data.dict())
|
| 226 |
+
db.add(nurse)
|
| 227 |
+
db.commit()
|
| 228 |
+
db.refresh(nurse)
|
| 229 |
+
return nurse
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
@app.get("/nurses")
|
| 233 |
+
def list_nurses(db: Session = next(get_db())):
|
| 234 |
+
return db.query(Nurse).all()
|
| 235 |
+
|
| 236 |
+
# =================================================================
|
| 237 |
+
# 8. 護理紀錄 API
|
| 238 |
+
# =================================================================
|
| 239 |
+
@app.post("/records")
|
| 240 |
+
def create_record(data: RecordCreate, db: Session = next(get_db())):
|
| 241 |
+
record = Record(**data.dict())
|
| 242 |
+
db.add(record)
|
| 243 |
+
db.commit()
|
| 244 |
+
db.refresh(record)
|
| 245 |
+
return record
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
@app.get("/records/{patient_id}")
|
| 249 |
+
def list_records(patient_id: int, db: Session = next(get_db())):
|
| 250 |
+
return db.query(Record).filter(Record.patient_id == patient_id).all()
|