...

Source file src/github.com/gin-gonic/contrib/rest/rest.go

Documentation: github.com/gin-gonic/contrib/rest

     1  package rest
     2  
     3  import (
     4  	"github.com/gin-gonic/gin"
     5  )
     6  
     7  // All of the methods are the same type as HandlerFunc
     8  // if you don't want to support any methods of CRUD, then don't implement it
     9  type CreateSupported interface {
    10  	CreateHandler(*gin.Context)
    11  }
    12  type ListSupported interface {
    13  	ListHandler(*gin.Context)
    14  }
    15  type TakeSupported interface {
    16  	TakeHandler(*gin.Context)
    17  }
    18  type UpdateSupported interface {
    19  	UpdateHandler(*gin.Context)
    20  }
    21  type DeleteSupported interface {
    22  	DeleteHandler(*gin.Context)
    23  }
    24  
    25  // It defines
    26  //   POST: /path
    27  //   GET:  /path
    28  //   PUT:  /path/:id
    29  //   POST: /path/:id
    30  func CRUD(group *gin.RouterGroup, path string, resource interface{}) {
    31  	if resource, ok := resource.(CreateSupported); ok {
    32  		group.POST(path, resource.CreateHandler)
    33  	}
    34  	if resource, ok := resource.(ListSupported); ok {
    35  		group.GET(path, resource.ListHandler)
    36  	}
    37  	if resource, ok := resource.(TakeSupported); ok {
    38  		group.GET(path+"/:id", resource.TakeHandler)
    39  	}
    40  	if resource, ok := resource.(UpdateSupported); ok {
    41  		group.PUT(path+"/:id", resource.UpdateHandler)
    42  	}
    43  	if resource, ok := resource.(DeleteSupported); ok {
    44  		group.DELETE(path+"/:id", resource.DeleteHandler)
    45  	}
    46  }
    47  

View as plain text