Skip to content

Commit f9bfe8f

Browse files
authored
Add dictionary type and datas queries (#679)
1 parent 408c866 commit f9bfe8f

File tree

7 files changed

+51
-3
lines changed

7 files changed

+51
-3
lines changed

backend/plugin/dict/api/v1/sys/dict_data.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
router = APIRouter()
2323

2424

25+
@router.get('/all', summary='获取所有字典数据', dependencies=[DependsJwtAuth])
26+
async def get_all_dict_datas() -> ResponseSchemaModel[list[GetDictDataDetail]]:
27+
data = await dict_data_service.get_all()
28+
return response_base.success(data=data)
29+
30+
2531
@router.get('/{pk}', summary='获取字典数据详情', dependencies=[DependsJwtAuth])
2632
async def get_dict_data(
2733
pk: Annotated[int, Path(description='字典数据 ID')],

backend/plugin/dict/api/v1/sys/dict_type.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
router = APIRouter()
2222

2323

24+
@router.get('/{pk}', summary='获取字典类型详情', dependencies=[DependsJwtAuth])
25+
async def get_dict_type(
26+
pk: Annotated[int, Path(description='字典类型 ID')],
27+
) -> ResponseSchemaModel[GetDictTypeDetail]:
28+
data = await dict_type_service.get(pk=pk)
29+
return response_base.success(data=data)
30+
31+
2432
@router.get(
2533
'',
2634
summary='分页获取所有字典类型',

backend/plugin/dict/crud/crud_dict_data.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3+
from typing import Sequence
4+
35
from sqlalchemy import Select
46
from sqlalchemy.ext.asyncio import AsyncSession
57
from sqlalchemy_crud_plus import CRUDPlus
@@ -21,6 +23,15 @@ async def get(self, db: AsyncSession, pk: int) -> DictData | None:
2123
"""
2224
return await self.select_model(db, pk)
2325

26+
async def get_all(self, db: AsyncSession) -> Sequence[DictData]:
27+
"""
28+
获取所有字典数据
29+
30+
:param db: 数据库会话
31+
:return:
32+
"""
33+
return await self.select_models(db, load_strategies={'type': 'noload'})
34+
2435
async def get_list(self, label: str | None, value: str | None, status: int | None) -> Select:
2536
"""
2637
获取字典数据列表
@@ -39,7 +50,7 @@ async def get_list(self, label: str | None, value: str | None, status: int | Non
3950
if status is not None:
4051
filters['status'] = status
4152

42-
return await self.select_order('id', 'desc', *filters)
53+
return await self.select_order('id', 'desc', load_strategies={'type': 'noload'}, **filters)
4354

4455
async def get_by_label(self, db: AsyncSession, label: str) -> DictData | None:
4556
"""

backend/plugin/dict/crud/crud_dict_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async def get_list(self, *, name: str | None, code: str | None, status: int | No
3939
if status is not None:
4040
filters['status'] = status
4141

42-
return await self.select_order('id', 'desc', **filters)
42+
return await self.select_order('id', 'desc', load_strategies={'datas': 'noload'}, **filters)
4343

4444
async def get_by_code(self, db: AsyncSession, code: str) -> DictType | None:
4545
"""

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.1'
3+
version = '0.0.2'
44
description = '通常用于约束前端工程数据展示'
55
author = 'wu-clan'
66

backend/plugin/dict/service/dict_data_service.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3+
from typing import Sequence
4+
35
from sqlalchemy import Select
46

57
from backend.common.exception import errors
@@ -27,6 +29,12 @@ async def get(*, pk: int) -> DictData:
2729
raise errors.NotFoundError(msg='字典数据不存在')
2830
return dict_data
2931

32+
@staticmethod
33+
async def get_all() -> Sequence[DictData]:
34+
async with async_db_session() as db:
35+
dict_datas = await dict_data_dao.get_all(db)
36+
return dict_datas
37+
3038
@staticmethod
3139
async def get_select(*, label: str | None, value: str | None, status: int | None) -> Select:
3240
"""

backend/plugin/dict/service/dict_type_service.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,27 @@
55
from backend.common.exception import errors
66
from backend.database.db import async_db_session
77
from backend.plugin.dict.crud.crud_dict_type import dict_type_dao
8+
from backend.plugin.dict.model import DictType
89
from backend.plugin.dict.schema.dict_type import CreateDictTypeParam, DeleteDictTypeParam, UpdateDictTypeParam
910

1011

1112
class DictTypeService:
1213
"""字典类型服务类"""
1314

15+
@staticmethod
16+
async def get(*, pk) -> DictType:
17+
"""
18+
获取字典类型详情
19+
20+
:param pk: 字典类型 ID
21+
:return:
22+
"""
23+
async with async_db_session() as db:
24+
dict_type = await dict_type_dao.get(db, pk)
25+
if not dict_type:
26+
raise errors.NotFoundError(msg='字典类型不存在')
27+
return dict_type
28+
1429
@staticmethod
1530
async def get_select(*, name: str | None, code: str | None, status: int | None) -> Select:
1631
"""

0 commit comments

Comments
 (0)