Skip to content

Commit 1a82771

Browse files
Refactor (#39)
* 重构类型 * 更换回到datetime * Refactor: 项目类型,CI/CD,格式 * fix: some wrong * 更改版本号,namespace --------- Co-authored-by: MrlingXD <[email protected]>
1 parent 5a90288 commit 1a82771

29 files changed

+237
-160
lines changed

.github/workflows/CI.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ jobs:
4040
with:
4141
python-path: ${{ env.PYTHON_BIN }}
4242
pylance-version: latest-release
43-
43+
- name: Check code format
44+
uses: astral-sh/ruff-action@v3
45+
with:
46+
args: check . --exit-non-zero-on-fix
4447

4548
- name: Build package
4649
run: uv build # 生成构建产物到dist目录

.github/workflows/PR.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ jobs:
4040
with:
4141
python-path: ${{ env.PYTHON_BIN }}
4242
pylance-version: latest-release
43-
44-
43+
- name: Check code format
44+
uses: astral-sh/ruff-action@v3
45+
with:
46+
args: check . --exit-non-zero-on-fix
4547
- name: Build package
4648
run: uv build # 生成构建产物到dist目录
4749

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@ wheels/
88

99
# Virtual environments
1010
.venv
11+
uv.toml
12+
13+
# Conda environments
14+
.conda
15+
16+
# PyCharm
17+
.idea/
18+
19+
# Ruff
20+
.ruff_cache/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ plugins = ["nonebot_plugin_value"]
6868

6969
1. 升级 nonebot_plugin_value 到最新版本
7070

71-
2. 运行迁移脚本((如果Bot根目录下**没有**迁移脚本目录,请使用`nb orm init`来初始化。)在bot根目录使用`nb orm revison``nb orm upgrade`进行迁移)
71+
2. 运行迁移脚本((如果 Bot 根目录下**没有**迁移脚本目录,请使用`nb orm init`来初始化。)在 bot 根目录使用`nb orm revison``nb orm upgrade`进行迁移)

docs/apis/advanced.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Value-高级API文档
1+
# Value-高级 API 文档
22

3-
> **本API为服务层API,包含有关数据库操作的核心逻辑**
3+
> **本 API 为服务层 API,包含有关数据库操作的核心逻辑**
44
5-
## currency-API(`~~`代指`~.db_api.balance`)
5+
## currency-API(`~~`代指`~.services.balance`)
66

77
<details>
88

@@ -103,12 +103,11 @@ async def get_default_currency(session: AsyncSession) -> CurrencyMeta:
103103
...
104104
```
105105

106-
107106
</details>
108107

109108
---
110109

111-
## balance-API(`~~`代指`~.db_api.balance`)
110+
## balance-API(`~~`代指`~.services.balance`)
112111

113112
<details>
114113

@@ -175,18 +174,18 @@ async def del_balance(
175174
amount: float,
176175
source: str = "",
177176
session: AsyncSession | None = None,
178-
) -> dict[str, Any]:
177+
) -> ActionResult:
179178
"""异步减少余额
180179
181180
Args:
182181
user_id (str): 用户ID
183182
currency_id (str): 货币ID
184-
amount (float): 数量
183+
amount (float): 金额
185184
source (str, optional): 来源说明. Defaults to "".
186185
session (AsyncSession | None, optional): 数据库异步会话. Defaults to None.
187186
188187
Returns:
189-
dict[str, Any]: 包含是否成功的说明
188+
ActionResult: 包含是否成功的说明
190189
"""
191190
...
192191
```
@@ -200,18 +199,18 @@ async def add_balance(
200199
amount: float,
201200
source: str = "",
202201
session: AsyncSession | None = None,
203-
) -> dict[str, Any]:
202+
) -> ActionResult:
204203
"""异步增加余额
205204
206205
Args:
207206
user_id (str): 用户ID
208207
currency_id (str): 货币ID
209-
amount (float): 数量
208+
amount (float): 金额
210209
source (str, optional): 来源说明. Defaults to "".
211210
session (AsyncSession | None, optional): 数据库异步会话. Defaults to None.
212211
213212
Returns:
214-
dict[str, Any]: 是否成功("success"),消息说明("message")
213+
ActionResult: 是否成功("success"),消息说明("message")
215214
"""
216215
...
217216
```
@@ -226,29 +225,28 @@ async def transfer_funds(
226225
amount: float,
227226
source: str = "transfer",
228227
session: AsyncSession | None = None,
229-
) -> dict[str, Any]:
228+
) -> TransferResult:
230229
"""异步转账
231230
232231
Args:
233232
fromuser_id (str): 源用户ID
234233
touser_id (str): 目标用户ID
235234
currency_id (str): 货币ID
236-
amount (float): 数量
235+
amount (float): 金额
237236
source (str, optional): 源说明. Defaults to "transfer".
238237
session (AsyncSession | None, optional): 数据库异步Session. Defaults to None.
239238
240239
Returns:
241-
dict[str, Any]: 如果成功则包含"from_balance"(源账户现在的balance),"to_balance"(目标账户现在的balance),否则包含"message"(错误消息)字段
240+
TransferResult: 如果成功则"from_balance"(源账户现在的balance),"to_balance"(目标账户现在的balance)字段不为None
242241
"""
243242
...
244243
```
245244

