From 6aa057e590479a83bc6b6264da41d5af401bc4a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=20=E6=9F=8F?= Date: Thu, 21 Sep 2023 07:22:12 +0000 Subject: [PATCH] flow api add --- backend/app/api/api_v1/routers/platform.py | 49 +++++++++++++++++++- backend/app/db/crud.py | 54 +++++++++++++++++++++- backend/app/db/models.py | 27 +++++++---- backend/app/db/schemas.py | 23 +++++++++ 4 files changed, 142 insertions(+), 11 deletions(-) diff --git a/backend/app/api/api_v1/routers/platform.py b/backend/app/api/api_v1/routers/platform.py index e70655f..c6f4c8f 100644 --- a/backend/app/api/api_v1/routers/platform.py +++ b/backend/app/api/api_v1/routers/platform.py @@ -2,7 +2,7 @@ from fastapi import Request,Depends, APIRouter, UploadFile,HTTPException,File from app.db import Base,engine from app.db.session import get_db from app.db.crud import * -from app.db.schemas import AppBase, AppEdit, App,Kintone,Action +from app.db.schemas import * platform_router = r = APIRouter() @@ -77,4 +77,49 @@ async def action_data( db=Depends(get_db), ): actions = get_actions(db) - return actions \ No newline at end of file + return actions + +@r.get( + "/flow/{flowid}", + response_model=Flow, + response_model_exclude_none=True, +) +async def flow_details( + request: Request, + flowid: str, + db=Depends(get_db), +): + app = get_flow(db, flowid) + return app + + +@r.post("/flow", response_model=Flow, response_model_exclude_none=True) +async def flow_create( + request: Request, + flow: FlowBase, + db=Depends(get_db), +): + return create_flow(db, flow) + + +@r.put( + "/flow/{flowid}", response_model=Flow, response_model_exclude_none=True +) +async def flow_edit( + request: Request, + flow: FlowBase, + db=Depends(get_db), +): + return edit_flow(db, flow) + + +@r.delete( + "/flow/{flowid}", response_model=Flow, response_model_exclude_none=True +) +async def flow_delete( + request: Request, + flowid: str, + db=Depends(get_db), +): + + return delete_flow(db, flowid) \ No newline at end of file diff --git a/backend/app/db/crud.py b/backend/app/db/crud.py index d7a6fa1..b0cf96d 100644 --- a/backend/app/db/crud.py +++ b/backend/app/db/crud.py @@ -121,4 +121,56 @@ def get_actions(db: Session): actions = db.query(models.Action).all() if not actions: raise HTTPException(status_code=404, detail="Data not found") - return actions \ No newline at end of file + return actions + + +def create_flow(db: Session, flow: schemas.FlowBase): + db_flow = models.Flow( + flowid=flow.flowid, + appid=flow.appid, + eventid=flow.eventid, + name=flow.name, + content=flow.content + ) + db.add(db_flow) + db.commit() + db.refresh(db_flow) + return db_flow + +def delete_flow(db: Session, flowid: str): + flow = get_flow(db, flowid) + if not flow: + raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Flow not found") + db.delete(flow) + db.commit() + return flow + + +def edit_flow( + db: Session, flow: schemas.FlowBase +) -> schemas.Flow: + db_flow = get_flow(db, flow.flowid) + if not db_flow: + raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Flow not found") + update_data = flow.dict(exclude_unset=True) + + for key, value in update_data.items(): + setattr(db_flow, key, value) + + db.add(db_flow) + db.commit() + db.refresh(db_flow) + return db_flow + + +def get_flows(db: Session, flowid: str): + flows = db.query(models.Flow).all() + if not flows: + raise HTTPException(status_code=404, detail="Data not found") + return flows + +def get_flow(db: Session, flowid: str): + flow = db.query(models.Flow).filter(models.Flow.flowid == flowid).first() + if not flow: + raise HTTPException(status_code=404, detail="Data not found") + return flow \ No newline at end of file diff --git a/backend/app/db/models.py b/backend/app/db/models.py index aff432d..a88f28c 100644 --- a/backend/app/db/models.py +++ b/backend/app/db/models.py @@ -1,12 +1,16 @@ -from sqlalchemy import Boolean, Column, Integer, String - -from .session import Base +from sqlalchemy import Boolean, Column, Integer, String, DateTime +from sqlalchemy.ext.declarative import as_declarative +from datetime import datetime +@as_declarative() +class Base: + id = Column(Integer, primary_key=True, index=True) + create_time = Column(DateTime, default=datetime.now) + update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now) class User(Base): __tablename__ = "user" - id = Column(Integer, primary_key=True, index=True) email = Column(String(50), unique=True, index=True, nullable=False) first_name = Column(String(100)) last_name = Column(String(100)) @@ -17,14 +21,12 @@ class User(Base): class AppSetting(Base): __tablename__ = "appsetting" - id = Column(Integer, primary_key=True, index=True) appid = Column(String(100), index=True, nullable=False) setting = Column(String(1000)) class Kintone(Base): __tablename__ = "kintone" - id = Column(Integer, primary_key=True, index=True) type = Column(Integer, index=True, nullable=False) name = Column(String(100), nullable=False) desc = Column(String) @@ -32,9 +34,18 @@ class Kintone(Base): class Action(Base): __tablename__ = "action" - id = Column(Integer, primary_key=True, index=True) + name = Column(String(100), index=True, nullable=False) title = Column(String(200)) subtitle = Column(String(500)) outputpoints = Column(String) - property = Column(String) \ No newline at end of file + property = Column(String) + +class Flow(Base): + __tablename__ = "flow" + + flowid = Column(String(100), index=True, nullable=False) + appid = Column(String(100), index=True, nullable=False) + eventid = Column(String(100), index=True, nullable=False) + name = Column(String(200)) + content = Column(String) \ No newline at end of file diff --git a/backend/app/db/schemas.py b/backend/app/db/schemas.py index acada8e..02c6483 100644 --- a/backend/app/db/schemas.py +++ b/backend/app/db/schemas.py @@ -1,7 +1,12 @@ from pydantic import BaseModel +from datetime import datetime import typing as t +class Base(BaseModel): + create_time: datetime + update_time: datetime + class UserBase(BaseModel): email: str is_active: bool = True @@ -78,5 +83,23 @@ class Action(BaseModel): outputpoints: str = None property: str = None + class Config: + orm_mode = True + +class FlowBase(BaseModel): + flowid: str + appid: str + eventid: str + name: str = None + content: str = None + +class Flow(Base): + id: int + flowid: str + appid: str + eventid: str + name: str = None + content: str = None + class Config: orm_mode = True \ No newline at end of file