mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-04 07:06:36 +08:00
[优化] 重构 API 处理逻辑,简化参数解析和响应处理
This commit is contained in:
@@ -5,11 +5,12 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const invalidParamsMessage = "鏃犳晥鐨勫弬鏁?"
|
||||
const invalidParamsMessage = "参数错误"
|
||||
|
||||
func respondSuccess(c *gin.Context, data any) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -72,3 +73,20 @@ func decodeOptionalJSONBody(body io.Reader, target any) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseIDParam(c *gin.Context) (uint, bool) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
respondBadRequest(c, "")
|
||||
return 0, false
|
||||
}
|
||||
return uint(id), true
|
||||
}
|
||||
|
||||
func bindJSON(c *gin.Context, target any) bool {
|
||||
if err := decodeJSONBody(c.Request.Body, target); err != nil {
|
||||
respondBadRequest(c, "")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user