2022-09-07 23:34:37 +08:00
|
|
|
package e
|
|
|
|
|
|
|
|
import (
|
2023-07-14 21:47:11 +08:00
|
|
|
"git.0x7f.app/WOJ/woj-server/pkg/utils"
|
2022-09-07 23:34:37 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2023-12-22 15:19:13 +08:00
|
|
|
type Response[T any] struct {
|
|
|
|
Code int `json:"code"`
|
|
|
|
Msg string `json:"msg"`
|
|
|
|
Body T `json:"body"`
|
2022-09-07 23:34:37 +08:00
|
|
|
}
|
|
|
|
|
2023-12-23 15:36:42 +08:00
|
|
|
type WithCount[T any] struct {
|
|
|
|
Count int64 `json:"count"`
|
|
|
|
Data []T `json:"data"`
|
|
|
|
}
|
|
|
|
|
2023-12-22 15:19:13 +08:00
|
|
|
func wrap[T any](status Status, body T) Response[interface{}] {
|
|
|
|
return Response[interface{}]{
|
2022-09-20 14:15:21 +08:00
|
|
|
Code: int(status),
|
|
|
|
Msg: status.String(),
|
2023-12-22 15:19:13 +08:00
|
|
|
Body: utils.If[interface{}](status == Success, body, nil),
|
2022-09-07 23:34:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-22 15:19:13 +08:00
|
|
|
func Pong[T any](c *gin.Context, status Status, body T) {
|
2022-09-26 17:15:31 +08:00
|
|
|
c.Set("err", status)
|
2023-12-22 15:19:13 +08:00
|
|
|
c.JSON(http.StatusOK, wrap(status, body))
|
2022-10-23 17:29:35 +08:00
|
|
|
}
|