Add backend/app/routers/workspaces.py
This commit is contained in:
@@ -0,0 +1,54 @@
|
|||||||
|
"""Workspace routes."""
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from app.database import get_db
|
||||||
|
from app.models.workspace import Workspace
|
||||||
|
from app.schemas.workspace import WorkspaceCreate, WorkspaceUpdate, WorkspaceResponse
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/workspaces", tags=["workspaces"])
|
||||||
|
|
||||||
|
@router.get("", response_model=List[WorkspaceResponse])
|
||||||
|
def list_workspaces(db: Session = Depends(get_db)):
|
||||||
|
"""List all workspaces."""
|
||||||
|
return db.query(Workspace).all()
|
||||||
|
|
||||||
|
@router.post("", response_model=WorkspaceResponse)
|
||||||
|
def create_workspace(workspace: WorkspaceCreate, db: Session = Depends(get_db)):
|
||||||
|
"""Create new workspace."""
|
||||||
|
db_workspace = Workspace(**workspace.model_dump())
|
||||||
|
db.add(db_workspace)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_workspace)
|
||||||
|
return db_workspace
|
||||||
|
|
||||||
|
@router.get("/{workspace_id}", response_model=WorkspaceResponse)
|
||||||
|
def get_workspace(workspace_id: int, db: Session = Depends(get_db)):
|
||||||
|
"""Get workspace by ID."""
|
||||||
|
workspace = db.query(Workspace).filter(Workspace.id == workspace_id).first()
|
||||||
|
if not workspace:
|
||||||
|
raise HTTPException(status_code=404, detail="Workspace not found")
|
||||||
|
return workspace
|
||||||
|
|
||||||
|
@router.put("/{workspace_id}", response_model=WorkspaceResponse)
|
||||||
|
def update_workspace(workspace_id: int, workspace: WorkspaceUpdate, db: Session = Depends(get_db)):
|
||||||
|
"""Update workspace."""
|
||||||
|
db_workspace = db.query(Workspace).filter(Workspace.id == workspace_id).first()
|
||||||
|
if not db_workspace:
|
||||||
|
raise HTTPException(status_code=404, detail="Workspace not found")
|
||||||
|
for key, value in workspace.model_dump(exclude_unset=True).items():
|
||||||
|
setattr(db_workspace, key, value)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_workspace)
|
||||||
|
return db_workspace
|
||||||
|
|
||||||
|
@router.delete("/{workspace_id}")
|
||||||
|
def delete_workspace(workspace_id: int, db: Session = Depends(get_db)):
|
||||||
|
"""Delete workspace."""
|
||||||
|
workspace = db.query(Workspace).filter(Workspace.id == workspace_id).first()
|
||||||
|
if not workspace:
|
||||||
|
raise HTTPException(status_code=404, detail="Workspace not found")
|
||||||
|
db.delete(workspace)
|
||||||
|
db.commit()
|
||||||
|
return {"message": "Workspace deleted"}
|
||||||
Reference in New Issue
Block a user