forked from worldOneo/crudist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crudist.go
123 lines (109 loc) · 2.9 KB
/
crudist.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package crudist
import (
"fmt"
"log"
"reflect"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// Context is custom for crudist handlers
type Context interface {
JSONBody(obj interface{}) error
JSON(code int, obj interface{}) error
Param(name string) string
}
// HandlerFunc for Crudist
type HandlerFunc func(ctx Context) error
// Crudist base interface definition
type Crudist interface {
// DB returns the db for the crudist instance
DB() *gorm.DB
// Get registers new handlers for http get method
Get(path string, handler ...HandlerFunc)
// Get registers new handlers for http Post method
Post(path string, handler ...HandlerFunc)
// Get registers new handlers for http Patch method
Patch(path string, handler ...HandlerFunc)
// Get registers new handlers for http Delete method
Delete(path string, handler ...HandlerFunc)
}
// Gin creates a new gin operator for crudist
func Gin(server *gin.Engine, db *gorm.DB) *GinOperator {
return &GinOperator{server, db}
}
// ErrorBadRequest when JSON parsing failed
var ErrorBadRequest error = fmt.Errorf("Bad Request")
// ErrorInternalServer when the DB couldn't
var ErrorInternalServer error = fmt.Errorf("Internal Server Error")
// Handle creates handler for GET POST PATCH and DELETE operation for your model
func Handle(crudist Crudist, path string, model interface{}) {
modelType := reflect.TypeOf(model)
sliceType := reflect.SliceOf(modelType)
newModel := func() interface{} {
return reflect.New(modelType).Interface()
}
newModels := func() interface{} {
return reflect.New(sliceType).Interface()
}
crudist.Get(path, func(ctx Context) error {
models := newModels()
fmt.Print(reflect.TypeOf(models))
err := crudist.DB().Find(models).Error
if err != nil {
return err
}
return ctx.JSON(200, models)
})
crudist.Get(path+":id/", func(ctx Context) error {
stringID := ctx.Param("id")
id, err := strconv.Atoi(stringID)
if err != nil {
return ErrorBadRequest
}
model := newModel()
err = crudist.DB().Find(model, id).Error
if err != nil {
return err
}
return ctx.JSON(200, model)
})
crudist.Post(path, func(ctx Context) error {
model := newModel()
err := ctx.JSONBody(model)
if err != nil {
log.Print(err)
return ErrorBadRequest
}
err = crudist.DB().Create(model).Error
if err != nil {
return ErrorInternalServer
}
return ctx.JSON(200, model)
})
crudist.Delete(path, func(ctx Context) error {
model := newModel()
err := ctx.JSONBody(model)
if err != nil {
log.Print(err)
return ErrorBadRequest
}
err = crudist.DB().Delete(model).Error
if err != nil {
return ErrorInternalServer
}
return ctx.JSON(200, model)
})
crudist.Patch(path, func(ctx Context) error {
model := newModel()
err := ctx.JSONBody(model)
if err != nil {
return ErrorBadRequest
}
err = crudist.DB().Save(model).Error
if err != nil {
return ErrorInternalServer
}
return ctx.JSON(200, model)
})
}