Skip to content

Commit d47375a

Browse files
authored
Optimize dict create and update logic (#691)
* Optimize dict create and update logic * Update version
1 parent 0602c61 commit d47375a

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

backend/plugin/dict/crud/crud_dict_data.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,33 @@ async def get_by_label(self, db: AsyncSession, label: str) -> DictData | None:
7070
"""
7171
return await self.select_model_by_column(db, label=label)
7272

73-
async def create(self, db: AsyncSession, obj: CreateDictDataParam) -> None:
73+
async def create(self, db: AsyncSession, obj: CreateDictDataParam, type_code: str) -> None:
7474
"""
7575
创建字典数据
7676
7777
:param db: 数据库会话
7878
:param obj: 创建字典数据参数
79+
:param type_code: 字典类型编码
7980
:return:
8081
"""
81-
await self.create_model(db, obj)
82+
dict_obj = obj.model_dump()
83+
dict_obj.update({'type_code': type_code})
84+
new_data = self.model(**dict_obj)
85+
db.add(new_data)
8286

83-
async def update(self, db: AsyncSession, pk: int, obj: UpdateDictDataParam) -> int:
87+
async def update(self, db: AsyncSession, pk: int, obj: UpdateDictDataParam, type_code: str) -> int:
8488
"""
8589
更新字典数据
8690
8791
:param db: 数据库会话
8892
:param pk: 字典数据 ID
8993
:param obj: 更新字典数据参数
94+
:param type_code: 字典类型编码
9095
:return:
9196
"""
92-
return await self.update_model(db, pk, obj)
97+
dict_obj = obj.model_dump()
98+
dict_obj.update({'type_code': type_code})
99+
return await self.update_model(db, pk, dict_obj)
93100

94101
async def delete(self, db: AsyncSession, pks: list[int]) -> int:
95102
"""

backend/plugin/dict/plugin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[plugin]
22
summary = '数据字典'
3-
version = '0.0.4'
3+
version = '0.0.5'
44
description = '通常用于约束前端工程数据展示'
55
author = 'wu-clan'
66

backend/plugin/dict/schema/dict_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class DictDataSchemaBase(SchemaBase):
1212
"""字典数据基础模型"""
1313

1414
type_id: int = Field(description='字典类型 ID')
15-
type_code: str = Field(description='字典类型编码')
1615
label: str = Field(description='字典标签')
1716
value: str = Field(description='字典值')
1817
sort: int = Field(description='排序')
@@ -40,5 +39,6 @@ class GetDictDataDetail(DictDataSchemaBase):
4039
model_config = ConfigDict(from_attributes=True)
4140

4241
id: int = Field(description='字典数据 ID')
42+
type_code: str = Field(description='字典类型编码')
4343
created_time: datetime = Field(description='创建时间')
4444
updated_time: datetime | None = Field(None, description='更新时间')

backend/plugin/dict/service/dict_data_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async def create(*, obj: CreateDictDataParam) -> None:
6868
dict_type = await dict_type_dao.get(db, obj.type_id)
6969
if not dict_type:
7070
raise errors.NotFoundError(msg='字典类型不存在')
71-
await dict_data_dao.create(db, obj)
71+
await dict_data_dao.create(db, obj, dict_type.code)
7272

7373
@staticmethod
7474
async def update(*, pk: int, obj: UpdateDictDataParam) -> int:
@@ -89,7 +89,7 @@ async def update(*, pk: int, obj: UpdateDictDataParam) -> int:
8989
dict_type = await dict_type_dao.get(db, obj.type_id)
9090
if not dict_type:
9191
raise errors.NotFoundError(msg='字典类型不存在')
92-
count = await dict_data_dao.update(db, pk, obj)
92+
count = await dict_data_dao.update(db, pk, obj, dict_type.code)
9393
return count
9494

9595
@staticmethod

0 commit comments

Comments
 (0)