-
Notifications
You must be signed in to change notification settings - Fork 4
/
render.go
133 lines (113 loc) · 3.68 KB
/
render.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
package actions
import (
"fmt"
"os"
"time"
"github.com/devrelcollective/xela/models"
"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/packr"
"github.com/gobuffalo/plush"
"github.com/gobuffalo/pop"
"github.com/gofrs/uuid"
"github.com/pkg/errors"
)
var r *render.Engine
var assetsBox = packr.NewBox("../public")
func init() {
r = render.New(render.Options{
// HTML layout to be used for all HTML requests:
HTMLLayout: "application.html",
// Box containing all of the templates:
TemplatesBox: packr.NewBox("../templates"),
AssetsBox: assetsBox,
// Add template helpers here:
Helpers: render.Helpers{
// uncomment for non-Bootstrap form helpers:
// "form": plush.FormHelper,
// "form_for": plush.FormForHelper,
"checkNull": func() {
},
"getUserEmail": func(userID uuid.UUID, c plush.HelperContext) (string, 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 User
user := &models.User{}
// To find the User the parameter user_id is used.
err := tx.Find(user, userID)
if err != nil {
return "", errors.WithStack(errors.New("query error"))
}
return user.Email.String, nil
},
"getSpeakerName": func(dutonianID uuid.UUID, c plush.HelperContext) (string, 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"))
}
dutonian := &models.Dutonian{}
err := tx.Find(dutonian, dutonianID)
if err != nil {
return "", errors.WithStack(errors.New("query error"))
} else {
return fmt.Sprintf("%s %s", dutonian.Firstname, dutonian.Lastname), nil
}
},
"buildS3Url": func() string {
return fmt.Sprintf("https://s3.%s.amazonaws.com/%s/", os.Getenv("S3_REGION"), os.Getenv("S3_BUCKET"))
},
"getAbstractName": func(abstractID uuid.UUID, c plush.HelperContext) (string, 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"))
}
abstract := &models.Abstract{}
err := tx.Find(abstract, abstractID)
if err != nil {
return "", errors.WithStack(errors.New("query error"))
} else {
return abstract.Title, nil
}
},
"getEventName": func(eventID uuid.UUID, c plush.HelperContext) (string, 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"))
}
event := &models.Event{}
err := tx.Find(event, eventID)
if err != nil {
return "", errors.WithStack(errors.New("query error"))
} else {
return event.Title, nil
}
},
"getEventLocation": func(eventID uuid.UUID, c plush.HelperContext) (string, 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"))
}
event := &models.Event{}
err := tx.Find(event, eventID)
if err != nil {
return "", errors.WithStack(errors.New("query error"))
} else {
return event.Location.String, nil
}
},
"displayEventDate": func(eventStartDate time.Time, eventEndDate time.Time) string {
if eventStartDate == eventEndDate {
return eventStartDate.Format("January 02, 2006")
} else {
return fmt.Sprintf("%s - %s", eventStartDate.Format("January 02, 2006"), eventEndDate.Format("January 02, 2006"))
}
},
},
})
}