Skip to content

Commit

Permalink
#49 bind models + fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Reindert Vetter committed Jun 8, 2020
1 parent db2ed19 commit ccb1c50
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 47 deletions.
1 change: 1 addition & 0 deletions app/http/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ func pipes() []inter.HttpMiddleware {
return []inter.HttpMiddleware{
// todo remove or use ValidatePostSize
middleware.ValidatePostSize{},
middleware.RouteModelBinding{},
}
}
11 changes: 11 additions & 0 deletions app/http/middleware/api.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 15 additions & 0 deletions app/http/middleware/route_model_binding.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion app/http/middleware/validate_post_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 11 additions & 0 deletions app/http/middleware/web.go
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 6 additions & 2 deletions app/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]"}
}

func NewUser(id int, email string) User {
return User{id: id, email: email}
}

func (u User) ToMap() map[string]interface{} {
Expand Down
1 change: 1 addition & 0 deletions app/providers/route_service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]+",
// })
Expand Down
31 changes: 15 additions & 16 deletions routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
11 changes: 6 additions & 5 deletions routes/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
58 changes: 38 additions & 20 deletions src/adapters/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "[email protected]"))

return users, err
},

OneByRequest: func(request inter.Request) (contract.User, error) {
var err error
userId := request.UrlValue("user").Number()

user := model.NewUser(userId, "[email protected]")
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, "[email protected]"))

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, "[email protected]")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controllers
package controller

import (
"github.com/lanvard/contract/inter"
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit ccb1c50

Please sign in to comment.