woj-server/internal/e/resp.go

32 lines
639 B
Go
Raw Permalink Normal View History

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"
)
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"`
}
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(),
Body: utils.If[interface{}](status == Success, body, nil),
2022-09-07 23:34:37 +08:00
}
}
func Pong[T any](c *gin.Context, status Status, body T) {
c.Set("err", status)
c.JSON(http.StatusOK, wrap(status, body))
}