Delete Resource interface and add Supported interfaces for each CRUD functions

This commit is contained in:
yuyabe 2014-08-19 21:58:45 -07:00
parent 30ead9600c
commit d5adf63fa1

25
gin.go
View File

@ -205,15 +205,19 @@ func (group *RouterGroup) HEAD(path string, handlers ...HandlerFunc) {
group.Handle("HEAD", path, handlers) group.Handle("HEAD", path, handlers)
} }
// Resouce API
// All of the methods are the same type as HandlerFunc // All of the methods are the same type as HandlerFunc
type Resource interface { // if you don't want to support any methods of CRUD, then don't implement it
type CreateSupported interface {
CreateHandler(*Context) CreateHandler(*Context)
}
type ReadSupported interface {
ReadHandler(*Context) ReadHandler(*Context)
}
type UpdateSupported interface {
UpdateHandler(*Context) UpdateHandler(*Context)
}
type DeleteSupported interface {
DeleteHandler(*Context) DeleteHandler(*Context)
Supported(string) bool
} }
// It defines // It defines
@ -221,20 +225,17 @@ type Resource interface {
// GET: /path // GET: /path
// PUT: /path/:id // PUT: /path/:id
// POST: /path/:id // POST: /path/:id
func (group *RouterGroup) CRUD(path string, resource Resource) { func (group *RouterGroup) CRUD(path string, resource interface{}) {
if resource.Supported("C") { if resource, ok := resource.(CreateSupported); ok {
group.POST(path, resource.CreateHandler) group.POST(path, resource.CreateHandler)
} }
if resource, ok := resource.(ReadSupported); ok {
if resource.Supported("R") {
group.GET(path, resource.ReadHandler) group.GET(path, resource.ReadHandler)
} }
if resource, ok := resource.(UpdateSupported); ok {
if resource.Supported("U") {
group.PUT(path+"/:id", resource.UpdateHandler) group.PUT(path+"/:id", resource.UpdateHandler)
} }
if resource, ok := resource.(DeleteSupported); ok {
if resource.Supported("D") {
group.DELETE(path+"/:id", resource.DeleteHandler) group.DELETE(path+"/:id", resource.DeleteHandler)
} }
} }