mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 20:22:20 +08:00
make each handlers of CRUD API optional
This commit is contained in:
parent
0330860e54
commit
30ead9600c
21
gin.go
21
gin.go
@ -212,6 +212,8 @@ type Resource interface {
|
||||
ReadHandler(*Context)
|
||||
UpdateHandler(*Context)
|
||||
DeleteHandler(*Context)
|
||||
|
||||
Supported(string) bool
|
||||
}
|
||||
|
||||
// It defines
|
||||
@ -220,10 +222,21 @@ type Resource interface {
|
||||
// PUT: /path/:id
|
||||
// POST: /path/:id
|
||||
func (group *RouterGroup) CRUD(path string, resource Resource) {
|
||||
group.POST(path, resource.CreateHandler)
|
||||
group.GET(path, resource.ReadHandler)
|
||||
group.PUT(path+"/:id", resource.UpdateHandler)
|
||||
group.DELETE(path+"/:id", resource.DeleteHandler)
|
||||
if resource.Supported("C") {
|
||||
group.POST(path, resource.CreateHandler)
|
||||
}
|
||||
|
||||
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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user