Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add resource for routes #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

keweegen
Copy link

Examples

Create handlers

func bookHandlers() rest.ResourceHandlers {
	return rest.ResourceHandlers{
		"Index": controllers.FindBooks,
		"Create": controllers.CreateBook,
		"Show": controllers.FindBook,
		"Update": controllers.UpdateBook,
		"Delete": controllers.DeleteBook,
	}
}

Initialize resource routes

func SetupRouter(r *gin.Engine) *gin.Engine {
	rest.Resource(r.Group("books"), bookHandlers(), rest.ResourceOptions{})
	rest.Resource(
		e.Group("/api/v1/tests"),
		bookHandlers(),
		rest.ResourceOptions{
			Param:   "customParam",
			Exclude: []string{"Create", "Update", "Delete"},
		},
	)
	return r
}

Run

go run .

output:
[GIN-debug] PUT    /books/:book              --> go-api/controllers.UpdateBook (3 handlers)
[GIN-debug] DELETE /books/:book              --> go-api/controllers.DeleteBook (3 handlers)
[GIN-debug] GET    /books                    --> go-api/controllers.FindBooks (3 handlers)
[GIN-debug] POST   /books                    --> go-api/controllers.CreateBook (3 handlers)
[GIN-debug] GET    /books/:book              --> go-api/controllers.FindBook (3 handlers)
[GIN-debug] GET    /api/v1/tests             --> go-api/controllers.FindBooks (3 handlers)
[GIN-debug] GET    /api/v1/tests/:customParam --> go-api/controllers.FindBook (3 handlers)
[GIN-debug] Listening and serving HTTP on 127.0.0.1:8080

Comment on lines +38 to +42
"Index": routeMethod{Name: "Index", HttpMethod: http.MethodGet},
"Create": routeMethod{Name: "Create", HttpMethod: http.MethodPost},
"Show": routeMethod{Name: "Show", HttpMethod: http.MethodGet, Param: param},
"Update": routeMethod{Name: "Update", HttpMethod: http.MethodPut, Param: param},
"Delete": routeMethod{Name: "Delete", HttpMethod: http.MethodDelete, Param: param},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'd go with having enum-style indexes, rather than strings, to avoid possibility of having a typo:

const (
	MethodIndex = iota
	MethodCreate
	MethodShow
	MethodUpdate
	MethodDelete
)

But I agree your way the code looks more readable.

Copy link
Contributor

@dieselburner dieselburner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good addition, just a small concern regarding query param handling.

Comment on lines +103 to +104
param := stringy.New(name).KebabCase().ToLower()
paramSingular := pluralize.NewClient().Singular(param)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why you did this magic on routeMethod.Param. I guess you probably were thinking about the safety and bad names specified by users? I would remove this and keep this up to the users to think about what is allowed and what is not in the query string.
Also, this adds two unnecessary dependencies on 3rd-party packages:

"github.com/gertd/go-pluralize"
"github.com/gobeam/stringy"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants