From 7f7d625fdd3c1f25d6a04d916beaf9648e050e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=20=E6=9F=8F?= Date: Sat, 23 Sep 2023 06:38:00 +0000 Subject: [PATCH] backend merge --- backend/app/api/api_v1/routers/platform.py | 62 +++++++++++++++++++++- backend/app/db/crud.py | 60 ++++++++++++++++++++- backend/app/db/models.py | 34 +++++++++--- backend/app/db/schemas.py | 34 ++++++++++++ 4 files changed, 180 insertions(+), 10 deletions(-) diff --git a/backend/app/api/api_v1/routers/platform.py b/backend/app/api/api_v1/routers/platform.py index 358aaab..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 +from app.db.schemas import * platform_router = r = APIRouter() @@ -64,4 +64,62 @@ async def kintone_data( db=Depends(get_db), ): kintone = get_kintones(db, type) - return kintone \ No newline at end of file + return kintone + +@r.get( + "/actions", + response_model=t.List[Action], + response_model_exclude={"id"}, + response_model_exclude_none=True, +) +async def action_data( + request: Request, + db=Depends(get_db), +): + actions = get_actions(db) + 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 08614f4..b0cf96d 100644 --- a/backend/app/db/crud.py +++ b/backend/app/db/crud.py @@ -115,4 +115,62 @@ def get_kintones(db: Session, type: int): kintones = db.query(models.Kintone).filter(models.Kintone.type == type).all() if not kintones: raise HTTPException(status_code=404, detail="Data not found") - return kintones \ No newline at end of file + return kintones + +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 + + +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 0e6812b..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,15 +21,31 @@ 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) - content = Column(String) \ No newline at end of file + content = Column(String) + +class Action(Base): + __tablename__ = "action" + + name = Column(String(100), index=True, nullable=False) + title = Column(String(200)) + subtitle = Column(String(500)) + outputpoints = Column(String) + 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 5d106c7..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 @@ -67,5 +72,34 @@ class Kintone(BaseModel): desc: str = None content: str = None + class Config: + orm_mode = True + +class Action(BaseModel): + id: int + name: str = None + title: str = None + subtitle: str = None + 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