Skip to content

Commit

Permalink
manage_plan.py
Browse files Browse the repository at this point in the history
  • Loading branch information
WSL0809 committed Apr 12, 2024
1 parent d0ba31e commit a6fbd57
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions api/manage_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class PlanRecvBase(BaseModel):
duration: int


class PlanCreate(BaseModel):
plan_category: str
details: str
duration: int


class PlanRecv(PlanRecvBase):
plan_category: str

Expand All @@ -31,21 +37,21 @@ class PlanResp(BaseModel):
recovery_plan: List[PlanRecvBase]


def create_plan(db, plan_recv):
def create_plan_in_db(db, plan_create):
create_plan_sql = ''
if plan_recv.plan_category == "meal_plan":
if plan_create.plan_category == "meal_plan":
create_plan_sql = text(
"INSERT INTO meal_plan (details, duration) VALUES (:details, :duration)"
)
elif plan_recv.plan_category == "recovery_plan":
elif plan_create.plan_category == "recovery_plan":
create_plan_sql = text(
"INSERT INTO recovery_plan (details, duration) VALUES (:details, :duration)"
)

plan_info = dict(plan_recv).copy()
plan_info = dict(plan_create).copy()
plan_info.pop("plan_category", None)
try:
db.execute(create_plan_sql, plan_recv)
db.execute(create_plan_sql, plan_create)
db.commit()
except SQLAlchemyError as e:
db.rollback()
Expand Down Expand Up @@ -92,14 +98,13 @@ def get_plans_in_db(db, plan_category):
raise HTTPException(status_code=500, detail=str(e))



# add new plan
@router.post("/new_plan")
async def new_plan(plan_recv: PlanRecv, current_user: User = Depends(get_current_active_user),
@router.post("/create_plan")
async def create_plan(plan_create: PlanCreate, 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 add new plan")
await create_plan(db, plan_recv)
create_plan_in_db(db, plan_create)

return {
"status": "success",
Expand Down

0 comments on commit a6fbd57

Please sign in to comment.