mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-26 22:31:21 +00:00
style: 统一代码风格和格式 docs: 完善函数和方法的文档字符串 refactor(base_model): 移除冗余的表名和表参数生成方法 refactor(constant): 更新返回码注释格式 refactor(router_class): 添加路由处理器的详细文档 refactor(database): 完善数据库连接函数的文档 refactor(security): 添加认证类和方法的详细文档 refactor(validator): 更新验证器函数的文档格式 refactor(serialize): 优化序列化工具类的文档 refactor(response): 完善响应类的文档字符串 refactor(dependencies): 添加依赖函数的详细文档 refactor(initialize): 完善初始化脚本的文档 refactor(plugin): 添加生命周期和中间件注册的文档 refactor(service): 完善服务层方法的文档 refactor(controller): 添加控制器方法的详细文档 refactor(crud): 完善CRUD操作的文档字符串 refactor(schema): 简化模型类并移除冗余字段 refactor(param): 更新查询参数类的注释格式 refactor(template): 优化代码生成模板的格式 refactor(console): 添加控制台输出功能的实现 refactor(util): 完善工具函数的文档字符串
134 lines
4.6 KiB
Python
134 lines
4.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from typing import List, Dict, Optional
|
|
|
|
from app.core.base_schema import BatchSetAvailable
|
|
from app.core.exceptions import CustomException
|
|
from app.core.logger import logger
|
|
from app.api.v1.module_system.auth.schema import AuthSchema
|
|
from .schema import ApplicationCreateSchema, ApplicationUpdateSchema, ApplicationOutSchema
|
|
from .param import ApplicationQueryParam
|
|
from .crud import ApplicationCRUD
|
|
|
|
|
|
class ApplicationService:
|
|
"""
|
|
应用系统管理服务层
|
|
"""
|
|
|
|
@classmethod
|
|
async def detail_service(cls, auth: AuthSchema, id: int) -> Dict:
|
|
"""
|
|
获取应用详情
|
|
|
|
参数:
|
|
- auth (AuthSchema): 认证信息模型
|
|
- id (int): 应用ID
|
|
|
|
返回:
|
|
- Dict: 应用详情字典
|
|
"""
|
|
obj = await ApplicationCRUD(auth).get_by_id_crud(id=id)
|
|
if not obj:
|
|
raise CustomException(msg='应用不存在')
|
|
return ApplicationOutSchema.model_validate(obj).model_dump()
|
|
|
|
@classmethod
|
|
async def list_service(cls, auth: AuthSchema, search: Optional[ApplicationQueryParam] = None, order_by: Optional[List[Dict[str, str]]] = None) -> List[Dict]:
|
|
"""
|
|
获取应用列表
|
|
|
|
参数:
|
|
- auth (AuthSchema): 认证信息模型
|
|
- search (Optional[ApplicationQueryParam]): 查询参数模型
|
|
- order_by (Optional[List[Dict[str, str]]]): 排序参数列表
|
|
|
|
返回:
|
|
- List[Dict]: 应用详情字典列表
|
|
"""
|
|
if order_by:
|
|
order_by = eval(order_by) if isinstance(order_by, str) else order_by
|
|
|
|
# 过滤空值
|
|
search_dict = {k: v for k, v in search.__dict__.items() if v is not None} if search else {}
|
|
obj_list = await ApplicationCRUD(auth).list_crud(search=search_dict, order_by=order_by)
|
|
return [ApplicationOutSchema.model_validate(obj).model_dump() for obj in obj_list]
|
|
|
|
@classmethod
|
|
async def create_service(cls, auth: AuthSchema, data: ApplicationCreateSchema) -> Dict:
|
|
"""
|
|
创建应用
|
|
|
|
参数:
|
|
- auth (AuthSchema): 认证信息模型
|
|
- data (ApplicationCreateSchema): 应用创建模型
|
|
|
|
返回:
|
|
- Dict: 应用详情字典
|
|
"""
|
|
# 检查名称是否重复
|
|
obj = await ApplicationCRUD(auth).get(name=data.name)
|
|
if obj:
|
|
raise CustomException(msg='创建失败,应用名称已存在')
|
|
|
|
obj = await ApplicationCRUD(auth).create_crud(data=data)
|
|
return ApplicationOutSchema.model_validate(obj).model_dump()
|
|
|
|
@classmethod
|
|
async def update_service(cls, auth: AuthSchema, id: int, data: ApplicationUpdateSchema) -> Dict:
|
|
"""
|
|
更新应用
|
|
|
|
参数:
|
|
- auth (AuthSchema): 认证信息模型
|
|
- id (int): 应用ID
|
|
- data (ApplicationUpdateSchema): 应用更新模型
|
|
|
|
返回:
|
|
- Dict: 应用详情字典
|
|
"""
|
|
obj = await ApplicationCRUD(auth).get_by_id_crud(id=id)
|
|
if not obj:
|
|
raise CustomException(msg='更新失败,该应用不存在')
|
|
|
|
# 检查名称重复
|
|
exist_obj = await ApplicationCRUD(auth).get(name=data.name)
|
|
if exist_obj and exist_obj.id != id:
|
|
raise CustomException(msg='更新失败,应用名称重复')
|
|
|
|
obj = await ApplicationCRUD(auth).update_crud(id=id, data=data)
|
|
return ApplicationOutSchema.model_validate(obj).model_dump()
|
|
|
|
@classmethod
|
|
async def delete_service(cls, auth: AuthSchema, ids: list[int]) -> None:
|
|
"""
|
|
删除应用
|
|
|
|
参数:
|
|
- auth (AuthSchema): 认证信息模型
|
|
- ids (list[int]): 应用ID列表
|
|
|
|
返回:
|
|
- None
|
|
"""
|
|
if len(ids) < 1:
|
|
raise CustomException(msg='删除失败,删除对象不能为空')
|
|
for id in ids:
|
|
obj = await ApplicationCRUD(auth).get_by_id_crud(id=id)
|
|
if not obj:
|
|
raise CustomException(msg=f'删除失败,应用 {id} 不存在')
|
|
await ApplicationCRUD(auth).delete_crud(ids=ids)
|
|
|
|
@classmethod
|
|
async def set_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
|
|
"""
|
|
批量设置应用状态
|
|
|
|
参数:
|
|
- auth (AuthSchema): 认证信息模型
|
|
- data (BatchSetAvailable): 批量设置应用状态模型
|
|
|
|
返回:
|
|
- None
|
|
"""
|
|
await ApplicationCRUD(auth).set_available_crud(ids=data.ids, status=data.status) |