/
tools.go
220 lines (196 loc) · 6.19 KB
/
tools.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package actions
import (
"fmt"
"strings"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/pop"
"github.com/pkg/errors"
"github.com/alex-held/devctl-toolkit/models"
)
// This file is generated by Buffalo. It offers a basic structure for
// adding, editing and deleting a page. If your model is more
// complex or you need more than the basic implementation you need to
// edit this file.
// Following naming logic is implemented in Buffalo:
// Model: Singular (Tool)
// DB Table: Plural (tools)
// Resource: Plural (Tools)
// Path: Plural (/tools)
// View Template Folder: Plural (/templates/tools/)
// ToolsResource is the resource for the Tool model
type ToolsResource struct {
buffalo.Resource
}
// List gets all Tools. This function is mapped to the path
// GET /tools
func (v ToolsResource) List(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.WithStack(errors.New("no transaction found"))
}
tools := &models.Tools{}
// Paginate results. Params "page" and "per_page" control pagination.
// Default values are "page=1" and "per_page=20".
q := tx.PaginateFromParams(c.Params())
if topic := c.Param("topic"); topic != "" {
q = q.Where("? = ANY(tools.topics)", topic)
}
if query := c.Param("query"); query != "" {
pattern := fmt.Sprintf("%%%v%%", strings.ToUpper(query))
q = q.Where("upper(name) LIKE ? OR upper(name_with_owner) LIKE ? OR upper(description) LIKE ?", pattern, pattern, pattern)
}
q = q.Order("stars desc")
// Retrieve all Tools from the DB
if err := q.All(tools); err != nil {
return errors.WithStack(err)
}
// Add the paginator to the context so it can be used in the template.
c.Set("pagination", q.Paginator)
return c.Render(200, r.Auto(c, tools))
}
// Show gets the data for one Tool. This function is mapped to
// the path GET /tools/{tool_id}
func (v ToolsResource) Show(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.WithStack(errors.New("no transaction found"))
}
// Allocate an empty Tool
tool := &models.Tool{}
// To find the Tool the parameter tool_id is used.
if err := tx.Eager().Find(tool, c.Param("tool_id")); err != nil {
return c.Error(404, err)
}
return c.Render(200, r.Auto(c, tool))
}
// // New renders the form for creating a new Tool.
// // This function is mapped to the path GET /tools/new
// func (v ToolsResource) New(c buffalo.Context) error {
// return c.Render(200, r.Auto(c, &models.Tool{}))
// }
// // Create adds a Tool to the DB. This function is mapped to the
// // path POST /tools
// func (v ToolsResource) Create(c buffalo.Context) error {
// // Allocate an empty Tool
// tool := &models.Tool{}
//
// // Bind tool to the html form elements
// if err := c.Bind(tool); err != nil {
// return errors.WithStack(err)
// }
//
// // Get the DB connection from the context
// tx, ok := c.Value("tx").(*pop.Connection)
// if !ok {
// return errors.WithStack(errors.New("no transaction found"))
// }
//
// // Validate the data from the html form
// verrs, err := tx.ValidateAndCreate(tool)
// if err != nil {
// return errors.WithStack(err)
// }
//
// if verrs.HasAny() {
// // Make the errors available inside the html template
// c.Set("errors", verrs)
//
// // Render again the new.html template that the user can
// // correct the input.
// return c.Render(422, r.Auto(c, tool))
// }
//
// // If there are no errors set a success message
// c.Flash().Add("success", "Tool was created successfully")
//
// // and redirect to the tools index page
// return c.Render(201, r.Auto(c, tool))
// }
// // Edit renders a edit form for a Tool. This function is
// // mapped to the path GET /tools/{tool_id}/edit
// func (v ToolsResource) Edit(c buffalo.Context) error {
// // Get the DB connection from the context
// tx, ok := c.Value("tx").(*pop.Connection)
// if !ok {
// return errors.WithStack(errors.New("no transaction found"))
// }
//
// // Allocate an empty Tool
// tool := &models.Tool{}
//
// if err := tx.Find(tool, c.Param("tool_id")); err != nil {
// return c.Error(404, err)
// }
//
// return c.Render(200, r.Auto(c, tool))
// }
// // Update changes a Tool in the DB. This function is mapped to
// // the path PUT /tools/{tool_id}
// func (v ToolsResource) Update(c buffalo.Context) error {
// // Get the DB connection from the context
// tx, ok := c.Value("tx").(*pop.Connection)
// if !ok {
// return errors.WithStack(errors.New("no transaction found"))
// }
//
// // Allocate an empty Tool
// tool := &models.Tool{}
//
// if err := tx.Find(tool, c.Param("tool_id")); err != nil {
// return c.Error(404, err)
// }
//
// // Bind Tool to the html form elements
// if err := c.Bind(tool); err != nil {
// return errors.WithStack(err)
// }
//
// verrs, err := tx.ValidateAndUpdate(tool)
// if err != nil {
// return errors.WithStack(err)
// }
//
// if verrs.HasAny() {
// // Make the errors available inside the html template
// c.Set("errors", verrs)
//
// // Render again the edit.html template that the user can
// // correct the input.
// return c.Render(422, r.Auto(c, tool))
// }
//
// // If there are no errors set a success message
// c.Flash().Add("success", "Tool was updated successfully")
//
// // and redirect to the tools index page
// return c.Render(200, r.Auto(c, tool))
// }
// // Destroy deletes a Tool from the DB. This function is mapped
// // to the path DELETE /tools/{tool_id}
// func (v ToolsResource) Destroy(c buffalo.Context) error {
// // Get the DB connection from the context
// tx, ok := c.Value("tx").(*pop.Connection)
// if !ok {
// return errors.WithStack(errors.New("no transaction found"))
// }
//
// // Allocate an empty Tool
// tool := &models.Tool{}
//
// // To find the Tool the parameter tool_id is used.
// if err := tx.Find(tool, c.Param("tool_id")); err != nil {
// return c.Error(404, err)
// }
//
// if err := tx.Destroy(tool); err != nil {
// return errors.WithStack(err)
// }
//
// // If there are no errors set a flash message
// c.Flash().Add("success", "Tool was destroyed successfully")
//
// // Redirect to the tools index page
// return c.Render(200, r.Auto(c, tool))
// }