From f555af9345fb6bb567945692ba1829b8888e0784 Mon Sep 17 00:00:00 2001 From: yoyofx Date: Wed, 24 Jan 2024 10:56:49 +0800 Subject: [PATCH] fixed bug for attribute route, that problem is access route path not match the mvc template path splits of length. --- .../simpleweb/contollers/usercontroller.go | 7 ++- web/mvc/api_result.go | 52 +++++++++++++++++++ web/mvc/route_template.go | 2 +- web/mvc/router_handler.go | 4 +- 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/examples/simpleweb/contollers/usercontroller.go b/examples/simpleweb/contollers/usercontroller.go index f545037b..e5beea2a 100644 --- a/examples/simpleweb/contollers/usercontroller.go +++ b/examples/simpleweb/contollers/usercontroller.go @@ -26,7 +26,7 @@ func NewUserController(userAction models.IUserAction, sd servicediscovery.IServi } type RegisterRequest struct { - mvc.RequestBody `route:"/v1/users/register"` + mvc.RequestBody `route:"/api/users/register"` UserName string `uri:"userName"` Password string `uri:"password"` @@ -66,6 +66,11 @@ func (controller UserController) GetHtmlBody() actionresult.IActionResult { }) } +func (controller UserController) GetDoc() mvc.ApiDocResult[string] { + + return mvc.ApiDocumentResult[string]().Success().Data("ok").Message("hello").Build() +} + func (controller UserController) GetInfo() mvc.ApiResult { return controller.OK(controller.userAction.Login("zhang")) diff --git a/web/mvc/api_result.go b/web/mvc/api_result.go index 603e8002..e6b93f44 100644 --- a/web/mvc/api_result.go +++ b/web/mvc/api_result.go @@ -124,3 +124,55 @@ func FailWithMsgFunc(data interface{}, fc func() string) ApiResult { Message: fc(), } } + +type ApiDocResult[T any] struct { + Success bool + Message string + Data T + Status int +} + +type ApiDocResultBuilder[T any] struct { + result *ApiDocResult[T] +} + +func (arb *ApiDocResultBuilder[T]) Success() *ApiDocResultBuilder[T] { + arb.result.Status = 200 + arb.result.Success = true + return arb +} + +func (arb *ApiDocResultBuilder[T]) Fail() *ApiDocResultBuilder[T] { + arb.result.Success = false + return arb +} + +func (arb *ApiDocResultBuilder[T]) Message(msg string) *ApiDocResultBuilder[T] { + arb.result.Message = msg + return arb +} + +func (arb *ApiDocResultBuilder[T]) MessageWithFunc(fc func() string) *ApiDocResultBuilder[T] { + arb.result.Message = fc() + return arb +} + +func (arb *ApiDocResultBuilder[T]) Data(data T) *ApiDocResultBuilder[T] { + arb.result.Data = data + return arb +} + +func (arb *ApiDocResultBuilder[T]) StatusCode(statusCode int) *ApiDocResultBuilder[T] { + arb.result.Status = statusCode + return arb +} + +func ApiDocumentResult[T any]() *ApiDocResultBuilder[T] { + return &ApiDocResultBuilder[T]{ + result: &ApiDocResult[T]{Status: 200}, + } +} + +func (arb *ApiDocResultBuilder[T]) Build() ApiDocResult[T] { + return *arb.result +} diff --git a/web/mvc/route_template.go b/web/mvc/route_template.go index e7a71286..5fe40fa1 100644 --- a/web/mvc/route_template.go +++ b/web/mvc/route_template.go @@ -28,7 +28,7 @@ func NewRouteTemplate(temp string) *RouteTemplate { } func (template *RouteTemplate) Match(pathComponents []string, matchinfo *MatchMvcInfo) bool { - if len(pathComponents) >= template.pathLen { + if len(pathComponents) == template.pathLen { matchinfo.ControllerName = pathComponents[template.GetControllerIndex()] matchinfo.ControllerName = strings.ToLower(matchinfo.ControllerName) if !strings.Contains(matchinfo.ControllerName, "controller") { diff --git a/web/mvc/router_handler.go b/web/mvc/router_handler.go index edbd510a..6101ae95 100644 --- a/web/mvc/router_handler.go +++ b/web/mvc/router_handler.go @@ -26,9 +26,9 @@ func NewMvcRouterHandler() *RouterHandler { func (handler *RouterHandler) Invoke(ctx *context.HttpContext, pathComponents []string) func(ctx *context.HttpContext) { matchInfo := MatchMvcInfo{} - foundRoute := handler.ActionRoutesAttributes.Match(ctx, pathComponents, &matchInfo) + foundRoute := handler.Options.Template.Match(pathComponents, &matchInfo) if !foundRoute { - foundRoute = handler.Options.Template.Match(pathComponents, &matchInfo) + foundRoute = handler.ActionRoutesAttributes.Match(ctx, pathComponents, &matchInfo) } if !foundRoute { return nil