forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
85 lines (72 loc) · 2.36 KB
/
resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package buffalo
import "errors"
// Resource interface allows for the easy mapping
// of common RESTful actions to a set of paths. See
// the a.Resource documentation for more details.
// NOTE: When skipping Resource handlers, you need to first declare your
// resource handler as a type of buffalo.Resource for the Skip function to
// properly recognize and match it.
/*
// Works:
var ur Resource
cr = &carsResource{&buffaloBaseResource{}}
g = a.Resource("/cars", cr)
g.Use(SomeMiddleware)
g.Middleware.Skip(SomeMiddleware, cr.Show)
// Doesn't Work:
cr := &carsResource{&buffaloBaseResource{}}
g = a.Resource("/cars", cr)
g.Use(SomeMiddleware)
g.Middleware.Skip(SomeMiddleware, cr.Show)
*/
type Resource interface {
List(Context) error
Show(Context) error
New(Context) error
Create(Context) error
Edit(Context) error
Update(Context) error
Destroy(Context) error
}
// BaseResource fills in the gaps for any Resource interface
// functions you don't want/need to implement.
/*
type UsersResource struct {
Resource
}
func (ur *UsersResource) List(c Context) error {
return c.Render(200, render.String("hello")
}
// This will fulfill the Resource interface, despite only having
// one of the functions defined.
&UsersResource{&BaseResource{})
*/
type BaseResource struct{}
// List default implementation. Returns a 404
func (v *BaseResource) List(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
}
// Show default implementation. Returns a 404
func (v *BaseResource) Show(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
}
// New default implementation. Returns a 404
func (v *BaseResource) New(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
}
// Create default implementation. Returns a 404
func (v *BaseResource) Create(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
}
// Edit default implementation. Returns a 404
func (v *BaseResource) Edit(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
}
// Update default implementation. Returns a 404
func (v *BaseResource) Update(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
}
// Destroy default implementation. Returns a 404
func (v *BaseResource) Destroy(c Context) error {
return c.Error(404, errors.New("resource not implemented"))
}