This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 577
/
resource.go
82 lines (70 loc) · 2.25 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
package buffalo
import (
"fmt"
"net/http"
)
// 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 cr 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
Create(Context) error
Update(Context) error
Destroy(Context) error
}
// Middler can be implemented to specify additional
// middleware specific to the resource
type Middler interface {
Use() []MiddlewareFunc
}
// 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(http.StatusOK, 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(http.StatusNotFound, fmt.Errorf("resource not implemented"))
}
// Show default implementation. Returns a 404
func (v BaseResource) Show(c Context) error {
return c.Error(http.StatusNotFound, fmt.Errorf("resource not implemented"))
}
// Create default implementation. Returns a 404
func (v BaseResource) Create(c Context) error {
return c.Error(http.StatusNotFound, fmt.Errorf("resource not implemented"))
}
// Update default implementation. Returns a 404
func (v BaseResource) Update(c Context) error {
return c.Error(http.StatusNotFound, fmt.Errorf("resource not implemented"))
}
// Destroy default implementation. Returns a 404
func (v BaseResource) Destroy(c Context) error {
return c.Error(http.StatusNotFound, fmt.Errorf("resource not implemented"))
}