Skip to content

Commit 0f56445

Browse files
committed
fix: add context rename functionality
- Add update_context function to backend CRUD - Add PUT endpoint for contexts API - Fixes 405 error when trying to rename contexts
1 parent 1dffd7c commit 0f56445

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

backend/api/v1/contexts.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from uuid import UUID
44
from neo4j import AsyncSession
55

6-
from schemas.context import Context, ContextCreate
6+
from schemas.context import Context, ContextCreate, ContextUpdate
77
from crud import context as context_crud
88
from api.dependencies import get_current_user, get_session
99
from schemas.user import User
@@ -56,6 +56,21 @@ async def get_context(
5656
raise HTTPException(status_code=404, detail="Context not found.")
5757
return context
5858

59+
@contexts_router.put("/{context_id}", response_model=Context)
60+
async def update_context(
61+
project_id: UUID,
62+
context_id: UUID,
63+
context_in: ContextUpdate,
64+
session: AsyncSession = Depends(get_session),
65+
current_user: User = Depends(get_current_user),
66+
):
67+
context = await context_crud.update_context(
68+
session, context_id=context_id, context_in=context_in, project_id=project_id, owner_id=current_user.id
69+
)
70+
if not context:
71+
raise HTTPException(status_code=404, detail="Context not found.")
72+
return context
73+
5974
@contexts_router.delete("/{context_id}", status_code=status.HTTP_204_NO_CONTENT)
6075
async def delete_context(
6176
project_id: UUID,

backend/crud/context.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,35 @@ async def get_context_with_variables(
9494

9595
return Context.model_validate(context_data)
9696

97+
async def update_context(
98+
session: AsyncSession, context_id: UUID, context_in: ContextUpdate, project_id: UUID, owner_id: UUID
99+
) -> Context | None:
100+
props = context_in.model_dump(exclude_unset=True)
101+
if not props:
102+
# If no properties to update, just return the current context
103+
return await get_context_with_variables(session, context_id, project_id, owner_id, include_sensitive=True)
104+
105+
query = """
106+
MATCH (user:User {id: $owner_id})-[:OWNS]->(project:Project {id: $project_id})-[:HAS_CONTEXT]->(context:Context {id: $context_id})
107+
SET context += $props
108+
RETURN context
109+
"""
110+
result = await session.run(
111+
query,
112+
{
113+
"owner_id": str(owner_id),
114+
"project_id": str(project_id),
115+
"context_id": str(context_id),
116+
"props": props,
117+
},
118+
)
119+
record = await result.single()
120+
if not record:
121+
return None
122+
123+
# Return the updated context with variables
124+
return await get_context_with_variables(session, context_id, project_id, owner_id, include_sensitive=True)
125+
97126
async def delete_context(session: AsyncSession, context_id: UUID, project_id: UUID, owner_id: UUID) -> bool:
98127
# Deletes context and all its variables due to DETACH DELETE
99128
query = """

0 commit comments

Comments
 (0)