feat: add workspace
This commit is contained in:
28
src/usecase/workspace/create_workspace.py
Normal file
28
src/usecase/workspace/create_workspace.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import uuid
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from src import domain, dto
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import Usecase
|
||||
|
||||
|
||||
async def create_workspace(
|
||||
self: 'Usecase', user_id: uuid.UUID, input: dto.CreateWorkspaceInput
|
||||
) -> dto.CreateWorkspaceOutput:
|
||||
user = await self.database.get_user(user_id=user_id)
|
||||
if not user:
|
||||
raise domain.UserNotFound(user_id)
|
||||
|
||||
workspace = domain.Workspace(
|
||||
name=input.name,
|
||||
)
|
||||
|
||||
async with self.database.transaction():
|
||||
await self.database.create_workspace(workspace)
|
||||
await self.database.add_user_to_workspace(workspace.id, user_id)
|
||||
|
||||
return dto.CreateWorkspaceOutput(
|
||||
id=workspace.id,
|
||||
name=workspace.name,
|
||||
)
|
||||
11
src/usecase/workspace/delete_workspace.py
Normal file
11
src/usecase/workspace/delete_workspace.py
Normal file
@@ -0,0 +1,11 @@
|
||||
import uuid
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import Usecase
|
||||
|
||||
|
||||
async def delete_workspace(self: 'Usecase', workspace_id: uuid.UUID, user_id: uuid.UUID) -> None:
|
||||
self.ensure_workspace_access(workspace_id, user_id)
|
||||
|
||||
await self.database.delete_workspace(workspace_id)
|
||||
21
src/usecase/workspace/get_workspaces.py
Normal file
21
src/usecase/workspace/get_workspaces.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import uuid
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from src import dto
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import Usecase
|
||||
|
||||
|
||||
async def get_workspaces(self: 'Usecase', user_id: uuid.UUID) -> dto.GetWorkspacesOutput:
|
||||
memberships = await self.database.get_user_workspaces(user_id)
|
||||
|
||||
return dto.GetWorkspacesOutput(
|
||||
workspaces=[
|
||||
dto.WorkspaceMembershipOutput(
|
||||
id=membership.workspace_id,
|
||||
name=membership.workspace.name,
|
||||
)
|
||||
for membership in memberships
|
||||
]
|
||||
)
|
||||
26
src/usecase/workspace/update_workspace.py
Normal file
26
src/usecase/workspace/update_workspace.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import uuid
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from src import domain, dto
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .. import Usecase
|
||||
|
||||
|
||||
async def update_workspace(
|
||||
self: 'Usecase', workspace_id: uuid.UUID, user_id: uuid.UUID, input: dto.UpdateWorkspaceInput
|
||||
) -> dto.WorkspaceMembershipOutput:
|
||||
membership = await self.database.get_workspace_membership(workspace_id, user_id)
|
||||
if not membership:
|
||||
raise domain.WorkspaceNotFound(workspace_id)
|
||||
|
||||
workspace = membership.workspace
|
||||
|
||||
if input.name is not None:
|
||||
workspace.name = input.name
|
||||
await self.database.update_workspace(workspace)
|
||||
|
||||
return dto.WorkspaceMembershipOutput(
|
||||
id=workspace.id,
|
||||
name=workspace.name,
|
||||
)
|
||||
Reference in New Issue
Block a user