Repository navigation
Expand file tree
/
Copy pathcrud.go
More file actions
125 lines (107 loc) · 3.41 KB
/
Copy pathcrud.go
File metadata and controls
125 lines (107 loc) · 3.41 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package apihandler
import "net/http"
// Read returns a Handler that resolves the addressed domain object and
// responds 200 OK with its lazily transformed representation.
func Read[T, R any](resolver Resolver[T], transformer Transformer[T, R]) Handler {
return func(r *http.Request) (Result, error) {
resource, err := resolver.Resolve(r)
if err != nil {
return Result{}, err
}
return Result{
Status: http.StatusOK,
Body: Resource[T, R]{Request: r, Value: resource, Transformer: transformer},
}, nil
}
}
// CreateOption configures a Handler built by Create.
type CreateOption[T any] func(config *createConfig[T])
// WithLocation sets the Location header on 201 Created responses, deriving
// the value from the request and the created domain object. It has no
// effect on the 204 No Content path.
func WithLocation[T any](location func(r *http.Request, resource T) string) CreateOption[T] {
return func(config *createConfig[T]) {
config.location = location
}
}
// createConfig collects the options applied to a Create handler.
type createConfig[T any] struct {
location func(r *http.Request, resource T) string
}
// Create returns a Handler that makes a new domain object from the request
// and responds 201 Created with its lazily transformed representation, or
// 204 No Content when the creator reports ok == false.
func Create[T, R any](
creator Creator[T],
transformer Transformer[T, R],
options ...CreateOption[T],
) Handler {
var config createConfig[T]
for _, option := range options {
option(&config)
}
return func(r *http.Request) (Result, error) {
resource, ok, err := creator.Create(r)
if err != nil {
return Result{}, err
}
if !ok {
return Result{Status: http.StatusNoContent}, nil
}
res := Result{
Status: http.StatusCreated,
Body: Resource[T, R]{Request: r, Value: resource, Transformer: transformer},
}
if config.location != nil {
res.SetHeader("Location", config.location(r, resource))
}
return res, nil
}
}
// Update returns a Handler that resolves the addressed domain object,
// applies the request to it, and responds 200 OK with the updated object's
// lazily transformed representation, or 204 No Content when the updater
// reports ok == false.
func Update[T, R any](
resolver Resolver[T],
updater Updater[T],
transformer Transformer[T, R],
) Handler {
return modify(resolver, transformer, updater.Update)
}
// Delete returns a Handler that resolves the addressed domain object,
// removes it, and responds 200 OK with the deleted object's lazily
// transformed representation, or 204 No Content when the deleter reports
// ok == false.
func Delete[T, R any](
resolver Resolver[T],
deleter Deleter[T],
transformer Transformer[T, R],
) Handler {
return modify(resolver, transformer, deleter.Delete)
}
// modify implements the shared resolve-then-mutate flow behind Update and
// Delete.
func modify[T, R any](
resolver Resolver[T],
transformer Transformer[T, R],
action func(r *http.Request, resource T) (T, bool, error),
) Handler {
return func(r *http.Request) (Result, error) {
resource, err := resolver.Resolve(r)
if err != nil {
return Result{}, err
}
modified, ok, err := action(r, resource)
if err != nil {
return Result{}, err
}
if !ok {
return Result{Status: http.StatusNoContent}, nil
}
return Result{
Status: http.StatusOK,
Body: Resource[T, R]{Request: r, Value: modified, Transformer: transformer},
}, nil
}
}