добавил метод получения пользователя и его роли
This commit is contained in:
parent
75a25f9d47
commit
dfac532216
@ -1,6 +1,6 @@
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from app.database.session import get_db
|
from app.database.session import get_db
|
||||||
@ -55,3 +55,22 @@ async def delete_profile(
|
|||||||
):
|
):
|
||||||
profiles_service = ProfilesService(db)
|
profiles_service = ProfilesService(db)
|
||||||
return await profiles_service.delete(profile_id, user)
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -102,6 +102,17 @@ class ProfilesService:
|
|||||||
|
|
||||||
return self.model_to_entity(result)
|
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
|
@staticmethod
|
||||||
def model_to_entity(profile_model: Profile) -> ProfileEntity:
|
def model_to_entity(profile_model: Profile) -> ProfileEntity:
|
||||||
return ProfileEntity(
|
return ProfileEntity(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user