diff --git a/backend/plugin/dict/api/v1/sys/dict_data.py b/backend/plugin/dict/api/v1/sys/dict_data.py index c258c2150..cff52a58f 100644 --- a/backend/plugin/dict/api/v1/sys/dict_data.py +++ b/backend/plugin/dict/api/v1/sys/dict_data.py @@ -22,6 +22,12 @@ router = APIRouter() +@router.get('/all', summary='获取所有字典数据', dependencies=[DependsJwtAuth]) +async def get_all_dict_datas() -> ResponseSchemaModel[list[GetDictDataDetail]]: + data = await dict_data_service.get_all() + return response_base.success(data=data) + + @router.get('/{pk}', summary='获取字典数据详情', dependencies=[DependsJwtAuth]) async def get_dict_data( pk: Annotated[int, Path(description='字典数据 ID')], diff --git a/backend/plugin/dict/api/v1/sys/dict_type.py b/backend/plugin/dict/api/v1/sys/dict_type.py index 04ec35d7f..64ab0ed1d 100644 --- a/backend/plugin/dict/api/v1/sys/dict_type.py +++ b/backend/plugin/dict/api/v1/sys/dict_type.py @@ -21,6 +21,14 @@ router = APIRouter() +@router.get('/{pk}', summary='获取字典类型详情', dependencies=[DependsJwtAuth]) +async def get_dict_type( + pk: Annotated[int, Path(description='字典类型 ID')], +) -> ResponseSchemaModel[GetDictTypeDetail]: + data = await dict_type_service.get(pk=pk) + return response_base.success(data=data) + + @router.get( '', summary='分页获取所有字典类型', diff --git a/backend/plugin/dict/crud/crud_dict_data.py b/backend/plugin/dict/crud/crud_dict_data.py index 51c934cd6..d642ffa80 100644 --- a/backend/plugin/dict/crud/crud_dict_data.py +++ b/backend/plugin/dict/crud/crud_dict_data.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +from typing import Sequence + from sqlalchemy import Select from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy_crud_plus import CRUDPlus @@ -21,6 +23,15 @@ async def get(self, db: AsyncSession, pk: int) -> DictData | None: """ return await self.select_model(db, pk) + async def get_all(self, db: AsyncSession) -> Sequence[DictData]: + """ + 获取所有字典数据 + + :param db: 数据库会话 + :return: + """ + return await self.select_models(db, load_strategies={'type': 'noload'}) + async def get_list(self, label: str | None, value: str | None, status: int | None) -> Select: """ 获取字典数据列表 @@ -39,7 +50,7 @@ async def get_list(self, label: str | None, value: str | None, status: int | Non if status is not None: filters['status'] = status - return await self.select_order('id', 'desc', *filters) + return await self.select_order('id', 'desc', load_strategies={'type': 'noload'}, **filters) async def get_by_label(self, db: AsyncSession, label: str) -> DictData | None: """ diff --git a/backend/plugin/dict/crud/crud_dict_type.py b/backend/plugin/dict/crud/crud_dict_type.py index 33872b3ee..b74c34700 100644 --- a/backend/plugin/dict/crud/crud_dict_type.py +++ b/backend/plugin/dict/crud/crud_dict_type.py @@ -39,7 +39,7 @@ async def get_list(self, *, name: str | None, code: str | None, status: int | No if status is not None: filters['status'] = status - return await self.select_order('id', 'desc', **filters) + return await self.select_order('id', 'desc', load_strategies={'datas': 'noload'}, **filters) async def get_by_code(self, db: AsyncSession, code: str) -> DictType | None: """ diff --git a/backend/plugin/dict/plugin.toml b/backend/plugin/dict/plugin.toml index 0011b7650..f8a51aa14 100644 --- a/backend/plugin/dict/plugin.toml +++ b/backend/plugin/dict/plugin.toml @@ -1,6 +1,6 @@ [plugin] summary = '数据字典' -version = '0.0.1' +version = '0.0.2' description = '通常用于约束前端工程数据展示' author = 'wu-clan' diff --git a/backend/plugin/dict/service/dict_data_service.py b/backend/plugin/dict/service/dict_data_service.py index 3e3b6b978..d69c25d1b 100644 --- a/backend/plugin/dict/service/dict_data_service.py +++ b/backend/plugin/dict/service/dict_data_service.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +from typing import Sequence + from sqlalchemy import Select from backend.common.exception import errors @@ -27,6 +29,12 @@ async def get(*, pk: int) -> DictData: raise errors.NotFoundError(msg='字典数据不存在') return dict_data + @staticmethod + async def get_all() -> Sequence[DictData]: + async with async_db_session() as db: + dict_datas = await dict_data_dao.get_all(db) + return dict_datas + @staticmethod async def get_select(*, label: str | None, value: str | None, status: int | None) -> Select: """ diff --git a/backend/plugin/dict/service/dict_type_service.py b/backend/plugin/dict/service/dict_type_service.py index 3ed1c3ff9..50ba96cc9 100644 --- a/backend/plugin/dict/service/dict_type_service.py +++ b/backend/plugin/dict/service/dict_type_service.py @@ -5,12 +5,27 @@ from backend.common.exception import errors from backend.database.db import async_db_session from backend.plugin.dict.crud.crud_dict_type import dict_type_dao +from backend.plugin.dict.model import DictType from backend.plugin.dict.schema.dict_type import CreateDictTypeParam, DeleteDictTypeParam, UpdateDictTypeParam class DictTypeService: """字典类型服务类""" + @staticmethod + async def get(*, pk) -> DictType: + """ + 获取字典类型详情 + + :param pk: 字典类型 ID + :return: + """ + async with async_db_session() as db: + dict_type = await dict_type_dao.get(db, pk) + if not dict_type: + raise errors.NotFoundError(msg='字典类型不存在') + return dict_type + @staticmethod async def get_select(*, name: str | None, code: str | None, status: int | None) -> Select: """