File size: 2,729 Bytes
fcacf10
 
 
1d92db8
 
34c89e2
1d92db8
34c89e2
fcacf10
1d92db8
 
 
 
 
 
e2532ee
1d92db8
 
 
 
 
 
 
 
 
 
 
 
34c89e2
1d92db8
 
 
 
 
 
34c89e2
1d92db8
 
 
 
 
 
 
 
e2532ee
1d92db8
 
e2532ee
 
fcacf10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from uuid import UUID

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from app.core.dependencies import get_current_user
from app.database.session import get_db
from app.models.user import User
from app.models.workspace import WorkspaceStatus
from app.schemas.workspace import (
    WorkspaceCreate,
    WorkspaceResponse,
)
from app.services.workspace_service import WorkspaceService


router = APIRouter(
    prefix="/workspaces",
    tags=["Workspaces"],
)


@router.post(
    "",
    response_model=WorkspaceResponse,
)
def create_workspace(
    workspace: WorkspaceCreate,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    service = WorkspaceService(db)

    return service.create_workspace(
        workspace,
        current_user.id,
    )


@router.get(
    "",
    response_model=list[WorkspaceResponse],
)
def list_workspaces(
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    return WorkspaceService(db).list_workspaces(
        current_user.id
    )


@router.delete("/{workspace_id}", status_code=204)
def delete_workspace(
    workspace_id: UUID,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    """Soft-delete a workspace. It will no longer appear in listings."""
    service = WorkspaceService(db)
    workspace = service.get_workspace(workspace_id)

    if workspace is None:
        raise HTTPException(status_code=404, detail="Workspace not found.")
    if workspace.created_by != current_user.id:
        raise HTTPException(status_code=403, detail="You do not have access to this workspace.")
    if workspace.status == WorkspaceStatus.DELETED:
        raise HTTPException(status_code=404, detail="Workspace not found.")

    from app.repositories.workspace_repository import WorkspaceRepository
    repo = WorkspaceRepository(db)
    repo.soft_delete(workspace)


@router.post("/{workspace_id}/archive", response_model=WorkspaceResponse)
def archive_workspace(
    workspace_id: UUID,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    """Archive a workspace. It will no longer appear in active listings."""
    service = WorkspaceService(db)
    workspace = service.get_workspace(workspace_id)

    if workspace is None:
        raise HTTPException(status_code=404, detail="Workspace not found.")
    if workspace.created_by != current_user.id:
        raise HTTPException(status_code=403, detail="You do not have access to this workspace.")

    from app.repositories.workspace_repository import WorkspaceRepository
    repo = WorkspaceRepository(db)
    return repo.archive(workspace)