Spaces:
Sleeping
Sleeping
Update api_server.py
Browse files- api_server.py +9 -9
api_server.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# api_server.py
|
| 2 |
|
| 3 |
-
from fastapi import FastAPI, HTTPException
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
|
@@ -148,7 +148,7 @@ SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
| 148 |
# 建立資料表
|
| 149 |
Base.metadata.create_all(bind=engine)
|
| 150 |
|
| 151 |
-
def get_db
|
| 152 |
db = SessionLocal()
|
| 153 |
try:
|
| 154 |
yield db
|
|
@@ -201,7 +201,7 @@ class RecordCreate(BaseModel):
|
|
| 201 |
# 6. 病患 API
|
| 202 |
# =================================================================
|
| 203 |
@app.post("/patients")
|
| 204 |
-
def create_patient(data: PatientCreate, db: Session = Depends(get_db
|
| 205 |
patient = Patient(**data.dict())
|
| 206 |
db.add(patient)
|
| 207 |
db.commit()
|
|
@@ -210,12 +210,12 @@ def create_patient(data: PatientCreate, db: Session = Depends(get_db())):
|
|
| 210 |
|
| 211 |
|
| 212 |
@app.get("/patients")
|
| 213 |
-
def list_patients(db: Session = Depends(get_db
|
| 214 |
return db.query(Patient).all()
|
| 215 |
|
| 216 |
|
| 217 |
@app.get("/patients/{patient_id}")
|
| 218 |
-
def get_patient(patient_id: int, db: Session = Depends(get_db
|
| 219 |
patient = db.query(Patient).filter(Patient.id == patient_id).first()
|
| 220 |
if not patient:
|
| 221 |
raise HTTPException(status_code=404, detail="找不到病患")
|
|
@@ -225,7 +225,7 @@ def get_patient(patient_id: int, db: Session = Depends(get_db())):
|
|
| 225 |
# 7. 護士 API
|
| 226 |
# =================================================================
|
| 227 |
@app.post("/nurses")
|
| 228 |
-
def create_nurse(data: NurseCreate, db: Session = Depends(get_db
|
| 229 |
nurse = Nurse(**data.dict())
|
| 230 |
db.add(nurse)
|
| 231 |
db.commit()
|
|
@@ -234,14 +234,14 @@ def create_nurse(data: NurseCreate, db: Session = Depends(get_db())):
|
|
| 234 |
|
| 235 |
|
| 236 |
@app.get("/nurses")
|
| 237 |
-
def list_nurses(db: Session = Depends(get_db
|
| 238 |
return db.query(Nurse).all()
|
| 239 |
|
| 240 |
# =================================================================
|
| 241 |
# 8. 護理紀錄 API
|
| 242 |
# =================================================================
|
| 243 |
@app.post("/records")
|
| 244 |
-
def create_record(data: RecordCreate, db: Session = Depends(get_db
|
| 245 |
record = Record(**data.dict())
|
| 246 |
db.add(record)
|
| 247 |
db.commit()
|
|
@@ -250,5 +250,5 @@ def create_record(data: RecordCreate, db: Session = Depends(get_db())):
|
|
| 250 |
|
| 251 |
|
| 252 |
@app.get("/records/{patient_id}")
|
| 253 |
-
def list_records(patient_id: int, db: Session = Depends(get_db
|
| 254 |
return db.query(Record).filter(Record.patient_id == patient_id).all()
|
|
|
|
| 1 |
# api_server.py
|
| 2 |
|
| 3 |
+
from fastapi import FastAPI, HTTPException, Depends
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
|
|
|
| 148 |
# 建立資料表
|
| 149 |
Base.metadata.create_all(bind=engine)
|
| 150 |
|
| 151 |
+
def get_db:
|
| 152 |
db = SessionLocal()
|
| 153 |
try:
|
| 154 |
yield db
|
|
|
|
| 201 |
# 6. 病患 API
|
| 202 |
# =================================================================
|
| 203 |
@app.post("/patients")
|
| 204 |
+
def create_patient(data: PatientCreate, db: Session = Depends(get_db)):
|
| 205 |
patient = Patient(**data.dict())
|
| 206 |
db.add(patient)
|
| 207 |
db.commit()
|
|
|
|
| 210 |
|
| 211 |
|
| 212 |
@app.get("/patients")
|
| 213 |
+
def list_patients(db: Session = Depends(get_db)):
|
| 214 |
return db.query(Patient).all()
|
| 215 |
|
| 216 |
|
| 217 |
@app.get("/patients/{patient_id}")
|
| 218 |
+
def get_patient(patient_id: int, db: Session = Depends(get_db)):
|
| 219 |
patient = db.query(Patient).filter(Patient.id == patient_id).first()
|
| 220 |
if not patient:
|
| 221 |
raise HTTPException(status_code=404, detail="找不到病患")
|
|
|
|
| 225 |
# 7. 護士 API
|
| 226 |
# =================================================================
|
| 227 |
@app.post("/nurses")
|
| 228 |
+
def create_nurse(data: NurseCreate, db: Session = Depends(get_db)):
|
| 229 |
nurse = Nurse(**data.dict())
|
| 230 |
db.add(nurse)
|
| 231 |
db.commit()
|
|
|
|
| 234 |
|
| 235 |
|
| 236 |
@app.get("/nurses")
|
| 237 |
+
def list_nurses(db: Session = Depends(get_db)):
|
| 238 |
return db.query(Nurse).all()
|
| 239 |
|
| 240 |
# =================================================================
|
| 241 |
# 8. 護理紀錄 API
|
| 242 |
# =================================================================
|
| 243 |
@app.post("/records")
|
| 244 |
+
def create_record(data: RecordCreate, db: Session = Depends(get_db)):
|
| 245 |
record = Record(**data.dict())
|
| 246 |
db.add(record)
|
| 247 |
db.commit()
|
|
|
|
| 250 |
|
| 251 |
|
| 252 |
@app.get("/records/{patient_id}")
|
| 253 |
+
def list_records(patient_id: int, db: Session = Depends(get_db)):
|
| 254 |
return db.query(Record).filter(Record.patient_id == patient_id).all()
|