246-
247245
</details>
248246

249247
---
250248

251-
## transaction-API(`~~`代指`~.db_api.transaction`)
249+
## transaction-API(`~~`代指`~.services.transaction`)
252250

253251
<details>
254252

docs/apis/kernel.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
# Value-底层API文档
1+
# Value-底层 API 文档
22

3-
> **本API为数据层API,包含操作数据库的底层逻辑**
3+
> **本 API 为数据层 API,包含操作数据库的底层逻辑**
44
55
## Repository(`~.repository`)
66

77
### 变量定义:
8+
89
```python
910
DEFAULT_NAME = "DEFAULT_CURRENCY_USD"
10-
DEFAULT_CURRENCY_UUID = uuid5(uuid.NAMESPACE_X500, "nonebot_plugin_value")
11+
DEFAULT_CURRENCY_UUID = uuid5(NAMESPACE_VALUE, "nonebot_plugin_value")
1112
```
1213

1314
---

docs/apis/standard.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Value-标准API文档
1+
# Value-标准 API 文档
22

3-
> **标准API文档**
3+
> **标准 API 文档**
44
55
## balance-API(`~~`代指`~.api.api_balance`)
66

@@ -67,7 +67,7 @@ async def add_balance(
6767
6868
Args:
6969
user_id (str): 用户ID
70-
amount (float): 数量
70+
amount (float): 金额
7171
source (str, optional): 源描述. Defaults to "_transfer".
7272
currency_id (str | None, optional): 货币ID(不填使用默认). Defaults to None.
7373
@@ -121,7 +121,7 @@ async def transfer_funds(
121121
Args:
122122
from_id (str): 源账户
123123
to_id (str): 目标账户
124-
amount (float): 数量
124+
amount (float): 金额
125125
source (str, optional): 来源说明. Defaults to "from {from_id} to {to_id}".
126126
currency_id (str | None, optional): 货币ID(不填则使用默认货币). Defaults to None.
127127
@@ -134,7 +134,6 @@ async def transfer_funds(
134134
...
135135
```
136136

137-
138137
</details>
139138

140139
---
@@ -259,7 +258,7 @@ async def get_transaction_history(
259258
260259
Args:
261260
account_id (str): 账户ID
262-
limit (int, optional): 最大数量. Defaults to 10.
261+
limit (int, optional): 数量. Defaults to 10.
263262
264263
Returns:
265264
list[TransactionData]: 包含交易数据的列表

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "nonebot-plugin-value"
3-
version = "0.0.7"
3+
version = "0.0.8.dev1"
44
description = "Value API for NoneBot2"
55
readme = "README.md"
66
requires-python = ">=3.10"
@@ -42,4 +42,4 @@ ignore = [
4242
]
4343

4444
[tool.pyright]
45-
typeCheckingMode = "standard"
45+
typeCheckingMode = "strict"

src/nonebot_plugin_value/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
from . import action_type, repository
77
from .api import api_balance, api_currency, api_transaction
88
from .api.api_currency import get_or_create_currency
9-
from .db_api import balance, transaction
10-
from .db_api import currency as currency_api
119
from .hook import context, exception, hooks_manager, hooks_type
1210
from .models import currency
1311
from .pyd_models import balance_pyd, base_pyd, currency_pyd
1412
from .pyd_models.currency_pyd import CurrencyData
1513
from .repository import DEFAULT_CURRENCY_UUID
14+
from .services import balance, transaction
15+
from .services import currency as currency_api
1616

1717
__plugin_meta__ = PluginMetadata(
1818
name="Value",

src/nonebot_plugin_value/api/api_balance.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from nonebot_plugin_orm import get_session
22

3-
from ..db_api.balance import add_balance as _a_balance
4-
from ..db_api.balance import del_account as _del_account
5-
from ..db_api.balance import del_balance as _d_balance
6-
from ..db_api.balance import get_or_create_account as _go_account
7-
from ..db_api.balance import list_accounts as _list_accounts
8-
from ..db_api.balance import transfer_funds as _transfer
93
from ..pyd_models.balance_pyd import UserAccountData
4+
from ..services.balance import add_balance as _a_balance
5+
from ..services.balance import del_account as _del_account
6+
from ..services.balance import del_balance as _d_balance
7+
from ..services.balance import get_or_create_account as _go_account
8+
from ..services.balance import list_accounts as _list_accounts
9+
from ..services.balance import transfer_funds as _transfer
1010
from .api_currency import get_default_currency as _get_default
1111

1212

@@ -84,7 +84,7 @@ async def add_balance(
8484
8585
Args:
8686
user_id (str): 用户ID
87-
amount (float): 数量
87+
amount (float): 金额
8888
source (str, optional): 源描述. Defaults to "_transfer".
8989
currency_id (str | None, optional): 货币ID(不填使用默认). Defaults to None.
9090
@@ -98,8 +98,8 @@ async def add_balance(
9898
if currency_id is None:
9999
currency_id = (await _get_default()).id
100100
data = await _a_balance(user_id, currency_id, amount, source)
101-
if not data.get("success", False):
102-
raise RuntimeError(data.get("message", ""))
101+
if not data.success:
102+
raise RuntimeError(data.message)
103103
return await get_or_create_account(user_id, currency_id)
104104

105105

@@ -126,8 +126,8 @@ async def del_balance(
126126
if currency_id is None:
127127
currency_id = (await _get_default()).id
128128
data = await _d_balance(user_id, currency_id, amount, source)
129-
if not data.get("success", False):
130-
raise RuntimeError(data.get("message", ""))
129+
if not data.success:
130+
raise RuntimeError(data.message)
131131
return await get_or_create_account(user_id, currency_id)
132132

133133

@@ -143,7 +143,7 @@ async def transfer_funds(
143143
Args:
144144
from_id (str): 源账户
145145
to_id (str): 目标账户
146-
amount (float): 数量
146+
amount (float): 金额
147147
source (str, optional): 来源说明. Defaults to "from {from_id} to {to_id}".
148148
currency_id (str | None, optional): 货币ID(不填则使用默认货币). Defaults to None.
149149
@@ -155,9 +155,9 @@ async def transfer_funds(
155155
"""
156156
if currency_id is None:
157157
currency_id = (await _get_default()).id
158-
if source == "":
158+
if not source:
159159
source = f"from '{from_id}' to '{to_id}'"
160160
data = await _transfer(from_id, to_id, currency_id, amount, source)
161-
if not data.get("success", False):
162-
raise RuntimeError(data.get("message", ""))
161+
if not data.success:
162+
raise RuntimeError(data.message)
163163
return await get_or_create_account(to_id, currency_id)

0 commit comments

Comments
 (0)