Skip to content

Commit

Permalink
feat: Resource Route (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi authored Apr 26, 2023
1 parent 64e0e09 commit 667cb1e
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
7 changes: 7 additions & 0 deletions contracts/http/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import (

type Middleware func(Context)
type HandlerFunc func(Context)
type ResourceController interface {
Index(Context)
Show(Context)
Store(Context)
Update(Context)
Destroy(Context)
}

//go:generate mockery --name=Context
type Context interface {
Expand Down
1 change: 1 addition & 0 deletions contracts/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Route interface {
Patch(relativePath string, handler contractshttp.HandlerFunc)
Put(relativePath string, handler contractshttp.HandlerFunc)
Options(relativePath string, handler contractshttp.HandlerFunc)
Resource(relativePath string, controller contractshttp.ResourceController)

Static(relativePath, root string)
StaticFile(relativePath, filepath string)
Expand Down
9 changes: 9 additions & 0 deletions route/gin_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ func (r *GinGroup) Options(relativePath string, handler httpcontract.HandlerFunc
r.getGinRoutesWithMiddlewares().OPTIONS(pathToGinPath(relativePath), []gin.HandlerFunc{handlerToGinHandler(handler)}...)
}

func (r *GinGroup) Resource(relativePath string, controller httpcontract.ResourceController) {
r.getGinRoutesWithMiddlewares().GET(pathToGinPath(relativePath), []gin.HandlerFunc{handlerToGinHandler(controller.Index)}...)
r.getGinRoutesWithMiddlewares().POST(pathToGinPath(relativePath), []gin.HandlerFunc{handlerToGinHandler(controller.Store)}...)
r.getGinRoutesWithMiddlewares().GET(pathToGinPath(relativePath+"/{id}"), []gin.HandlerFunc{handlerToGinHandler(controller.Show)}...)
r.getGinRoutesWithMiddlewares().PUT(pathToGinPath(relativePath+"/{id}"), []gin.HandlerFunc{handlerToGinHandler(controller.Update)}...)
r.getGinRoutesWithMiddlewares().PATCH(pathToGinPath(relativePath+"/{id}"), []gin.HandlerFunc{handlerToGinHandler(controller.Update)}...)
r.getGinRoutesWithMiddlewares().DELETE(pathToGinPath(relativePath+"/{id}"), []gin.HandlerFunc{handlerToGinHandler(controller.Destroy)}...)
}

func (r *GinGroup) Static(relativePath, root string) {
r.getGinRoutesWithMiddlewares().Static(pathToGinPath(relativePath), root)
}
Expand Down
104 changes: 104 additions & 0 deletions route/gin_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,44 @@ import (
"github.com/goravel/framework/testing/mock"
)

type resourceController struct{}

func (c resourceController) Index(ctx httpcontract.Context) {
ctx.Response().Json(http.StatusOK, httpcontract.Json{
"action": "index",
})
}

func (c resourceController) Show(ctx httpcontract.Context) {
id := ctx.Request().Input("id")
ctx.Response().Json(http.StatusOK, httpcontract.Json{
"action": "show",
"id": id,
})
}

func (c resourceController) Store(ctx httpcontract.Context) {
ctx.Response().Json(http.StatusOK, httpcontract.Json{
"action": "store",
})
}

func (c resourceController) Update(ctx httpcontract.Context) {
id := ctx.Request().Input("id")
ctx.Response().Json(http.StatusOK, httpcontract.Json{
"action": "update",
"id": id,
})
}

func (c resourceController) Destroy(ctx httpcontract.Context) {
id := ctx.Request().Input("id")
ctx.Response().Json(http.StatusOK, httpcontract.Json{
"action": "destroy",
"id": id,
})
}

func TestGinGroup(t *testing.T) {
var (
gin *Gin
Expand Down Expand Up @@ -212,6 +250,72 @@ func TestGinGroup(t *testing.T) {
expectCode: http.StatusOK,
expectBody: "{\"id\":\"1\"}",
},
{
name: "Resource Index",
setup: func(req *http.Request) {
resource := resourceController{}
gin.Resource("/resource", resource)
},
method: "GET",
url: "/resource",
expectCode: http.StatusOK,
expectBody: "{\"action\":\"index\"}",
},
{
name: "Resource Show",
setup: func(req *http.Request) {
resource := resourceController{}
gin.Resource("/resource", resource)
},
method: "GET",
url: "/resource/1",
expectCode: http.StatusOK,
expectBody: "{\"action\":\"show\",\"id\":\"1\"}",
},
{
name: "Resource Store",
setup: func(req *http.Request) {
resource := resourceController{}
gin.Resource("/resource", resource)
},
method: "POST",
url: "/resource",
expectCode: http.StatusOK,
expectBody: "{\"action\":\"store\"}",
},
{
name: "Resource Update (PUT)",
setup: func(req *http.Request) {
resource := resourceController{}
gin.Resource("/resource", resource)
},
method: "PUT",
url: "/resource/1",
expectCode: http.StatusOK,
expectBody: "{\"action\":\"update\",\"id\":\"1\"}",
},
{
name: "Resource Update (PATCH)",
setup: func(req *http.Request) {
resource := resourceController{}
gin.Resource("/resource", resource)
},
method: "PATCH",
url: "/resource/1",
expectCode: http.StatusOK,
expectBody: "{\"action\":\"update\",\"id\":\"1\"}",
},
{
name: "Resource Destroy",
setup: func(req *http.Request) {
resource := resourceController{}
gin.Resource("/resource", resource)
},
method: "DELETE",
url: "/resource/1",
expectCode: http.StatusOK,
expectBody: "{\"action\":\"destroy\",\"id\":\"1\"}",
},
{
name: "Static",
setup: func(req *http.Request) {
Expand Down

0 comments on commit 667cb1e

Please sign in to comment.