From 9d0ab3c0f247618b0f6d8267c59d0f3259bce748 Mon Sep 17 00:00:00 2001 From: Ankit Patial Date: Sun, 17 Aug 2025 20:04:48 +0530 Subject: [PATCH] resource extra handler --- resource.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/resource.go b/resource.go index ae718fa..8c824d1 100644 --- a/resource.go +++ b/resource.go @@ -102,10 +102,38 @@ func (res *Resource) Delete(h http.HandlerFunc) { res.handlerFunc(http.MethodDelete, p, h) } -func (res *Resource) Handle(pattern string, h http.HandlerFunc) { - p := suffixIt(res.pattern, "{id}") - res.routes.Add(http.MethodDelete + " " + p) - res.handlerFunc(http.MethodDelete, p, h) +// HandleGET on /group-pattern/:id/pattern +func (res *Resource) HandleGET(pattern string, h http.HandlerFunc) { + res.handle(http.MethodGet, pattern, h) +} + +// HandlePOST on /group-pattern/:id/pattern +func (res *Resource) HandlePOST(pattern string, h http.HandlerFunc) { + res.handle(http.MethodPost, pattern, h) +} + +// HandlePUT on /group-pattern/:id/pattern +func (res *Resource) HandlePUT(pattern string, h http.HandlerFunc) { + res.handle(http.MethodPut, pattern, h) +} + +// HandlePATCH on /group-pattern/:id/pattern +func (res *Resource) HandlePATCH(pattern string, h http.HandlerFunc) { + res.handle(http.MethodPatch, pattern, h) +} + +// HandleDELETE on /group-pattern/:id/pattern +func (res *Resource) HandleDELETE(pattern string, h http.HandlerFunc) { + res.handle(http.MethodDelete, pattern, h) +} + +func (res *Resource) handle(method string, pattern string, h http.HandlerFunc) { + if !strings.HasPrefix(pattern, "/") { + pattern = "/" + pattern + } + p := suffixIt(res.pattern, "{id}"+pattern) + res.routes.Add(method + " " + p) + res.handlerFunc(method, p, h) } // handlerFunc registers the handler function for the given pattern.