mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 13:12:24 +00:00
Add dictionary type and datas queries (#679)
This commit is contained in:
@@ -22,6 +22,12 @@ from backend.plugin.dict.service.dict_data_service import dict_data_service
|
||||
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')],
|
||||
|
||||
@@ -21,6 +21,14 @@ from backend.plugin.dict.service.dict_type_service import dict_type_service
|
||||
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='分页获取所有字典类型',
|
||||
|
||||
@@ -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 @@ class CRUDDictData(CRUDPlus[DictData]):
|
||||
"""
|
||||
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 @@ class CRUDDictData(CRUDPlus[DictData]):
|
||||
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:
|
||||
"""
|
||||
|
||||
@@ -39,7 +39,7 @@ class CRUDDictType(CRUDPlus[DictType]):
|
||||
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:
|
||||
"""
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[plugin]
|
||||
summary = '数据字典'
|
||||
version = '0.0.1'
|
||||
version = '0.0.2'
|
||||
description = '通常用于约束前端工程数据展示'
|
||||
author = 'wu-clan'
|
||||
|
||||
|
||||
@@ -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 @@ class DictDataService:
|
||||
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:
|
||||
"""
|
||||
|
||||
@@ -5,12 +5,27 @@ from sqlalchemy import Select
|
||||
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:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user