make each handlers of CRUD API optional

This commit is contained in:
yuyabe 2014-08-19 21:29:18 -07:00
parent 0330860e54
commit 30ead9600c

21
gin.go
View File

@ -212,6 +212,8 @@ type Resource interface {
ReadHandler(*Context) ReadHandler(*Context)
UpdateHandler(*Context) UpdateHandler(*Context)
DeleteHandler(*Context) DeleteHandler(*Context)
Supported(string) bool
} }
// It defines // It defines
@ -220,10 +222,21 @@ type Resource interface {
// 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 Resource) {
group.POST(path, resource.CreateHandler) if resource.Supported("C") {
group.GET(path, resource.ReadHandler) group.POST(path, resource.CreateHandler)
group.PUT(path+"/:id", resource.UpdateHandler) }
group.DELETE(path+"/:id", resource.DeleteHandler)
if resource.Supported("R") {
group.GET(path, resource.ReadHandler)
}
if resource.Supported("U") {
group.PUT(path+"/:id", resource.UpdateHandler)
}
if resource.Supported("D") {
group.DELETE(path+"/:id", resource.DeleteHandler)
}
} }
// Static serves files from the given file system root. // Static serves files from the given file system root.