|
| 1 | +import database |
| 2 | +from typing import Annotated |
| 3 | +from database import Template |
| 4 | +from sqlalchemy.orm import Session |
| 5 | +from fastapi import APIRouter, Depends, Query, Header |
| 6 | +from pydantic import BaseModel |
| 7 | +from commons.utils import get_user_from_jwt, verify_user |
| 8 | + |
| 9 | +template_router = APIRouter(prefix="", tags=["template"]) |
| 10 | + |
| 11 | + |
| 12 | +class TemplateModel(BaseModel): |
| 13 | + name: str = None |
| 14 | + dag: dict = None |
| 15 | + description: str = None |
| 16 | + |
| 17 | + |
| 18 | +@template_router.get("/templates", status_code=200) |
| 19 | +def get_all_templates(token: Annotated[str, Header()], db: Session = Depends(database.db_session)): |
| 20 | + verify_user(get_user_from_jwt(token)) |
| 21 | + templates = db.query(Template).all() |
| 22 | + response = {"msg": "success", "templates": [template.to_dict() for template in templates]} |
| 23 | + return response |
| 24 | + |
| 25 | + |
| 26 | +@template_router.get("/template/{id}", status_code=200) |
| 27 | +def get_template_by_id(id: int, token: Annotated[str, Header()], db: Session = Depends(database.db_session)): |
| 28 | + verify_user(get_user_from_jwt(token)) |
| 29 | + template: Template = db.query(Template).filter(Template.id == id).first() # type: ignore |
| 30 | + response = {"msg": "success", "template": template.to_dict()} |
| 31 | + return response |
| 32 | + |
| 33 | + |
| 34 | +@template_router.post("/template", status_code=200) |
| 35 | +def create_template(inputs: TemplateModel, token: Annotated[str, Header()], db: Session = Depends(database.db_session)): |
| 36 | + verify_user(get_user_from_jwt(token)) |
| 37 | + template = Template(name=inputs.name, description=inputs.description, dag=inputs.dag) |
| 38 | + db.add(template) |
| 39 | + db.commit() |
| 40 | + response = {"msg": "success", "template": template.to_dict()} |
| 41 | + return response |
0 commit comments