@@ -32,50 +32,40 @@ func mapAndSendError(c *PigBatchController, ctx *gin.Context, action string, err
// idExtractorFunc 定义了一个函数类型, 用于从gin.Context中提取主ID。
type idExtractorFunc func ( ctx * gin . Context ) ( uint , error )
// handleAPIRequest 封装了控制器中处理带有请求体和路径参数的API请求 的通用逻辑。
// 它负责请求体绑定、操作员ID获取、服务层调用、错误映射和响应发送 。
// extractOperatorAndPrimaryID 封装了从gin.Context中提取操作员ID和主ID 的通用逻辑。
// 它负责处理ID提取过程中的错误, 并发送相应的HTTP响应 。
//
// 参数:
//
// c: *PigBatchController - 控制器实例,用于访问其服务和 日志。
// c: *PigBatchController - 控制器实例,用于访问其日志。
// ctx: *gin.Context - Gin上下文。
// action: string - 当前操作的描述,用于日志和审计。
// reqDTO: Req - 请求数据传输对象。
// serviceExecutor: func(ctx *gin.Context, operatorID uint, primaryID uint, req Req) error - 实际执行服务层逻辑的回调函数。
// 这个回调函数负责从ctx中解析所有必要的路径参数( 除了primaryID, 如果idExtractorFunc提供了) ,
// 获取operatorID, 并调用具体的服务方法。
// successMsg: string - 操作成功时返回给客户端的消息。
// idExtractor: idExtractorFunc - 可选函数, 用于从ctx中提取主ID。如果为nil, 则尝试从":id"路径参数中提取。
func handleAPIRequest [ Req any ] ( // 使用泛型Req
//
// 返回值:
//
// operatorID: uint - 提取到的操作员ID。
// primaryID: uint - 提取到的主ID。
// ok: bool - 如果ID提取成功且没有发送错误响应, 则为true。
func extractOperatorAndPrimaryID (
c * PigBatchController ,
ctx * gin . Context ,
action string ,
reqDTO Req ,
serviceExecutor func ( ctx * gin . Context , operatorID uint , primaryID uint , req Req ) error ,
successMsg string ,
idExtractor idExtractorFunc ,
) {
var primaryID uint // 用于审计和日志的主 ID
// 1. 绑定请求体
if err := ctx . ShouldBindJSON ( & reqDTO ) ; err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeBadRequest , "无效的请求体" , action , "请求体绑定失败" , reqDTO )
return
}
// 2. 获取操作员ID
) ( operatorID uint , primaryID uint , ok bool ) {
// 1. 获取操作员 ID
operatorID , err := controller . GetOperatorIDFromContext ( ctx )
if err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeUnauthorized , "未授权" , action , "无法获取操作员ID" , nil )
return
return 0 , 0 , false
}
// 3 . 提取主ID
// 2 . 提取主ID
if idExtractor != nil {
primaryID , err = idExtractor ( ctx )
if err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeBadRequest , "无效的ID格式" , action , "ID格式错误" , err . Error ( ) )
return
return 0 , 0 , false
}
} else { // 默认从 ":id" 路径参数提取
idParam := ctx . Param ( "id" )
@@ -85,20 +75,46 @@ func handleAPIRequest[Req any]( // 使用泛型Req
parsedID , err := strconv . ParseUint ( idParam , 10 , 32 )
if err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeBadRequest , "无效的ID格式" , action , "ID格式错误" , idParam )
return
return 0 , 0 , false
}
primaryID = uint ( parsedID )
}
}
// 4. 执行服务层逻辑
err = serviceExecutor ( ctx , operatorID , primaryID , reqDTO )
return operatorID , primaryID , true
}
// handleAPIRequest 封装了控制器中处理带有请求体和路径参数的API请求的通用逻辑。
// 它负责请求体绑定、操作员ID获取、服务层调用、错误映射和响应发送。
func handleAPIRequest [ Req any ] (
c * PigBatchController ,
ctx * gin . Context ,
action string ,
reqDTO Req ,
serviceExecutor func ( ctx * gin . Context , operatorID uint , primaryID uint , req Req ) error ,
successMsg string ,
idExtractor idExtractorFunc ,
) {
// 1. 绑定请求体
if err := ctx . ShouldBindJSON ( & reqDTO ) ; err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeBadRequest , "无效的请求体" , action , "请求体绑定失败" , reqDTO )
return
}
// 2. 提取操作员ID和主ID
operatorID , primaryID , ok := extractOperatorAndPrimaryID ( c , ctx , action , idExtractor )
if ! ok {
return // 错误已在 extractOperatorAndPrimaryID 中处理
}
// 3. 执行服务层逻辑
err := serviceExecutor ( ctx , operatorID , primaryID , reqDTO )
if err != nil {
mapAndSendError ( c , ctx , action , err , primaryID )
return
}
// 5 . 发送成功响应
// 4 . 发送成功响应
controller . SendSuccessWithAudit ( ctx , controller . CodeSuccess , successMsg , nil , action , successMsg , primaryID )
}
@@ -111,44 +127,20 @@ func handleNoBodyAPIRequest(
successMsg string ,
idExtractor idExtractorFunc ,
) {
var primaryID uint // 用于审计和日志的 主ID
// 1. 获取操作员ID
operatorID , err := controller . GetOperatorIDFromContext ( ctx )
if err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeUnauthorized , "未授权" , action , "无法获取操作员ID" , nil )
return
// 1. 提取操作员ID和 主ID
operatorID , primaryID , ok := extractOperatorAndPrimaryID ( c , ctx , action , idExtractor )
if ! ok {
return // 错误已在 extractOperatorAndPrimaryID 中处理
}
// 2. 提取主ID
if idExt rac tor != nil {
primaryID , err = idExtractor ( ctx )
if err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeBadRequest , "无效的ID格式" , action , "ID格式错误" , err . Error ( ) )
return
}
} else { // 默认从 ":id" 路径参数提取
idParam := ctx . Param ( "id" )
if idParam == "" {
// 如果没有ID参数且没有自定义提取器, primaryID保持为0
} else {
parsedID , err := strconv . ParseUint ( idParam , 10 , 32 )
if err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeBadRequest , "无效的ID格式" , action , "ID格式错误" , idParam )
return
}
primaryID = uint ( parsedID )
}
}
// 3. 执行服务层逻辑
err = serviceExecutor ( ctx , operatorID , primaryID )
// 2. 执行服务层逻辑
err := serviceExecutor ( ctx , ope ratorID , primaryID )
if err != nil {
mapAndSendError ( c , ctx , action , err , primaryID )
return
}
// 4 . 发送成功响应
// 3 . 发送成功响应
controller . SendSuccessWithAudit ( ctx , controller . CodeSuccess , successMsg , nil , action , successMsg , primaryID )
}
@@ -162,50 +154,26 @@ func handleAPIRequestWithResponse[Req any, Resp any](
successMsg string ,
idExtractor idExtractorFunc ,
) {
var primaryID uint // 用于审计和日志的主ID
// 1. 绑定请求体
if err := ctx . ShouldBindJSON ( & reqDTO ) ; err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeBadRequest , "无效的请求体" , action , "请求体绑定失败" , reqDTO )
return
}
// 2. 获 取操作员ID
operatorID , err := controller . GetOperatorIDFromContext ( ctx )
if err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeUnauthorized , "未授权" , action , "无法获取操作员ID" , nil )
return
// 2. 提 取操作员ID和主ID
operatorID , primaryID , ok := extractOperatorAndPrimaryID ( c , ctx , action , idExtractor )
if ! ok {
return // 错误已在 extractOperatorAndPrimaryID 中处理
}
// 3. 提取主ID
if idExtractor != nil {
primaryID , err = idExtractor ( ctx )
if err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeBadRequest , "无效的ID格式" , action , "ID格式错误" , err . Error ( ) )
return
}
} else { // 默认从 ":id" 路径参数提取
idParam := ctx . Param ( "id" )
if idParam == "" { // 有些端点可能没有 "id" 参数,例如列表或创建操作
// 如果没有ID参数且没有自定义提取器, primaryID保持为0, 这对于某些操作是可接受的
} else {
parsedID , err := strconv . ParseUint ( idParam , 10 , 32 )
if err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeBadRequest , "无效的ID格式" , action , "ID格式错误" , idParam )
return
}
primaryID = uint ( parsedID )
}
}
// 4. 执行服务层逻辑
// 3. 执行服务层逻辑
respDTO , err := serviceExecutor ( ctx , operatorID , primaryID , reqDTO )
if err != nil {
mapAndSendError ( c , ctx , action , err , primaryID )
return
}
// 5 . 发送成功响应
// 4 . 发送成功响应
controller . SendSuccessWithAudit ( ctx , controller . CodeSuccess , successMsg , respDTO , action , successMsg , primaryID )
}
@@ -218,44 +186,20 @@ func handleNoBodyAPIRequestWithResponse[Resp any](
successMsg string ,
idExtractor idExtractorFunc ,
) {
var primaryID uint // 用于审计和日志的 主ID
// 1. 获取操作员ID
operatorID , err := controller . GetOperatorIDFromContext ( ctx )
if err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeUnauthorized , "未授权" , action , "无法获取操作员ID" , nil )
return
// 1. 提取操作员ID和 主ID
operatorID , primaryID , ok := extractOperatorAndPrimaryID ( c , ctx , action , idExtractor )
if ! ok {
return // 错误已在 extractOperatorAndPrimaryID 中处理
}
// 2. 提取主ID
if idExtractor != nil {
primaryID , err = idExtractor ( ctx )
if err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeBadRequest , "无效的ID格式" , action , "ID格式错误" , err . Error ( ) )
return
}
} else { // 默认从 ":id" 路径参数提取
idParam := ctx . Param ( "id" )
if idParam == "" {
// 如果没有ID参数且没有自定义提取器, primaryID保持为0
} else {
parsedID , err := strconv . ParseUint ( idParam , 10 , 32 )
if err != nil {
controller . SendErrorWithAudit ( ctx , controller . CodeBadRequest , "无效的ID格式" , action , "ID格式错误" , idParam )
return
}
primaryID = uint ( parsedID )
}
}
// 3. 执行服务层逻辑
// 2. 执行服务层逻辑
respDTO , err := serviceExecutor ( ctx , operatorID , primaryID )
if err != nil {
mapAndSendError ( c , ctx , action , err , primaryID )
return
}
// 4 . 发送成功响应
// 3 . 发送成功响应
controller . SendSuccessWithAudit ( ctx , controller . CodeSuccess , successMsg , respDTO , action , successMsg , primaryID )
}