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 aa19731 commit acd3c25
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions api/manage_plan.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List

from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from sqlalchemy import text
Expand All @@ -24,6 +26,11 @@ class Config:
orm_mode = True


class PlanResp(BaseModel):
meal_plan: List[PlanRecvBase]
recovery_plan: List[PlanRecvBase]


def create_plan(db, plan_recv):
create_plan_sql = ''
if plan_recv.plan_category == "meal_plan":
Expand Down Expand Up @@ -66,7 +73,7 @@ def delete_plan(db, plan_recv):
raise HTTPException(status_code=500, detail=str(e))


def get_plans(db, plan_category) -> list[dict]:
def get_plans(db, plan_category):
get_plans_sql = ''
if plan_category == "meal_plan":
get_plans_sql = text(
Expand All @@ -82,7 +89,8 @@ def get_plans(db, plan_category) -> list[dict]:
except SQLAlchemyError as e:
db.rollback()
raise HTTPException(status_code=500, detail=str(e))
return [dict(row) for row in res]
return [PlanRecvBase(**dict(row)) for row in res]


# add new plan
@router.post("/new_plan")
Expand Down Expand Up @@ -118,15 +126,10 @@ async def delete_plan(plan_recv: PlanRecv, current_user: User = Depends(get_curr
}


@router.get("/get_plans")
@router.get("/get_plans", response_model=PlanResp)
async def get_plans(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 get plans")
meal_plan_res = get_plans(db, "meal_plan")
recovery_plan_res = get_plans(db, "recovery_plan")
return {
"meal_plan": meal_plan_res,
"recovery_plan": recovery_plan_res,
"status": "success",
"details": "get plans success"
}
return PlanResp(meal_plan=meal_plan_res, recovery_plan=recovery_plan_res)

0 comments on commit acd3c25

Please sign in to comment.