'init'
This commit is contained in:
69
app/sdk/pocketbase.py
Normal file
69
app/sdk/pocketbase.py
Normal file
@@ -0,0 +1,69 @@
|
||||
"""
|
||||
PocketBase SDK
|
||||
数据库操作封装,支持配置注入
|
||||
"""
|
||||
from typing import Optional
|
||||
|
||||
from pocketbase import PocketBase
|
||||
|
||||
|
||||
class PocketBaseSDK:
|
||||
"""PocketBase 客户端 SDK"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
url: str,
|
||||
admin_email: str,
|
||||
admin_password: str,
|
||||
):
|
||||
self.url = url
|
||||
self.admin_email = admin_email
|
||||
self.admin_password = admin_password
|
||||
self._client: Optional[PocketBase] = None
|
||||
|
||||
def get_client(self) -> PocketBase:
|
||||
"""获取已认证的 PocketBase 客户端"""
|
||||
if self._client is None:
|
||||
self._client = PocketBase(self.url)
|
||||
self._client.admins.auth_with_password(self.admin_email, self.admin_password)
|
||||
return self._client
|
||||
|
||||
def collection(self, name: str):
|
||||
"""获取集合"""
|
||||
return self.get_client().collection(name)
|
||||
|
||||
def create_payment(self, user_id: str, pay_type: str, order_id: str, amount: int) -> dict:
|
||||
"""创建支付记录"""
|
||||
return self.collection("payments").create({
|
||||
"user_id": user_id,
|
||||
"type": pay_type,
|
||||
"order_id": order_id,
|
||||
"amount": amount,
|
||||
})
|
||||
|
||||
def update_meetup_payment(
|
||||
self,
|
||||
user_id: str,
|
||||
order_id: str,
|
||||
amount: int,
|
||||
) -> None:
|
||||
"""更新或创建 meetup 支付记录"""
|
||||
solan = self.collection("solan")
|
||||
existing = solan.get_list(1, 1, {
|
||||
"filter": f'user_id = "{user_id}"',
|
||||
"sort": "-created",
|
||||
})
|
||||
if existing.items:
|
||||
record_id = existing.items[0].id
|
||||
solan.update(record_id, {
|
||||
"type": "meetup",
|
||||
"order_id": order_id,
|
||||
"amount": amount,
|
||||
})
|
||||
else:
|
||||
solan.create({
|
||||
"user_id": user_id,
|
||||
"type": "meetup",
|
||||
"order_id": order_id,
|
||||
"amount": amount,
|
||||
})
|
||||
Reference in New Issue
Block a user