55
66from fastapi import APIRouter , Depends , HTTPException , Query
77
8- from api .projects import MOCK_PROJECTS
98from middleware .auth_middleware import verify_token
109from models .response_schemas import (
1110 ApiResponse ,
1716 SendMessageRequest ,
1817 SendMessageResponse ,
1918)
19+ from services .project_service import get_project_service
2020
2121router = APIRouter (prefix = "/chat" , tags = ["chat" ])
22+ project_service = get_project_service ()
2223
2324# Mock chat messages database
2425MOCK_CHAT_MESSAGES = {}
@@ -241,12 +242,15 @@ async def send_message(
241242 """Send message and get query results"""
242243
243244 # Verify project exists and user has access
244- if project_id not in MOCK_PROJECTS :
245- raise HTTPException (status_code = 404 , detail = "Project not found" )
245+ try :
246+ user_uuid = uuid .UUID (user_id )
247+ project_uuid = uuid .UUID (project_id )
246248
247- project_data = MOCK_PROJECTS [project_id ]
248- if project_data ["user_id" ] != user_id :
249- raise HTTPException (status_code = 403 , detail = "Access denied" )
249+ if not project_service .check_project_ownership (project_uuid , user_uuid ):
250+ raise HTTPException (status_code = 404 , detail = "Project not found" )
251+
252+ except ValueError :
253+ raise HTTPException (status_code = 400 , detail = "Invalid project ID" )
250254
251255 # Create user message
252256 user_message = ChatMessage (
@@ -293,12 +297,15 @@ async def get_messages(
293297 """Get chat message history"""
294298
295299 # Verify project exists and user has access
296- if project_id not in MOCK_PROJECTS :
297- raise HTTPException (status_code = 404 , detail = "Project not found" )
300+ try :
301+ user_uuid = uuid .UUID (user_id )
302+ project_uuid = uuid .UUID (project_id )
303+
304+ if not project_service .check_project_ownership (project_uuid , user_uuid ):
305+ raise HTTPException (status_code = 404 , detail = "Project not found" )
298306
299- project_data = MOCK_PROJECTS [project_id ]
300- if project_data ["user_id" ] != user_id :
301- raise HTTPException (status_code = 403 , detail = "Access denied" )
307+ except ValueError :
308+ raise HTTPException (status_code = 400 , detail = "Invalid project ID" )
302309
303310 # Get messages for project
304311 messages_data = MOCK_CHAT_MESSAGES .get (project_id , [])
@@ -328,12 +335,15 @@ async def get_csv_preview(
328335 """Get CSV data preview"""
329336
330337 # Verify project exists and user has access
331- if project_id not in MOCK_PROJECTS :
332- raise HTTPException (status_code = 404 , detail = "Project not found" )
338+ try :
339+ user_uuid = uuid .UUID (user_id )
340+ project_uuid = uuid .UUID (project_id )
333341
334- project_data = MOCK_PROJECTS [project_id ]
335- if project_data ["user_id" ] != user_id :
336- raise HTTPException (status_code = 403 , detail = "Access denied" )
342+ if not project_service .check_project_ownership (project_uuid , user_uuid ):
343+ raise HTTPException (status_code = 404 , detail = "Project not found" )
344+
345+ except ValueError :
346+ raise HTTPException (status_code = 400 , detail = "Invalid project ID" )
337347
338348 # Get preview data for project
339349 if project_id not in MOCK_CSV_PREVIEWS :
@@ -352,12 +362,15 @@ async def get_query_suggestions(
352362 """Get query suggestions"""
353363
354364 # Verify project exists and user has access
355- if project_id not in MOCK_PROJECTS :
356- raise HTTPException (status_code = 404 , detail = "Project not found" )
365+ try :
366+ user_uuid = uuid .UUID (user_id )
367+ project_uuid = uuid .UUID (project_id )
368+
369+ if not project_service .check_project_ownership (project_uuid , user_uuid ):
370+ raise HTTPException (status_code = 404 , detail = "Project not found" )
357371
358- project_data = MOCK_PROJECTS [project_id ]
359- if project_data ["user_id" ] != user_id :
360- raise HTTPException (status_code = 403 , detail = "Access denied" )
372+ except ValueError :
373+ raise HTTPException (status_code = 400 , detail = "Invalid project ID" )
361374
362375 # Return mock suggestions
363376 suggestions = [QuerySuggestion (** sug ) for sug in MOCK_SUGGESTIONS ]
0 commit comments