feat(user): 新增用户信息(访问者)返回是否关注is_follow CRUD同步新增exists方法
This commit is contained in:
@@ -152,6 +152,15 @@ class CRUD:
|
|||||||
await self.session.commit()
|
await self.session.commit()
|
||||||
return result.rowcount > 0
|
return result.rowcount > 0
|
||||||
|
|
||||||
|
async def exists(self, filters: dict, model: Model = None) -> bool:
|
||||||
|
model = model or self.model
|
||||||
|
|
||||||
|
result = await self.session.execute(select(model).filter(
|
||||||
|
*self.build_filter_conditions(filters, model))
|
||||||
|
)
|
||||||
|
state = True if result.scalars().first() else False
|
||||||
|
return state
|
||||||
|
|
||||||
async def total(self, filters: dict, model: Model = None) -> int:
|
async def total(self, filters: dict, model: Model = None) -> int:
|
||||||
model = model or self.model
|
model = model or self.model
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ from core.routers.auth.schema import LoginUser
|
|||||||
from core.routers.auth.services import get_current_user_form_http
|
from core.routers.auth.services import get_current_user_form_http
|
||||||
|
|
||||||
from . import schema
|
from . import schema
|
||||||
from .schema import UpdateUserIn
|
|
||||||
|
|
||||||
user_router = APIRouter(prefix='/user', tags=['用户模块'])
|
user_router = APIRouter(prefix='/user', tags=['用户模块'])
|
||||||
|
|
||||||
@@ -45,7 +44,7 @@ async def get_me_info(
|
|||||||
@user_router.get(
|
@user_router.get(
|
||||||
'/visit/{user_id}/info',
|
'/visit/{user_id}/info',
|
||||||
summary='用户信息(访问者)',
|
summary='用户信息(访问者)',
|
||||||
response_model=base_schema.BaseResponse[schema.LiteUser]
|
response_model=base_schema.BaseResponse[schema.VisitUser]
|
||||||
)
|
)
|
||||||
async def get_visit_user_info(
|
async def get_visit_user_info(
|
||||||
user_id: int,
|
user_id: int,
|
||||||
@@ -59,6 +58,22 @@ async def get_visit_user_info(
|
|||||||
user = await db_crud.get(filters={'user_id': user_id})
|
user = await db_crud.get(filters={'user_id': user_id})
|
||||||
if user is None:
|
if user is None:
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='访问用户不存在')
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='访问用户不存在')
|
||||||
|
|
||||||
|
user.is_follow = await db_crud.exists(
|
||||||
|
model=UserFollows,
|
||||||
|
filters={
|
||||||
|
'follower_id': login_user.user_id,
|
||||||
|
'followed_id': user_id
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
user.displayed_card_total = await db_crud.total(
|
||||||
|
model=UserCollection,
|
||||||
|
filters={
|
||||||
|
'user_id': user.user_id,
|
||||||
|
'displayed': True}
|
||||||
|
)
|
||||||
|
|
||||||
return {'data': user}
|
return {'data': user}
|
||||||
|
|
||||||
|
|
||||||
@@ -136,12 +151,10 @@ async def follow_user(
|
|||||||
if follower_user is None:
|
if follower_user is None:
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='被关注者不存在')
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='被关注者不存在')
|
||||||
|
|
||||||
user: User | None = await db_crud.get(filters={'user_id': login_user.user_id})
|
|
||||||
|
|
||||||
db_crud = CRUD(db_session, UserFollows)
|
db_crud = CRUD(db_session, UserFollows)
|
||||||
record = {
|
record = {
|
||||||
'follower_id': login_user.user_id,
|
'follower_id': login_user.user_id,
|
||||||
'followed_id': user.user_id,
|
'followed_id': follower_user.user_id,
|
||||||
}
|
}
|
||||||
if action == 'follower':
|
if action == 'follower':
|
||||||
await db_crud.create(data=record, filters=record)
|
await db_crud.create(data=record, filters=record)
|
||||||
|
|||||||
@@ -30,6 +30,10 @@ class LiteUser(BaseModel):
|
|||||||
gender: Optional[int] = None
|
gender: Optional[int] = None
|
||||||
avatar_url: Optional[str] = None
|
avatar_url: Optional[str] = None
|
||||||
vip_expire_date: Optional[date] = None
|
vip_expire_date: Optional[date] = None
|
||||||
|
|
||||||
|
|
||||||
|
class VisitUser(LiteUser):
|
||||||
|
is_follow: Optional[bool] = False
|
||||||
displayed_card_total: Optional[int] = 0
|
displayed_card_total: Optional[int] = 0
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user