generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusecase.go
150 lines (125 loc) · 4.02 KB
/
usecase.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"context"
"log"
"net/http"
"strconv"
"github.com/swaggest/jsonform-go"
"github.com/swaggest/usecase"
"github.com/swaggest/usecase/status"
)
func createUserForm(r *jsonform.Repository) usecase.Interactor {
type another struct {
Foo string `json:"foo" required:"true" title:"Foo" minLength:"3"`
Bar string `json:"bar" required:"true" title:"Bar" maxLength:"3"`
}
u := usecase.NewInteractor(func(ctx context.Context, input struct{}, output *usecase.OutputWithEmbeddedWriter) error {
return r.Render(output.Writer,
jsonform.Page{
Title: "Create User and some more",
AppendHTMLHead: `
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.37.1/ace.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.37.1/ext-inline_autocomplete.min.js" integrity="sha512-99FE+tBv3oH1/pBRMytEllCvV2Web1lvyeunqcKnJHjiGMFLiwQ6WK6rknV/HYq/esz9i6JuprfgV4senYwtlA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.37.1/ext-language_tools.min.js" integrity="sha512-WPFoecnG6+TbAah6uWLOr+/36IDeXpa9klWh+SFWRzQeVC6x/n7rzODbzctFYL7rqxaDK2pbKj3psH7sws70Ng==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
window.jsonform_ace_setup = function(setup){
console.log("jsonform_ace_setup", setup)
setup()
}
</script>
`,
PrependHTML: `<div><img src="http://placekitten.com/200/300" /></div>`,
AppendHTML: `<div><img src="http://placekitten.com/300/200" /></div>`,
},
jsonform.Form{
Title: "Create User",
SubmitMethod: http.MethodPost,
SubmitURL: "/users",
Value: User{},
SuccessStatus: http.StatusCreated,
},
jsonform.Form{
Title: "Another random form",
SubmitMethod: http.MethodPut,
SubmitURL: "/nowhere",
Value: another{},
},
jsonform.Form{
Title: "More random forms",
SubmitMethod: http.MethodPut,
SubmitURL: "/nowhere",
Value: another{},
},
)
})
return u
}
func editUserForm(r *jsonform.Repository, ur *userRepo) usecase.Interactor {
type in struct {
ID int `path:"id"`
}
u := usecase.NewInteractor(func(ctx context.Context, input in, output *usecase.OutputWithEmbeddedWriter) error {
user, err := ur.get(input.ID)
if err != nil {
return err
}
err = r.Render(output.Writer, jsonform.Page{}, jsonform.Form{
Title: "Update User",
SubmitMethod: http.MethodPut,
SubmitURL: "/user/" + strconv.Itoa(input.ID) + ".json",
Value: user,
SuccessStatus: http.StatusNoContent,
OnSuccess: `function(x){console.log(x);alert("response status: " + x.status)}`,
})
if err != nil {
log.Println(err.Error())
}
return err
})
return u
}
func createUser(ur *userRepo) usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, input User, output *struct{}) error {
ur.create(input)
return nil
})
// Describe use case interactor.
u.SetTitle("Create User")
u.SetExpectedErrors(status.InvalidArgument)
return u
}
func listUsers(ur *userRepo) usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, input struct{}, output *[]User) (err error) {
*output = ur.list()
return err
})
// Describe use case interactor.
u.SetTitle("List Users")
return u
}
func getUser(ur *userRepo) usecase.Interactor {
type getUserInput struct {
ID int `path:"id"`
}
u := usecase.NewInteractor(func(ctx context.Context, input getUserInput, output *User) (err error) {
*output, err = ur.get(input.ID)
return err
})
// Describe use case interactor.
u.SetExpectedErrors(status.InvalidArgument)
return u
}
func updateUser(ur *userRepo) usecase.Interactor {
type updateUserInput struct {
ID int `path:"id"`
User
}
u := usecase.NewInteractor(func(ctx context.Context, input updateUserInput, output *struct{}) error {
ur.update(input.ID, input.User)
return nil
})
// Describe use case interactor.
u.SetTitle("Create User")
u.SetExpectedErrors(status.InvalidArgument)
return u
}