woj-server/internal/e/resp.go

32 lines
639 B
Go

package e
import (
"git.0x7f.app/WOJ/woj-server/pkg/utils"
"github.com/gin-gonic/gin"
"net/http"
)
type Response[T any] struct {
Code int `json:"code"`
Msg string `json:"msg"`
Body T `json:"body"`
}
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{}]{
Code: int(status),
Msg: status.String(),
Body: utils.If[interface{}](status == Success, body, nil),
}
}
func Pong[T any](c *gin.Context, status Status, body T) {
c.Set("err", status)
c.JSON(http.StatusOK, wrap(status, body))
}