-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortened_links.go
288 lines (233 loc) · 9.25 KB
/
shortened_links.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package actions
import (
"fmt"
"net/http"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/pop/v6"
"github.com/gobuffalo/x/responder"
"github.com/magikid/goshorter/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 (ShortenedLink)
// DB Table: Plural (shortened_links)
// Resource: Plural (ShortenedLinks)
// Path: Plural (/shortened_links)
// View Template Folder: Plural (/templates/shortened_links/)
// ShortenedLinksResource is the resource for the ShortenedLink model
type ShortenedLinksResource struct {
buffalo.Resource
}
// List gets all ShortenedLinks. This function is mapped to the path
// GET /shortened_links
func (v ShortenedLinksResource) List(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return fmt.Errorf("no transaction found")
}
shortenedLinks := &models.ShortenedLinks{}
// Paginate results. Params "page" and "per_page" control pagination.
// Default values are "page=1" and "per_page=20".
q := tx.PaginateFromParams(c.Params())
// Retrieve all ShortenedLinks from the DB
if err := q.All(shortenedLinks); err != nil {
return err
}
return responder.Wants("html", func(c buffalo.Context) error {
// Add the paginator to the context so it can be used in the template.
c.Set("pagination", q.Paginator)
c.Set("shortenedLinks", shortenedLinks)
return c.Render(http.StatusOK, r.HTML("shortened_links/index.plush.html"))
}).Wants("json", func(c buffalo.Context) error {
return c.Render(200, r.JSON(shortenedLinks))
}).Wants("xml", func(c buffalo.Context) error {
return c.Render(200, r.XML(shortenedLinks))
}).Respond(c)
}
// Show gets the data for one ShortenedLink. This function is mapped to
// the path GET /shortened_links/{shortened_link_id}
func (v ShortenedLinksResource) Show(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return fmt.Errorf("no transaction found")
}
// Allocate an empty ShortenedLink
shortenedLink := &models.ShortenedLink{}
// To find the ShortenedLink the parameter shortened_link_id is used.
if err := tx.Find(shortenedLink, c.Param("shortened_link_id")); err != nil {
return c.Error(http.StatusNotFound, err)
}
return responder.Wants("html", func(c buffalo.Context) error {
c.Set("shortenedLink", shortenedLink)
return c.Render(http.StatusOK, r.HTML("shortened_links/show.plush.html"))
}).Wants("json", func(c buffalo.Context) error {
return c.Render(200, r.JSON(shortenedLink))
}).Wants("xml", func(c buffalo.Context) error {
return c.Render(200, r.XML(shortenedLink))
}).Respond(c)
}
// New renders the form for creating a new ShortenedLink.
// This function is mapped to the path GET /shortened_links/new
func (v ShortenedLinksResource) New(c buffalo.Context) error {
c.Set("shortenedLink", &models.ShortenedLink{})
return c.Render(http.StatusOK, r.HTML("shortened_links/new.plush.html"))
}
// Create adds a ShortenedLink to the DB. This function is mapped to the
// path POST /shortened_links
func (v ShortenedLinksResource) Create(c buffalo.Context) error {
// Allocate an empty ShortenedLink
shortenedLink := &models.ShortenedLink{}
// Bind shortenedLink to the html form elements
if err := c.Bind(shortenedLink); err != nil {
return err
}
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return fmt.Errorf("no transaction found")
}
// Validate the data from the html form
verrs, err := tx.ValidateAndCreate(shortenedLink)
if err != nil {
return err
}
if verrs.HasAny() {
return responder.Wants("html", func(c buffalo.Context) error {
// 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.
c.Set("shortenedLink", shortenedLink)
return c.Render(http.StatusUnprocessableEntity, r.HTML("shortened_links/new.plush.html"))
}).Wants("json", func(c buffalo.Context) error {
return c.Render(http.StatusUnprocessableEntity, r.JSON(verrs))
}).Wants("xml", func(c buffalo.Context) error {
return c.Render(http.StatusUnprocessableEntity, r.XML(verrs))
}).Respond(c)
}
return responder.Wants("html", func(c buffalo.Context) error {
// If there are no errors set a success message
c.Flash().Add("success", T.Translate(c, "shortenedLink.created.success"))
// and redirect to the show page
return c.Redirect(http.StatusSeeOther, "/shortened_links/%v", shortenedLink.ID)
}).Wants("json", func(c buffalo.Context) error {
return c.Render(http.StatusCreated, r.JSON(shortenedLink))
}).Wants("xml", func(c buffalo.Context) error {
return c.Render(http.StatusCreated, r.XML(shortenedLink))
}).Respond(c)
}
// Edit renders a edit form for a ShortenedLink. This function is
// mapped to the path GET /shortened_links/{shortened_link_id}/edit
func (v ShortenedLinksResource) Edit(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return fmt.Errorf("no transaction found")
}
// Allocate an empty ShortenedLink
shortenedLink := &models.ShortenedLink{}
if err := tx.Find(shortenedLink, c.Param("shortened_link_id")); err != nil {
return c.Error(http.StatusNotFound, err)
}
c.Set("shortenedLink", shortenedLink)
return c.Render(http.StatusOK, r.HTML("shortened_links/edit.plush.html"))
}
// Update changes a ShortenedLink in the DB. This function is mapped to
// the path PUT /shortened_links/{shortened_link_id}
func (v ShortenedLinksResource) Update(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return fmt.Errorf("no transaction found")
}
// Allocate an empty ShortenedLink
shortenedLink := &models.ShortenedLink{}
if err := tx.Find(shortenedLink, c.Param("shortened_link_id")); err != nil {
return c.Error(http.StatusNotFound, err)
}
// Bind ShortenedLink to the html form elements
if err := c.Bind(shortenedLink); err != nil {
return err
}
verrs, err := tx.ValidateAndUpdate(shortenedLink)
if err != nil {
return err
}
if verrs.HasAny() {
return responder.Wants("html", func(c buffalo.Context) error {
// 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.
c.Set("shortenedLink", shortenedLink)
return c.Render(http.StatusUnprocessableEntity, r.HTML("shortened_links/edit.plush.html"))
}).Wants("json", func(c buffalo.Context) error {
return c.Render(http.StatusUnprocessableEntity, r.JSON(verrs))
}).Wants("xml", func(c buffalo.Context) error {
return c.Render(http.StatusUnprocessableEntity, r.XML(verrs))
}).Respond(c)
}
return responder.Wants("html", func(c buffalo.Context) error {
// If there are no errors set a success message
c.Flash().Add("success", T.Translate(c, "shortenedLink.updated.success"))
// and redirect to the show page
return c.Redirect(http.StatusSeeOther, "/shortened_links/%v", shortenedLink.ID)
}).Wants("json", func(c buffalo.Context) error {
return c.Render(http.StatusOK, r.JSON(shortenedLink))
}).Wants("xml", func(c buffalo.Context) error {
return c.Render(http.StatusOK, r.XML(shortenedLink))
}).Respond(c)
}
// Destroy deletes a ShortenedLink from the DB. This function is mapped
// to the path DELETE /shortened_links/{shortened_link_id}
func (v ShortenedLinksResource) Destroy(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return fmt.Errorf("no transaction found")
}
// Allocate an empty ShortenedLink
shortenedLink := &models.ShortenedLink{}
// To find the ShortenedLink the parameter shortened_link_id is used.
if err := tx.Find(shortenedLink, c.Param("shortened_link_id")); err != nil {
return c.Error(http.StatusNotFound, err)
}
if err := tx.Destroy(shortenedLink); err != nil {
return err
}
return responder.Wants("html", func(c buffalo.Context) error {
// If there are no errors set a flash message
c.Flash().Add("success", T.Translate(c, "shortenedLink.destroyed.success"))
// Redirect to the index page
return c.Redirect(http.StatusSeeOther, "/shortened_links")
}).Wants("json", func(c buffalo.Context) error {
return c.Render(http.StatusOK, r.JSON(shortenedLink))
}).Wants("xml", func(c buffalo.Context) error {
return c.Render(http.StatusOK, r.XML(shortenedLink))
}).Respond(c)
}
func RedirectHandler(c buffalo.Context) error {
// Get the DB connection from the context
_, ok := c.Value("tx").(*pop.Connection)
if !ok {
return fmt.Errorf("no transaction found")
}
// Allocate an empty ShortenedLink
shortenedLink := &models.ShortenedLink{}
err := models.DB.Where("short_code = ?", c.Param("shortCode")).First(shortenedLink)
if err != nil {
return c.Error(http.StatusNotFound, err)
}
shortenedLink.Hits++
err = models.DB.Update(shortenedLink)
if err != nil {
c.Logger().Error(err)
}
c.Logger().Infof("Redirecting to %v", shortenedLink.URL)
return c.Redirect(http.StatusTemporaryRedirect, shortenedLink.URL)
}