-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivities.go
259 lines (216 loc) · 5.87 KB
/
activities.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
package main
import (
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"github.com/ejv2/prepper/data"
"github.com/ejv2/prepper/session"
)
// handleActivities is the handler for "/activity/".
//
// Shows a table of registered activities, along with a search feature reused
// from the booking menu.
func handleActivities(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
ddat, err := NewDashboardData(s)
if err != nil {
internalError(c, err)
return
}
act, err := data.GetPermanentActivities(Database)
if err != nil {
internalError(c, err)
return
}
delname, deleted := c.GetQuery("deleted")
_, saved := c.GetQuery("saved")
dat := struct {
DashboardData
Activities []data.Activity
Deleted bool
DeletedName string
Saved bool
}{ddat, act, deleted, delname, saved}
c.HTML(http.StatusOK, "activities.gohtml", dat)
}
// activityEditor shows the activity editor HTML page.
func activityEditor(c *gin.Context, activity data.Activity, s session.Session) {
ddat, err := NewDashboardData(s)
if err != nil {
internalError(c, err)
return
}
eq, err := data.GetEquipment(Database)
if err != nil {
internalError(c, err)
return
}
c.HTML(http.StatusOK, "activity-edit.gohtml", struct {
DashboardData
Activity data.Activity
Equipment []data.EquipmentItem
}{ddat, activity, eq})
}
// handleActivityNew is the handler for "/activity/new".
func handleActivityNew(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
newact, err := data.NewActivity(Database, s.UserID)
if err != nil {
internalError(c, err)
return
}
activityEditor(c, newact, s)
}
// handleActivityEdit is the handler for GET "/activity/[ID]/edit".
func handleActivityEdit(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
actsid := c.Param("activity")
actid, err := strconv.ParseUint(actsid, 10, 32)
if err != nil {
c.String(http.StatusBadRequest, "Bad Activity ID")
return
}
act, err := data.GetActivity(Database, uint(actid))
if err != nil {
c.String(http.StatusBadRequest, "Activity Not Found")
return
}
activityEditor(c, act, s)
}
// handleActivityDoEdit is the handler for POST "/activity/[ID]/edit".
//
// This is the form handler for the edit form.
func handleActivityDoEdit(c *gin.Context) {
actsid := c.Param("activity")
actid, err := strconv.ParseUint(actsid, 10, 32)
if err != nil {
c.String(http.StatusBadRequest, "Bad Activity ID")
return
}
act, err := data.GetActivity(Database, uint(actid))
if err != nil {
c.String(http.StatusNotFound, "Activity Not Found")
return
}
c.MultipartForm()
set, err := NewPostItemInformation(c.Request)
if err != nil {
c.String(http.StatusBadRequest, "Invalid item set")
return
}
sub := struct {
Title string `form:"title"`
Description string `form:"description"`
Category string `form:"category"`
}{act.Title, act.Description, act.Category}
err = c.Bind(&sub)
if err != nil {
c.String(http.StatusBadRequest, "Recieved bad data")
}
// Process equipment sets
// We later use this to determine what to delete
for i := range act.Equipment {
act.Equipment[i].Quantity = 0
}
set.Copy(&act)
act.Title = sub.Title
act.Description = sub.Description
act.Category = sub.Category
if err := Database.Updates(&act).Error; err != nil {
internalError(c, err)
return
}
for _, eq := range act.Equipment {
if eq.Quantity == 0 {
if err := Database.Model(&eq).Where(&eq).Delete(&eq).Error; err != nil {
internalError(c, err)
return
}
continue
}
if err := Database.Updates(&eq).Error; err != nil {
internalError(c, err)
return
}
}
c.Redirect(http.StatusFound, "/activity/?saved")
}
// handleActivityDelete is the handler for "/activity/[ID]/delete".
//
// This shows a confirmation screen given no query arguments, and performs the
// deletion if given them.
func handleActivityDelete(c *gin.Context) {
s := Sessions.Start(c)
defer s.Update()
ddat, err := NewDashboardData(s)
if err != nil {
internalError(c, err)
return
}
actsid := c.Param("activity")
actid, err := strconv.ParseUint(actsid, 10, 32)
if err != nil {
c.String(http.StatusBadRequest, "Bad Activity ID")
return
}
act, err := data.GetActivity(Database, uint(actid))
if err != nil {
c.String(http.StatusBadRequest, "Activity Not Found")
return
}
// Actually do deletion when confirmed.
_, conf := c.GetQuery("confirm")
if conf {
// If err isn't nil, GORM will rollback this transaction.
err := Database.Transaction(func(tx *gorm.DB) error {
acts := make([]data.Activity, 0, 10)
if err := tx.Where(data.Activity{CopiedFrom: act.ID}).Find(&acts).Error; err != nil {
internalError(c, err)
return err
}
// Delete child activities and bookings.
for _, a := range acts {
// Delete equipment sets
if err := tx.Model(data.EquipmentSet{}).Where(&data.EquipmentSet{ActivityID: a.ID}).Delete(&data.EquipmentSet{}).Error; err != nil {
return err
}
if err := tx.Model(data.Booking{}).Where(&data.Booking{ActivityID: a.ID}).Delete(&data.Booking{}).Error; err != nil {
return err
}
if err := tx.Delete(&a).Error; err != nil {
return err
}
}
if err := tx.Model(data.EquipmentSet{}).Where(&data.EquipmentSet{ActivityID: act.ID}).Delete(&data.EquipmentSet{}).Error; err != nil {
return err
}
// Delete parent activity.
if err := tx.Where(&act).Delete(&act).Error; err != nil {
return err
}
return nil
})
if err != nil {
internalError(c, err)
return
}
c.Redirect(http.StatusFound, fmt.Sprint("/activity/?deleted=", url.QueryEscape(act.Title)))
return
}
numbook := int64(0)
if err := Database.Model(&data.Activity{}).Where(&data.Activity{CopiedFrom: act.ID}).Count(&numbook).Error; err != nil {
internalError(c, err)
return
}
c.HTML(http.StatusOK, "activity-delete.gohtml", struct {
DashboardData
data.Activity
BookingCount int64
}{ddat, act, numbook})
}