...
1 package rest
2
3 import (
4 "github.com/gin-gonic/gin"
5 )
6
7
8
9 type CreateSupported interface {
10 CreateHandler(*gin.Context)
11 }
12 type ListSupported interface {
13 ListHandler(*gin.Context)
14 }
15 type TakeSupported interface {
16 TakeHandler(*gin.Context)
17 }
18 type UpdateSupported interface {
19 UpdateHandler(*gin.Context)
20 }
21 type DeleteSupported interface {
22 DeleteHandler(*gin.Context)
23 }
24
25
26
27
28
29
30 func CRUD(group *gin.RouterGroup, path string, resource interface{}) {
31 if resource, ok := resource.(CreateSupported); ok {
32 group.POST(path, resource.CreateHandler)
33 }
34 if resource, ok := resource.(ListSupported); ok {
35 group.GET(path, resource.ListHandler)
36 }
37 if resource, ok := resource.(TakeSupported); ok {
38 group.GET(path+"/:id", resource.TakeHandler)
39 }
40 if resource, ok := resource.(UpdateSupported); ok {
41 group.PUT(path+"/:id", resource.UpdateHandler)
42 }
43 if resource, ok := resource.(DeleteSupported); ok {
44 group.DELETE(path+"/:id", resource.DeleteHandler)
45 }
46 }
47
View as plain text