controller.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # -*- coding: utf-8 -*-
  2. from fastapi import APIRouter, Body, Depends, Path
  3. from fastapi.responses import JSONResponse
  4. from app.common.response import SuccessResponse
  5. from app.common.request import PaginationService
  6. from app.core.base_params import PaginationQueryParam
  7. from app.core.dependencies import AuthPermission
  8. from app.core.base_schema import BatchSetAvailable
  9. from app.core.logger import log
  10. from app.api.v1.module_system.auth.schema import AuthSchema
  11. from app.core.router_class import OperationLogRoute
  12. from .service import ApplicationService
  13. from .schema import (
  14. ApplicationCreateSchema,
  15. ApplicationUpdateSchema,
  16. ApplicationQueryParam
  17. )
  18. MyAppRouter = APIRouter(route_class=OperationLogRoute, prefix="/myapp", tags=["应用管理"])
  19. @MyAppRouter.get("/detail/{id}", summary="获取应用详情", description="获取应用详情")
  20. async def get_obj_detail_controller(
  21. id: int = Path(..., description="应用ID"),
  22. auth: AuthSchema = Depends(AuthPermission(["module_application:myapp:query"]))
  23. ) -> JSONResponse:
  24. """
  25. 获取应用详情
  26. 参数:
  27. - id (int): 应用ID
  28. - auth (AuthSchema): 认证信息模型
  29. 返回:
  30. - JSONResponse: 包含应用详情的JSON响应
  31. """
  32. result_dict = await ApplicationService.detail_service(id=id, auth=auth)
  33. log.info(f"获取应用详情成功 {id}")
  34. return SuccessResponse(data=result_dict, msg="获取应用详情成功")
  35. @MyAppRouter.get("/list", summary="查询应用列表", description="查询应用列表")
  36. async def get_obj_list_controller(
  37. page: PaginationQueryParam = Depends(),
  38. search: ApplicationQueryParam = Depends(),
  39. auth: AuthSchema = Depends(AuthPermission(["module_application:myapp:query"]))
  40. ) -> JSONResponse:
  41. """
  42. 查询应用列表
  43. 参数:
  44. - page (PaginationQueryParam): 分页参数模型
  45. - search (ApplicationQueryParam): 查询参数模型
  46. - auth (AuthSchema): 认证信息模型
  47. 返回:
  48. - JSONResponse: 包含应用列表的JSON响应
  49. """
  50. result_dict_list = await ApplicationService.list_service(auth=auth, search=search, order_by=page.order_by)
  51. result_dict = await PaginationService.paginate(data_list=result_dict_list, page_no=page.page_no, page_size=page.page_size)
  52. log.info(f"查询应用列表成功")
  53. return SuccessResponse(data=result_dict, msg="查询应用列表成功")
  54. @MyAppRouter.post("/create", summary="创建应用", description="创建应用")
  55. async def create_obj_controller(
  56. data: ApplicationCreateSchema,
  57. auth: AuthSchema = Depends(AuthPermission(["module_application:myapp:create"]))
  58. ) -> JSONResponse:
  59. """
  60. 创建应用
  61. 参数:
  62. - data (ApplicationCreateSchema): 应用创建模型
  63. - auth (AuthSchema): 认证信息模型
  64. 返回:
  65. - JSONResponse: 包含创建应用详情的JSON响应
  66. """
  67. result_dict = await ApplicationService.create_service(auth=auth, data=data)
  68. log.info(f"创建应用成功: {result_dict}")
  69. return SuccessResponse(data=result_dict, msg="创建应用成功")
  70. @MyAppRouter.put("/update/{id}", summary="修改应用", description="修改应用")
  71. async def update_obj_controller(
  72. data: ApplicationUpdateSchema,
  73. id: int = Path(..., description="应用ID"),
  74. auth: AuthSchema = Depends(AuthPermission(["module_application:myapp:update"]))
  75. ) -> JSONResponse:
  76. """
  77. 修改应用
  78. 参数:
  79. - data (ApplicationUpdateSchema): 应用更新模型
  80. - id (int): 应用ID
  81. - auth (AuthSchema): 认证信息模型
  82. 返回:
  83. - JSONResponse: 包含修改应用详情的JSON响应
  84. """
  85. result_dict = await ApplicationService.update_service(auth=auth, id=id, data=data)
  86. log.info(f"修改应用成功: {result_dict}")
  87. return SuccessResponse(data=result_dict, msg="修改应用成功")
  88. @MyAppRouter.delete("/delete", summary="删除应用", description="删除应用")
  89. async def delete_obj_controller(
  90. ids: list[int] = Body(..., description="ID列表"),
  91. auth: AuthSchema = Depends(AuthPermission(["module_application:myapp:delete"]))
  92. ) -> JSONResponse:
  93. """
  94. 删除应用
  95. 参数:
  96. - ids (list[int]): 应用ID列表
  97. - auth (AuthSchema): 认证信息模型
  98. 返回:
  99. - JSONResponse: 包含删除应用详情的JSON响应
  100. """
  101. await ApplicationService.delete_service(auth=auth, ids=ids)
  102. log.info(f"删除应用成功: {ids}")
  103. return SuccessResponse(msg="删除应用成功")
  104. @MyAppRouter.patch("/available/setting", summary="批量修改应用状态", description="批量修改应用状态")
  105. async def batch_set_available_obj_controller(
  106. data: BatchSetAvailable,
  107. auth: AuthSchema = Depends(AuthPermission(["module_application:myapp:patch"]))
  108. ) -> JSONResponse:
  109. """
  110. 批量修改应用状态
  111. 参数:
  112. - data (BatchSetAvailable): 批量修改应用状态模型
  113. - auth (AuthSchema): 认证信息模型
  114. 返回:
  115. - JSONResponse: 批量修改应用状态成功
  116. """
  117. await ApplicationService.set_available_service(auth=auth, data=data)
  118. log.info(f"批量修改应用状态成功: {data.ids}")
  119. return SuccessResponse(msg="批量修改应用状态成功")