MarcoLeung052 commited on
Commit
9fce3dc
·
verified ·
1 Parent(s): 948e704

Update api_server.py

Browse files
Files changed (1) hide show
  1. api_server.py +52 -1
api_server.py CHANGED
@@ -161,6 +161,11 @@ def get_db():
161
  # =================================================================
162
  from pydantic import BaseModel
163
 
 
 
 
 
 
164
  # 病患
165
  class PatientCreate(BaseModel):
166
  name: str
@@ -202,6 +207,7 @@ class RecordCreate(BaseModel):
202
  # =================================================================
203
  # 6. 病患 API
204
  # =================================================================
 
205
  @app.post("/patients")
206
  def create_patient(data: PatientCreate, db: Session = Depends(get_db)):
207
  patient = Patient(**data.dict())
@@ -250,13 +256,16 @@ def delete_patient(patient_id: int, db: Session = Depends(get_db)):
250
  # =================================================================
251
  @app.post("/nurses")
252
  def create_nurse(data: NurseCreate, db: Session = Depends(get_db)):
253
- nurse = Nurse(**data.dict())
 
 
254
  db.add(nurse)
255
  db.commit()
256
  db.refresh(nurse)
257
  return nurse
258
 
259
 
 
260
  @app.get("/nurses")
261
  def list_nurses(db: Session = Depends(get_db)):
262
  return db.query(Nurse).all()
@@ -300,6 +309,48 @@ def delete_record(record_id: int, db: Session = Depends(get_db)):
300
  db.commit()
301
  return {"message": "紀錄已刪除"}
302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
  from fastapi.responses import FileResponse
304
  @app.get("/download-db")
305
  def download_db():
 
161
  # =================================================================
162
  from pydantic import BaseModel
163
 
164
+ # 管理員
165
+ class LoginRequest(BaseModel):
166
+ username: str
167
+ password: str
168
+
169
  # 病患
170
  class PatientCreate(BaseModel):
171
  name: str
 
207
  # =================================================================
208
  # 6. 病患 API
209
  # =================================================================
210
+
211
  @app.post("/patients")
212
  def create_patient(data: PatientCreate, db: Session = Depends(get_db)):
213
  patient = Patient(**data.dict())
 
256
  # =================================================================
257
  @app.post("/nurses")
258
  def create_nurse(data: NurseCreate, db: Session = Depends(get_db)):
259
+ nurse_data = data.dict()
260
+ nurse_data["password"] = data.staff_id # 預設密碼 = staff_id
261
+ nurse = Nurse(**nurse_data)
262
  db.add(nurse)
263
  db.commit()
264
  db.refresh(nurse)
265
  return nurse
266
 
267
 
268
+
269
  @app.get("/nurses")
270
  def list_nurses(db: Session = Depends(get_db)):
271
  return db.query(Nurse).all()
 
309
  db.commit()
310
  return {"message": "紀錄已刪除"}
311
 
312
+ @app.post("/login")
313
+ def login(data: LoginRequest, db: Session = Depends(get_db)):
314
+
315
+ # 管理員登入
316
+ if data.username == "admin" and data.password == "1234":
317
+ return {"role": "admin", "name": "Admin", "id": 0}
318
+
319
+ # 護理師登入
320
+ nurse = db.query(Nurse).filter(Nurse.staff_id == data.username).first()
321
+
322
+ if not nurse:
323
+ raise HTTPException(status_code=401, detail="帳號不存在")
324
+
325
+ if nurse.password != data.password:
326
+ raise HTTPException(status_code=401, detail="密碼錯誤")
327
+
328
+ return {
329
+ "role": "nurse",
330
+ "name": nurse.name,
331
+ "id": nurse.id
332
+ }
333
+
334
+ @app.get("/current-user")
335
+ def current_user(token: str | None = None, db: Session = Depends(get_db)):
336
+
337
+ if token == "admin":
338
+ return {"role": "admin", "name": "Admin", "id": 0}
339
+
340
+ if token is None:
341
+ raise HTTPException(status_code=401, detail="未登入")
342
+
343
+ nurse = db.query(Nurse).filter(Nurse.id == int(token)).first()
344
+
345
+ if not nurse:
346
+ raise HTTPException(status_code=401, detail="登入者不存在")
347
+
348
+ return {
349
+ "role": "nurse",
350
+ "name": nurse.name,
351
+ "id": nurse.id
352
+ }
353
+
354
  from fastapi.responses import FileResponse
355
  @app.get("/download-db")
356
  def download_db():