Skip to content

Commit 1a9deb8

Browse files
更新逻辑(使用主键查询) (#47)
1 parent 349ae9a commit 1a9deb8

File tree

10 files changed

+46
-38
lines changed

10 files changed

+46
-38
lines changed

docs/apis/advanced.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ async def remove_currency(currency_id: str, session: AsyncSession):
5353
...
5454
```
5555

56-
### `~~.getcurrency`
56+
### `~~.get_currency`
5757

5858
```python
59-
async def getcurrency(currency_id: str, session: AsyncSession) -> CurrencyMeta | None:
59+
async def get_currency(currency_id: str, session: AsyncSession) -> CurrencyMeta | None:
6060
"""获取一个货币的元信息
6161
6262
Args:

docs/apis/kernel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class CurrencyRepository:
3131
async def update_currency(self, currency_data: CurrencyData) -> CurrencyMeta:
3232
"""更新货币信息"""
3333

34-
async def getcurrency(self, currency_id: str) -> CurrencyMeta | None:
34+
async def get_currency(self, currency_id: str) -> CurrencyMeta | None:
3535
"""获取货币信息"""
3636
...
3737

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "nonebot-plugin-value"
3-
version = "0.0.9.post1"
3+
version = "0.0.9.post2"
44
description = "Value API for NoneBot2"
55
readme = "README.md"
66
requires-python = ">=3.10"

src/nonebot_plugin_value/api/api_balance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async def del_account(user_id: str, currency_id: str | None = None) -> bool:
4646
"""
4747
if currency_id is None:
4848
currency_id = (await _get_default()).id
49-
return await _del_account(user_id)
49+
return await _del_account(user_id, currency_id=currency_id)
5050

5151

5252
async def get_or_create_account(

src/nonebot_plugin_value/api/api_currency.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from nonebot_plugin_orm import get_session
22

33
from ..pyd_models.currency_pyd import CurrencyData
4+
from ..services.currency import get_currency as _g_currency
45
from ..services.currency import get_default_currency as _default_currency
56
from ..services.currency import get_or_create_currency as _create_currency
6-
from ..services.currency import getcurrency as _g_currency
77
from ..services.currency import list_currencies as _currencies
88
from ..services.currency import remove_currency as _remove_currency
99
from ..services.currency import update_currency as _update_currency

src/nonebot_plugin_value/repository.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .models.balance import Transaction, UserAccount
1010
from .models.currency import CurrencyMeta
1111
from .pyd_models.currency_pyd import CurrencyData
12-
from .uuid_lib import NAMESPACE_VALUE
12+
from .uuid_lib import NAMESPACE_VALUE, get_uni_id
1313

1414
DEFAULT_NAME = "DEFAULT_CURRENCY_USD"
1515
DEFAULT_CURRENCY_UUID = uuid5(NAMESPACE_VALUE, DEFAULT_NAME)
@@ -53,7 +53,7 @@ async def update_currency(self, currency_data: CurrencyData) -> CurrencyMeta:
5353
session.add(currency_meta)
5454
return currency_meta
5555

56-
async def getcurrency(self, currency_id: str) -> CurrencyMeta | None:
56+
async def get_currency(self, currency_id: str) -> CurrencyMeta | None:
5757
"""获取货币信息"""
5858
async with self.session as session:
5959
result = await self.session.execute(
@@ -117,10 +117,7 @@ async def get_or_create_account(
117117
# 检查账户是否存在
118118
stmt = (
119119
select(UserAccount)
120-
.where(
121-
UserAccount.id == user_id,
122-
UserAccount.currency_id == currency_id,
123-
)
120+
.where(UserAccount.uni_id == get_uni_id(user_id, currency_id))
124121
.with_for_update()
125122
)
126123
result = await session.execute(stmt)
@@ -132,7 +129,7 @@ async def get_or_create_account(
132129

133130
session.add(currency)
134131
account = UserAccount(
135-
uni_id=uuid5(NAMESPACE_VALUE, f"{user_id}{currency_id}").hex,
132+
uni_id=get_uni_id(user_id, currency_id),
136133
id=user_id,
137134
currency_id=currency_id,
138135
balance=currency.default_balance,
@@ -142,17 +139,17 @@ async def get_or_create_account(
142139
await session.commit()
143140

144141
stmt = select(UserAccount).where(
145-
UserAccount.id == user_id,
146-
UserAccount.currency_id == currency_id,
142+
UserAccount.uni_id == get_uni_id(user_id, currency_id)
147143
)
148144
result = await session.execute(stmt)
149145
account = result.scalar_one()
150146
session.add(account)
151147
return account
152148

153-
async def get_balance(self, account_id: str) -> float | None:
149+
async def get_balance(self, account_id: str, currency_id: str) -> float | None:
154150
"""获取账户余额"""
155-
account = await self.session.get(UserAccount, account_id)
151+
uni_id = get_uni_id(account_id, currency_id)
152+
account = await self.session.get(UserAccount, uni_id)
156153
return account.balance if account else None
157154

158155
async def update_balance(
@@ -165,10 +162,7 @@ async def update_balance(
165162
account = (
166163
await session.execute(
167164
select(UserAccount)
168-
.where(
169-
UserAccount.id == account_id,
170-
UserAccount.currency_id == currency_id,
171-
)
165+
.where(UserAccount.uni_id == get_uni_id(account_id, currency_id))
172166
.with_for_update()
173167
)
174168
).scalar_one_or_none()
@@ -212,14 +206,21 @@ async def list_accounts(
212206
session.add_all(data)
213207
return data
214208

215-
async def remove_account(self, account_id: str):
209+
async def remove_account(self, account_id: str, currency_id: str | None = None):
216210
"""删除账户"""
217211
async with self.session as session:
218-
stmt = (
219-
select(UserAccount)
220-
.where(UserAccount.id == account_id)
221-
.with_for_update()
222-
)
212+
if not currency_id:
213+
stmt = (
214+
select(UserAccount)
215+
.where(UserAccount.id == account_id)
216+
.with_for_update()
217+
)
218+
else:
219+
stmt = (
220+
select(UserAccount)
221+
.where(UserAccount.uni_id == get_uni_id(account_id, currency_id))
222+
.with_for_update()
223+
)
223224
accounts = (await session.execute(stmt)).scalars().all()
224225
if not accounts:
225226
raise ValueError("Account not found")
@@ -250,7 +251,7 @@ async def create_transaction(
250251
if timestamp is None:
251252
timestamp = datetime.now(timezone.utc)
252253
uuid = uuid1().hex
253-
stmt = insert(Transaction).values(
254+
transaction_data = Transaction(
254255
id=uuid,
255256
account_id=account_id,
256257
currency_id=currency_id,
@@ -261,18 +262,18 @@ async def create_transaction(
261262
balance_after=balance_after,
262263
timestamp=timestamp,
263264
)
264-
await session.execute(stmt)
265+
session.add(transaction_data)
265266
await session.commit()
266267
stmt = (
267268
select(Transaction)
268269
.where(
269270
Transaction.id == uuid,
270-
Transaction.timestamp == timestamp,
271271
)
272272
.with_for_update()
273273
)
274274
result = await session.execute(stmt)
275-
transaction = result.scalars().one()
275+
transaction = result.scalar_one_or_none()
276+
assert transaction, f"无法读取到交易记录[... WHERE id = {uuid} ...]!"
276277
session.add(transaction)
277278
return transaction
278279

src/nonebot_plugin_value/services/balance.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717

1818
async def del_account(
19-
account_id: str, session: AsyncSession | None = None, fail_then_throw: bool = False
19+
account_id: str,
20+
session: AsyncSession | None = None,
21+
fail_then_throw: bool = False,
22+
currency_id: str | None = None,
2023
) -> bool:
2124
"""删除账户
2225
@@ -28,7 +31,7 @@ async def del_account(
2831
session = get_session()
2932
async with session:
3033
try:
31-
await AccountRepository(session).remove_account(account_id)
34+
await AccountRepository(session).remove_account(account_id, currency_id)
3235
return True
3336
except Exception:
3437
await session.rollback()

src/nonebot_plugin_value/services/currency.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async def list_currencies(session: AsyncSession) -> Sequence[CurrencyMeta]:
5252
return data
5353

5454

55-
async def getcurrency(currency_id: str, session: AsyncSession) -> CurrencyMeta | None:
55+
async def get_currency(currency_id: str, session: AsyncSession) -> CurrencyMeta | None:
5656
"""获取一个货币的元信息
5757
5858
Args:
@@ -63,7 +63,7 @@ async def getcurrency(currency_id: str, session: AsyncSession) -> CurrencyMeta |
6363
CurrencyMeta | None: 货币元数据(不存在为None)
6464
"""
6565
async with session:
66-
metadata = await CurrencyRepository(session).getcurrency(currency_id)
66+
metadata = await CurrencyRepository(session).get_currency(currency_id)
6767
return metadata
6868

6969

@@ -85,15 +85,15 @@ async def get_or_create_currency(
8585
while True:
8686
currency_data.id = uuid4().hex
8787
if (
88-
await getcurrency(
88+
await get_currency(
8989
uuid4().hex,
9090
session,
9191
)
9292
is None
9393
):
9494
break
9595
if (
96-
metadata := await getcurrency(
96+
metadata := await get_currency(
9797
currency_data.id,
9898
session,
9999
)

src/nonebot_plugin_value/uuid_lib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@
66

77
def to_uuid(s: str) -> str:
88
return uuid5(NAMESPACE_VALUE, s).hex
9+
10+
11+
def get_uni_id(user_id: str, currency_id: str) -> str:
12+
return uuid5(NAMESPACE_VALUE, f"{user_id}{currency_id}").hex

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)