Skip to content

Commit

Permalink
insert_client.py
Browse files Browse the repository at this point in the history
  • Loading branch information
WSL0809 committed Apr 9, 2024
1 parent ae1c657 commit 035261a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
76 changes: 76 additions & 0 deletions api/insert_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import json

from datetime import datetime
from typing import Optional, Union, Any, Dict

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import text
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from pydantic import BaseModel
from starlette import status

from api.utils import exception_handler
from auth import get_current_active_user
from auth_schema import User
from database import get_db
from config import ClientStatus

router = APIRouter()


class InsertClientRecv(BaseModel):
id_number: str
name: str
tel: str
age: int
scheduled_date: str
check_in_date: Union[str, None] = None
hospital_for_childbirth: str
contact_name: str
contact_tel: str
meal_plan_id: int
recovery_plan_id: Optional[int] = None
mode_of_delivery: str
assigned_baby_nurse: Optional[int] = None
room: Union[str, None] = None
meal_plan_seller: Union[Dict, None] = {}
recovery_plan_seller: Union[Dict, None] = {}
due_date: Union[str, None] = None

class Config:
orm_mode = True


def update_client_and_room(db, insert_client_recv: InsertClientRecv):
create_client_sql = text(
"""
INSERT INTO client (name, tel, age, scheduled_date, check_in_date, hospital_for_childbirth, contact_name, contact_tel, mode_of_delivery, room, meal_plan_id, recovery_plan_id, assigned_baby_nurse, id_number, status, meal_plan_seller, recovery_plan_seller, due_date)
VALUES (:name, :tel, :age, :scheduled_date, :check_in_date, :hospital_for_childbirth, :contact_name, :contact_tel, :mode_of_delivery, :room, :meal_plan_id, :recovery_plan_id, :assigned_baby_nurse, :id_number, :status, :meal_plan_seller, :recovery_plan_seller, :due_date)
"""
)

reserve_recv_dict = dict(insert_client_recv)
reserve_recv_dict["meal_plan_seller"] = json.dumps(insert_client_recv.meal_plan_seller)
reserve_recv_dict["recovery_plan_seller"] = json.dumps(insert_client_recv.recovery_plan_seller)
reserve_recv_dict["status"] = ClientStatus.manual_create.value
try:
db.execute(create_client_sql, reserve_recv_dict)
db.flush()
db.commit()
except SQLAlchemyError as e:
db.rollback()
raise e


@router.post("/insert_client")
async def insert_client(insert_client_recv: InsertClientRecv, current_user: User = Depends(get_current_active_user),
db: Session = Depends(get_db)):
if current_user.role != "admin":
raise HTTPException(status_code=401, detail="only admin can insert client")
update_client_and_room(db, insert_client_recv)

return {
"status": "success",
"details": "insert client success"
}
5 changes: 3 additions & 2 deletions api/reserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ReserveRecv(BaseModel):
room: str
meal_plan_seller: Union[Dict, None] = {}
recovery_plan_seller: Union[Dict, None] = {}
due_date: Union[str, None] = None

class Config:
orm_mode = True
Expand All @@ -53,8 +54,8 @@ def update_client_and_room(db, reserve_recv: ReserveRecv):
# db_client = model.Client(**reserve_recv.dict())
create_client_sql = text(
"""
INSERT INTO client (name, tel, age, scheduled_date, check_in_date, hospital_for_childbirth, contact_name, contact_tel, mode_of_delivery, room, meal_plan_id, recovery_plan_id, assigned_baby_nurse, id_number, status, meal_plan_seller, recovery_plan_seller)
VALUES (:name, :tel, :age, :scheduled_date, :check_in_date, :hospital_for_childbirth, :contact_name, :contact_tel, :mode_of_delivery, :room, :meal_plan_id, :recovery_plan_id, :assigned_baby_nurse, :id_number, 0, :meal_plan_seller, :recovery_plan_seller)
INSERT INTO client (name, tel, age, scheduled_date, check_in_date, hospital_for_childbirth, contact_name, contact_tel, mode_of_delivery, room, meal_plan_id, recovery_plan_id, assigned_baby_nurse, id_number, status, meal_plan_seller, recovery_plan_seller, due_date)
VALUES (:name, :tel, :age, :scheduled_date, :check_in_date, :hospital_for_childbirth, :contact_name, :contact_tel, :mode_of_delivery, :room, :meal_plan_id, :recovery_plan_id, :assigned_baby_nurse, :id_number, 0, :meal_plan_seller, :recovery_plan_seller, :due_date)
RETURNING id
"""
)
Expand Down
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class RoomStatus(Enum):
class ClientStatus(Enum):
in_there = 0
out = 1
# 手动创建
manual_create = 2


class BabyNurseWorkStatus(Enum):
Expand Down
3 changes: 1 addition & 2 deletions model/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class Client(Base):
status = Column(Integer, default=0)
meal_plan_seller = Column(JSONB)
recovery_plan_seller = Column(JSONB)


due_date = Column(String(255))
babies = relationship("Baby", back_populates="client")


Expand Down

0 comments on commit 035261a

Please sign in to comment.