3
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
"""Plan 套餐 Repository。"""
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.plan import Plan
|
||||
|
||||
|
||||
class PlanRepository:
|
||||
def __init__(self, session: Session) -> None:
|
||||
self._session = session
|
||||
|
||||
def get_by_id(self, plan_id: str) -> Plan | None:
|
||||
return self._session.get(Plan, plan_id)
|
||||
|
||||
def get_by_code(self, code: str) -> Plan | None:
|
||||
stmt = select(Plan).where(Plan.code == code)
|
||||
return self._session.scalars(stmt).first()
|
||||
|
||||
def get_or_create_free(self) -> Plan:
|
||||
"""获取 free plan,不存在则创建(seed 逻辑)。"""
|
||||
plan = self.get_by_code("free")
|
||||
if plan is None:
|
||||
plan = Plan(
|
||||
code="free",
|
||||
name="免费版",
|
||||
storage_quota=104_857_600, # 100 MB
|
||||
max_file_size=20_971_520, # 20 MB
|
||||
is_active=True,
|
||||
)
|
||||
self._session.add(plan)
|
||||
self._session.flush()
|
||||
return plan
|
||||
Reference in New Issue
Block a user