import json from fastapi import APIRouter, Response, status from pydantic import BaseModel from pydantic.utils import Optional import backend.postgres as postgres from api.middleware.verify_token import VerifyTokenRoute tables_routes = APIRouter(route_class=VerifyTokenRoute) class Table(BaseModel): name: Optional[str] column: Optional[str] type: Optional[str] column_data: Optional[list] row_number: Optional[str] columns: Optional[list] columns_data: Optional[list] class Sort(BaseModel): property: str order: str priority: str class Filter(BaseModel): property: str value: str function: str @tables_routes.post("/tables") def create_table(aux: Table, response: Response): postgres.create_table(aux.name) response.status_code = status.HTTP_201_CREATED @tables_routes.put("/tables/{name}") def edit_table(aux: Table, name: str): if aux.column is not None and aux.type is not None: postgres.add_column(name, aux.column, aux.type) if aux.column_data is not None: postgres.insert_columns(name, aux.column_data) if aux.row_number is not None: postgres.edit_columns(name, aux.columns, aux.columns_data, aux.row_number) @tables_routes.post("/tables/{name}/sort") def create_sort(name: str, response: Response): postgres.create_sort(name) response.status_code = status.HTTP_201_CREATED @tables_routes.put("/tables/{name}/sort") def add_sort(aux: Sort, name: str): postgres.add_sort(name, aux.property, aux.order, aux.priority) @tables_routes.get("/tables/{name}/sort") def sort(name: str): return postgres.sort(name) @tables_routes.post("/tables/{name}/filter") def create_filter(name: str, response: Response): postgres.create_filter(name) postgres.add_filter_trigger(name) response.status_code = status.HTTP_201_CREATED @tables_routes.put("/tables/{name}/filter") def add_filter(aux: Filter, name: str): postgres.add_filter(name, aux.property, aux.value, aux.function) @tables_routes.get("/tables/{name}/filter") def filter(name: str): return postgres.filter(name)