feat(auth、user):新增用户的粉丝数、关注数、简介

This commit is contained in:
xingc
2025-10-16 15:28:07 +08:00
parent cddb0ab0dd
commit 0cf3145e86
5 changed files with 99 additions and 11 deletions

View File

@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
# @File: actions.py
# @Date: 2025/10/15 18:58
# @Software: PyCharm
# @Author: xingc
# @Desc:
import logging
from sqlalchemy.ext.asyncio import AsyncSession
from core.db.crud import CRUD
from core.db.engine import async_db_session
from core.db.models import User, UserFollows
logger = logging.getLogger(__name__)
async def update_user_follower_total(
follower_id: int,
followed_id: int,
db_session: AsyncSession
):
"""
更新用户计数
:param follower_id: 关注者id
:param followed_id: 被关注者id
:param db_session:
:return:
"""
db_crud = CRUD(db_session, UserFollows)
# 更新关注数
followers_count = await db_crud.total(
filters={
'follower_id': follower_id,
'followed_id': followed_id,
}
)
await db_crud.update(
model=User,
data={
'followers_count': followers_count,
},
filters={
'user_id': follower_id,
}
)
# 更新粉丝数
fans_count = await db_crud.total(
filters={
'followed_id': followed_id,
}
)
await db_crud.update(
model=User,
data={
'fans_count': fans_count,
},
filters={
'user_id': followed_id,
}
)

View File

@@ -18,6 +18,7 @@ from core.routers.auth.schema import LoginUser
from core.routers.auth.services import get_current_user_form_http
from . import schema
from .actions import update_user_follower_total
user_router = APIRouter(prefix='/user', tags=['用户模块'])
@@ -158,10 +159,15 @@ async def follow_user(
}
if action == 'follower':
await db_crud.create(data=record, filters=record)
if action == 'unfollower':
elif action == 'unfollower':
await db_crud.delete(filters=record)
await update_user_follower_total(
follower_id=login_user.user_id,
followed_id=follower_user.user_id,
db_session=db_session
)
return {'message': 'ok'}

View File

@@ -7,11 +7,12 @@
from datetime import date
from typing import Optional
from fastapi.openapi.models import Schema
from pydantic import BaseModel, constr
from pydantic import constr
from core.base.schema import BaseMixin
class User(BaseModel):
class User(BaseMixin):
nickname: str
user_id: int
gender: Optional[int] = None
@@ -22,9 +23,13 @@ class User(BaseModel):
vip_expire_date: Optional[date] = None
points: Optional[int] = 0
displayed_card_total: Optional[int] = 0
description: Optional[str | None] = None
last_active_at: Optional[str | None] = None
fans_count: Optional[int] = 0
followers_count: Optional[int] = 0
class LiteUser(BaseModel):
class LiteUser(BaseMixin):
nickname: str
user_id: int
gender: Optional[int] = None
@@ -35,16 +40,20 @@ class LiteUser(BaseModel):
class VisitUser(LiteUser):
is_follow: Optional[bool] = False
displayed_card_total: Optional[int] = 0
description: Optional[str | None] = None
last_active_at: Optional[str | None] = None
fans_count: Optional[int] = 0
class UserFollows(BaseModel):
class UserFollows(BaseMixin):
nickname: str
user_id: int
gender: Optional[int] = None
avatar_url: Optional[int] = None
class UpdateUserIn(BaseModel):
class UpdateUserIn(BaseMixin):
nickname: Optional[constr(max_length=20)] = None
gender: Optional[int] = None
avatar_url: Optional[constr(pattern='^http[a-zA-Z0-9:/\.]+')] = None
description: Optional[constr(max_length=200) | None] = None