From 47320dd923392fe184185cfd73988169c1ae2857 Mon Sep 17 00:00:00 2001 From: Yuya Yabe Date: Sun, 31 Aug 2014 16:26:51 -0700 Subject: [PATCH] provide CRUD API as a sub-package of Gin --- gin.go | 41 ----------------------------------------- rest/rest.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 41 deletions(-) create mode 100644 rest/rest.go diff --git a/gin.go b/gin.go index 749ee3a2..106e1b9c 100644 --- a/gin.go +++ b/gin.go @@ -205,47 +205,6 @@ func (group *RouterGroup) HEAD(path string, handlers ...HandlerFunc) { 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. // Internally a http.FileServer is used, therefore http.NotFound is used instead // of the Router's NotFound handler. diff --git a/rest/rest.go b/rest/rest.go new file mode 100644 index 00000000..5a7a3a21 --- /dev/null +++ b/rest/rest.go @@ -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) + } +}