修正若干bug,后端基本完成
This commit is contained in:
@@ -14,9 +14,9 @@ def get_current_time() -> int:
|
||||
"""获取当前UTC时间戳"""
|
||||
return int(time.time())
|
||||
|
||||
def create_token(user_id: int, username: str, role: str, expire_delta: int) -> str:
|
||||
def create_token(user_id: int, username: str, role: str, expire_delta) -> str:
|
||||
"""创建JWT token"""
|
||||
expire = get_current_time() + expire_delta
|
||||
expire = get_current_time() + int(expire_delta.total_seconds())
|
||||
to_encode = {
|
||||
"id": user_id,
|
||||
"username": username,
|
||||
|
@@ -24,8 +24,7 @@ def get_db_engine() -> AsyncEngine:
|
||||
raise RuntimeError("Database engine not initialized")
|
||||
return _engine
|
||||
|
||||
@asynccontextmanager
|
||||
async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
|
||||
async def get_db_session() -> AsyncSession:
|
||||
"""获取数据库会话"""
|
||||
async with AsyncSession(get_db_engine()) as session:
|
||||
try:
|
||||
@@ -35,6 +34,16 @@ async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
|
||||
await session.rollback()
|
||||
raise
|
||||
|
||||
async def get_db_session_dep() -> AsyncSession:
|
||||
"""FastAPI依赖注入使用的数据库会话获取函数"""
|
||||
async with AsyncSession(get_db_engine()) as session:
|
||||
try:
|
||||
yield session
|
||||
await session.commit()
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
raise
|
||||
|
||||
async def init_db(engine: AsyncEngine):
|
||||
"""初始化数据库"""
|
||||
global _engine
|
||||
|
@@ -30,7 +30,7 @@ async def get_user(session: AsyncSession, user_id: int) -> Optional[UserResponse
|
||||
return UserResponse.from_orm(user) if user else None
|
||||
|
||||
|
||||
async def get_users(session: AsyncSession, skip: int = 0, limit: int = 100) -> List[UserResponse]:
|
||||
async def get_users_list(session: AsyncSession, skip: int = 0, limit: int = 100) -> List[UserResponse]:
|
||||
"""获取用户列表"""
|
||||
result = await session.execute(select(User).offset(skip).limit(limit))
|
||||
users = result.scalars().all()
|
||||
|
Reference in New Issue
Block a user