добавил метод получения пользователя и его роли

This commit is contained in:
Мельников Данил 2025-05-31 09:23:38 +05:00
parent 75a25f9d47
commit dfac532216
2 changed files with 31 additions and 1 deletions

View File

@ -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

View File

@ -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(