mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 21:06:39 +08:00
provide CRUD API as a sub-package of Gin
This commit is contained in:
parent
d5a0029191
commit
47320dd923
41
gin.go
41
gin.go
@ -205,47 +205,6 @@ func (group *RouterGroup) HEAD(path string, handlers ...HandlerFunc) {
|
|||||||
group.Handle("HEAD", path, handlers)
|
group.Handle("HEAD", path, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// All of the methods are the same type as HandlerFunc
|
|
||||||
// if you don't want to support any methods of CRUD, then don't implement it
|
|
||||||
type CreateSupported interface {
|
|
||||||
CreateHandler(*Context)
|
|
||||||
}
|
|
||||||
type ListSupported interface {
|
|
||||||
ListHandler(*Context)
|
|
||||||
}
|
|
||||||
type TakeSupported interface {
|
|
||||||
TakeHandler(*Context)
|
|
||||||
}
|
|
||||||
type UpdateSupported interface {
|
|
||||||
UpdateHandler(*Context)
|
|
||||||
}
|
|
||||||
type DeleteSupported interface {
|
|
||||||
DeleteHandler(*Context)
|
|
||||||
}
|
|
||||||
|
|
||||||
// It defines
|
|
||||||
// POST: /path
|
|
||||||
// GET: /path
|
|
||||||
// PUT: /path/:id
|
|
||||||
// POST: /path/:id
|
|
||||||
func (group *RouterGroup) CRUD(path string, resource interface{}) {
|
|
||||||
if resource, ok := resource.(CreateSupported); ok {
|
|
||||||
group.POST(path, resource.CreateHandler)
|
|
||||||
}
|
|
||||||
if resource, ok := resource.(ListSupported); ok {
|
|
||||||
group.GET(path, resource.ListHandler)
|
|
||||||
}
|
|
||||||
if resource, ok := resource.(TakeSupported); ok {
|
|
||||||
group.GET(path+"/:id", resource.TakeHandler)
|
|
||||||
}
|
|
||||||
if resource, ok := resource.(UpdateSupported); ok {
|
|
||||||
group.PUT(path+"/:id", resource.UpdateHandler)
|
|
||||||
}
|
|
||||||
if resource, ok := resource.(DeleteSupported); ok {
|
|
||||||
group.DELETE(path+"/:id", resource.DeleteHandler)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Static serves files from the given file system root.
|
// Static serves files from the given file system root.
|
||||||
// Internally a http.FileServer is used, therefore http.NotFound is used instead
|
// Internally a http.FileServer is used, therefore http.NotFound is used instead
|
||||||
// of the Router's NotFound handler.
|
// of the Router's NotFound handler.
|
||||||
|
46
rest/rest.go
Normal file
46
rest/rest.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package rest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// All of the methods are the same type as HandlerFunc
|
||||||
|
// if you don't want to support any methods of CRUD, then don't implement it
|
||||||
|
type CreateSupported interface {
|
||||||
|
CreateHandler(*gin.Context)
|
||||||
|
}
|
||||||
|
type ListSupported interface {
|
||||||
|
ListHandler(*gin.Context)
|
||||||
|
}
|
||||||
|
type TakeSupported interface {
|
||||||
|
TakeHandler(*gin.Context)
|
||||||
|
}
|
||||||
|
type UpdateSupported interface {
|
||||||
|
UpdateHandler(*gin.Context)
|
||||||
|
}
|
||||||
|
type DeleteSupported interface {
|
||||||
|
DeleteHandler(*gin.Context)
|
||||||
|
}
|
||||||
|
|
||||||
|
// It defines
|
||||||
|
// POST: /path
|
||||||
|
// GET: /path
|
||||||
|
// PUT: /path/:id
|
||||||
|
// POST: /path/:id
|
||||||
|
func CRUD(group *gin.RouterGroup, path string, resource interface{}) {
|
||||||
|
if resource, ok := resource.(CreateSupported); ok {
|
||||||
|
group.POST(path, resource.CreateHandler)
|
||||||
|
}
|
||||||
|
if resource, ok := resource.(ListSupported); ok {
|
||||||
|
group.GET(path, resource.ListHandler)
|
||||||
|
}
|
||||||
|
if resource, ok := resource.(TakeSupported); ok {
|
||||||
|
group.GET(path+"/:id", resource.TakeHandler)
|
||||||
|
}
|
||||||
|
if resource, ok := resource.(UpdateSupported); ok {
|
||||||
|
group.PUT(path+"/:id", resource.UpdateHandler)
|
||||||
|
}
|
||||||
|
if resource, ok := resource.(DeleteSupported); ok {
|
||||||
|
group.DELETE(path+"/:id", resource.DeleteHandler)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user