From ccb1c50f29a366369fa9257ceca0faa4c6ea5cc3 Mon Sep 17 00:00:00 2001 From: Reindert Vetter Date: Mon, 8 Jun 2020 23:23:17 +0200 Subject: [PATCH] lanvard/lanvard#49 bind models + fallback --- app/http/kernel.go | 1 + app/http/middleware/api.go | 11 ++++ app/http/middleware/route_model_binding.go | 15 +++++ app/http/middleware/validate_post_size.go | 2 +- app/http/middleware/web.go | 11 ++++ app/model/user.go | 8 ++- app/providers/route_service_provider.go | 1 + routes/api.go | 31 +++++----- routes/web.go | 11 ++-- src/adapters/user.go | 58 ++++++++++++------- .../user_controller.go | 7 ++- 11 files changed, 109 insertions(+), 47 deletions(-) create mode 100644 app/http/middleware/api.go create mode 100644 app/http/middleware/route_model_binding.go create mode 100644 app/http/middleware/web.go rename src/{controllers => controller}/user_controller.go (76%) diff --git a/app/http/kernel.go b/app/http/kernel.go index aca565f..dbbad40 100755 --- a/app/http/kernel.go +++ b/app/http/kernel.go @@ -17,5 +17,6 @@ func pipes() []inter.HttpMiddleware { return []inter.HttpMiddleware{ // todo remove or use ValidatePostSize middleware.ValidatePostSize{}, + middleware.RouteModelBinding{}, } } diff --git a/app/http/middleware/api.go b/app/http/middleware/api.go new file mode 100644 index 0000000..9b403d4 --- /dev/null +++ b/app/http/middleware/api.go @@ -0,0 +1,11 @@ +package middleware + +import ( + "github.com/lanvard/contract/inter" +) + +type Api struct{} + +func (a Api) Handle(request inter.Request, next inter.Next) inter.Response { + return next(request) +} diff --git a/app/http/middleware/route_model_binding.go b/app/http/middleware/route_model_binding.go new file mode 100644 index 0000000..d76d7c9 --- /dev/null +++ b/app/http/middleware/route_model_binding.go @@ -0,0 +1,15 @@ +package middleware + +import ( + "github.com/lanvard/contract/inter" +) + +type RouteModelBinding struct{} + +func (b RouteModelBinding) Handle(request inter.Request, next inter.Next) inter.Response { + // request.App().Instance("user", func() model.User { + // return model.User.Find(request.UrlValue("user")) + // }) + + return next(request) +} diff --git a/app/http/middleware/validate_post_size.go b/app/http/middleware/validate_post_size.go index 5d3f5ae..b9950bb 100644 --- a/app/http/middleware/validate_post_size.go +++ b/app/http/middleware/validate_post_size.go @@ -4,7 +4,7 @@ import "github.com/lanvard/contract/inter" type ValidatePostSize struct{} -func (v ValidatePostSize) Handle(request inter.Request, next inter.MiddlewareDestination) inter.Response { +func (v ValidatePostSize) Handle(request inter.Request, next inter.Next) inter.Response { // todo validate return next(request) } diff --git a/app/http/middleware/web.go b/app/http/middleware/web.go new file mode 100644 index 0000000..809d04c --- /dev/null +++ b/app/http/middleware/web.go @@ -0,0 +1,11 @@ +package middleware + +import ( + "github.com/lanvard/contract/inter" +) + +type Web struct{} + +func (a Web) Handle(request inter.Request, next inter.Next) inter.Response { + return next(request) +} diff --git a/app/model/user.go b/app/model/user.go index bd3ef0c..64228cc 100644 --- a/app/model/user.go +++ b/app/model/user.go @@ -5,8 +5,12 @@ type User struct { email string } -func NewUser(id int, fullName string) User { - return User{id: id, email: fullName} +func (u User) Find() User { + return User{id: 1, email: "fake@test.nl"} +} + +func NewUser(id int, email string) User { + return User{id: id, email: email} } func (u User) ToMap() map[string]interface{} { diff --git a/app/providers/route_service_provider.go b/app/providers/route_service_provider.go index 02e6c80..df09ef5 100644 --- a/app/providers/route_service_provider.go +++ b/app/providers/route_service_provider.go @@ -16,6 +16,7 @@ func (p RouteServiceProvider) Boot(app inter.App) inter.App { collection.Merge(routes.Api) collection.Merge(routes.Web) + // Here you can make your adjustments that apply to all routes: // collection.WhereMulti(map[string]string{ // "id": "[0-9]+", // }) diff --git a/routes/api.go b/routes/api.go index 8be22c4..398e868 100644 --- a/routes/api.go +++ b/routes/api.go @@ -2,26 +2,25 @@ package routes import ( . "github.com/lanvard/routing" - "lanvard/src/controllers" + "lanvard/app/http/middleware" + "lanvard/src/controller" ) /* -|-------------------------------------------------------------------------- -| API MapMethodRoutes -|-------------------------------------------------------------------------- +|--------------------------------------------------------------------------- +| API routes +|--------------------------------------------------------------------------- | -| Here is where you can register API routes for your application. By -| default this is loaded in a group and is assigned to the "api" middleware -| group. By default, API group is placed with "/api" prefix. Feel free to -| remove the prefix if you are using a subdomain for your api. Enjoy -| building your API! +| Here is where you can register API routes for your application. By default +| this is loaded in a group. The group is assigned to the "Api" middleware +| and is placed with "/api" prefix. Feel free to remove the prefix if you +| are using a subdomain for your API. Enjoy building your API! | */ -var Api = Group( - Get("/users", controllers.User.Index), - Get("/users/{users?}", controllers.User.Index), - Get("/user/{user}", controllers.User.Show), - Post("/users", controllers.User.Store), - Delete("/users/{users}", controllers.User.Destroy), -).Prefix("/api") +var Api = Group( + Get("/users", controller.User.Index), + Get("/user/{user}", controller.User.Show), + Post("/users", controller.User.Store), + Delete("/users/{users}", controller.User.Destroy), +).Prefix("/api").Middleware(middleware.Api{}) diff --git a/routes/web.go b/routes/web.go index b2525f2..fef2bd8 100644 --- a/routes/web.go +++ b/routes/web.go @@ -2,18 +2,19 @@ package routes import ( . "github.com/lanvard/routing" + "lanvard/app/http/middleware" ) /* |-------------------------------------------------------------------------- -| Web MapMethodRoutes +| Web routes |-------------------------------------------------------------------------- | -| Here is where you can register Web routes for your website. These -| routes are loaded by the RouteServiceProvider within a group which -| is assigned the "web" middleware group. Enjoy building your website! +| Here is where you can register Web routes for your website. By default +| these routes are loaded within a group which is assigned the "Web" +| middleware. Enjoy building your website! | */ var Web = Group( // -) +).Middleware(middleware.Web{}) diff --git a/src/adapters/user.go b/src/adapters/user.go index 42db213..a4a5d0b 100644 --- a/src/adapters/user.go +++ b/src/adapters/user.go @@ -6,24 +6,42 @@ import ( "lanvard/src/contract" ) -var User = struct { - AllByRequest func(inter.Request) ([]contract.User, error) - OneByRequest func(inter.Request) (contract.User, error) -}{ - AllByRequest: func(request inter.Request) ([]contract.User, error) { - var err error - var users []contract.User - - users = append(users, model.NewUser(5435, "test@lanvard.com")) - - return users, err - }, - - OneByRequest: func(request inter.Request) (contract.User, error) { - var err error - userId := request.UrlValue("user").Number() - - user := model.NewUser(userId, "test@lanvard.com") - return user, err - }, +type User struct { + Request inter.Request +} + +func (adapter User) All() []contract.User { + users, err := adapter.AllE() + if err != nil { + panic(err) + } + + return users +} + +func (adapter User) AllE() ([]contract.User, error) { + var err error + var users []contract.User + + users = append(users, model.NewUser(5435, "test@lanvard.com")) + + return users, err +} + +func (adapter User) Find() contract.User { + user, err := adapter.FindE() + if err != nil { + panic(err) + } + + return user +} + +func (adapter User) FindE() (contract.User, error) { + userId, err := adapter.Request.UrlValue("user").NumberE() + if err != nil { + return _, err + } + + return model.NewUser(userId, "test@lanvard.com") } diff --git a/src/controllers/user_controller.go b/src/controller/user_controller.go similarity index 76% rename from src/controllers/user_controller.go rename to src/controller/user_controller.go index 5957a00..8427976 100644 --- a/src/controllers/user_controller.go +++ b/src/controller/user_controller.go @@ -1,4 +1,4 @@ -package controllers +package controller import ( "github.com/lanvard/contract/inter" @@ -10,7 +10,7 @@ var User = struct { Index, Create, Store, Show, Edit, Update, Destroy inter.Controller }{ Index: func(request inter.Request) inter.Response { - users, err := adapters.User.AllByRequest(request) + users, err := adapters.User{request}.AllE() if err != nil { return outcome.Error(err) } @@ -19,7 +19,8 @@ var User = struct { }, Show: func(request inter.Request) inter.Response { - user, err := adapters.User.OneByRequest(request) + user := adapters.User{request}.Find() + user, err := adapters.User{request}.FindE() if err != nil { return outcome.Error(err) }