modify: auth 插件

This commit is contained in:
2026-07-23 17:45:44 +08:00
parent 95ef842d92
commit 2b7b8ee08c
21 changed files with 753 additions and 43 deletions
+20 -11
View File
@@ -1,4 +1,4 @@
import modelvault
from sqlalchemy import select, update
from chongming_worker import rpc_method
@@ -15,19 +15,28 @@ from ..models.counter import Counter
async def count_get() -> CountGetResponse:
from ..global_var import db
# 确保数据库已初始化(如果未初始化,则同步初始化,但这里假设已就绪)
if db is None:
raise RuntimeError("Database not initialized")
counters = modelvault.models.async_collection(db, Counter) # type: ignore
async with db.transaction():
counter = await counters.get("visits")
async with db.get_session() as session:
statement = select(Counter).where(Counter.name == "visits") # type: ignore
result = await session.execute(statement)
counter = result.scalar_one_or_none()
if counter is None:
counter = Counter(name="visits", value=0)
await counters.insert(counter)
counter = Counter(name="visits", value=1)
session.add(counter)
await session.commit()
return CountGetResponse(name="visits", value=1)
else:
new_value = counter.value + 1
await counters.update("visits", {"value": new_value})
counter.value = new_value # 更新本地对象以便返回
return CountGetResponse(name=counter.name, value=counter.value) # type: ignore
update_stmt = (
update(Counter)
.where(Counter.name == "visits") # type: ignore
.values(value=new_value)
)
await session.execute(update_stmt)
await session.commit()
return CountGetResponse(name="visits", value=new_value)