diff --git a/API/app/contollers/profiles_router.py b/API/app/contollers/profiles_router.py index 58d980a..0c4f4b4 100644 --- a/API/app/contollers/profiles_router.py +++ b/API/app/contollers/profiles_router.py @@ -1,6 +1,6 @@ from typing import Optional -from fastapi import APIRouter, Depends +from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.ext.asyncio import AsyncSession from app.database.session import get_db @@ -55,3 +55,22 @@ async def delete_profile( ): profiles_service = ProfilesService(db) return await profiles_service.delete(profile_id, user) + + +@router.get( + '/user/{user_id}/', + response_model=Optional[ProfileEntity], + summary='Get profile by user ID', + description='Retrieve profile data by user ID', +) +async def get_profile_by_user_id( + user_id: int, + db: AsyncSession = Depends(get_db), + user=Depends(get_current_user), +): + profiles_service = ProfilesService(db) + profile = await profiles_service.get_by_user_id(user_id) + + return profile + + diff --git a/API/app/infrastructure/profiles_service.py b/API/app/infrastructure/profiles_service.py index 8a1e695..4b4336b 100644 --- a/API/app/infrastructure/profiles_service.py +++ b/API/app/infrastructure/profiles_service.py @@ -102,6 +102,17 @@ class ProfilesService: return self.model_to_entity(result) + async def get_by_user_id(self, user_id: int) -> Optional[ProfileEntity]: + user = await self.users_repository.get_by_id(user_id) + if user is None: + raise HTTPException(status_code=404, detail='User not found') + + profile_model = await self.profiles_repository.get_by_id(user.profile_id) + if not profile_model: + raise HTTPException(status_code=404, detail='Profile not found') + + return self.model_to_entity(profile_model) + @staticmethod def model_to_entity(profile_model: Profile) -> ProfileEntity: return ProfileEntity(