/
result.go
61 lines (49 loc) · 1.07 KB
/
result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package ginx
import (
"net/http"
"github.com/gin-gonic/gin"
)
type DataResult[T any] struct {
Success bool `json:"success"`
Data T `json:"data"`
}
func JSON[T any](ctx *gin.Context, v T) {
ctx.JSON(http.StatusOK, DataResult[T]{
Data: v,
Success: true,
})
}
type CreateEntityResult struct {
ID string `json:"id"`
}
type CreateManyEntitiesResult struct {
Count int `json:"count"`
}
type EntityUpdatedResult struct {
Count int `json:"count"`
}
type SuccessWithMessageResult struct {
Success bool `json:"success"`
Msg string `json:"msg"`
}
func EntityCreated(ctx *gin.Context, id string) {
ctx.JSON(http.StatusOK, CreateEntityResult{
ID: id,
})
}
func ManyEntitiesCreated(ctx *gin.Context, count int) {
ctx.JSON(http.StatusOK, CreateManyEntitiesResult{
Count: count,
})
}
func EntityUpdated(ctx *gin.Context, count int) {
ctx.JSON(http.StatusOK, EntityUpdatedResult{
Count: count,
})
}
func SuccessWithMessage(ctx *gin.Context, success bool, msg string) {
ctx.JSON(http.StatusOK, SuccessWithMessageResult{
Success: success,
Msg: msg,
})
}