-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocols.go
203 lines (158 loc) · 5.1 KB
/
protocols.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
package handlers
import (
"fmt"
"strings"
"time"
"net/http"
"github.com/freitagsrunde/protokollamt/models"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/jinzhu/gorm"
"github.com/satori/go.uuid"
)
// DBConner defines the functions needed to
// retrieve and update values in the database.
type DBConner interface {
GetDBConn() *gorm.DB
}
// NewProtocolPayload represents the form data
// needed to upload a new internal meeting protocol.
type NewProtocolPayload struct {
Date string `form:"protocol-date"`
Content string `form:"protocol-content"`
}
// Protocols lists the existing meeting protocols
// and offers a button to upload a new protocol.
func Protocols(dbConner DBConner) gin.HandlerFunc {
return func(c *gin.Context) {
var Protocols []models.Protocol
dbConner.GetDBConn().Order("\"meeting_date\" desc").Find(&Protocols)
// Create nicely formatted strings for print
// in template output.
for i := range Protocols {
Protocols[i].MeetingDateString = Protocols[i].MeetingDate.Format("02.01.2006")
}
c.HTML(http.StatusOK, "protocols-list.html", gin.H{
"PageTitle": "Protokollamt der Freitagsrunde - Übersicht aller Protokolle",
"MainTitle": "Übersicht aller Protokolle",
"Protocols": Protocols,
})
}
}
// ProtocolsNew delivers the page including the
// upload form for a new meeting protocol.
func ProtocolsNew() gin.HandlerFunc {
return func(c *gin.Context) {
c.HTML(http.StatusOK, "protocols-new.html", gin.H{
"PageTitle": "Protokollamt der Freitagsrunde - Neues Protokoll hochladen",
"MainTitle": "Neues Protokoll hochladen",
})
}
}
// ProtocolsNewUpload processes submitted data
// for uploading a new meeting protocol.
func ProtocolsNewUpload(dbConner DBConner) gin.HandlerFunc {
return func(c *gin.Context) {
var Payload NewProtocolPayload
var Protocol models.Protocol
// Parse protocol form data into above defined payload.
err := c.BindWith(&Payload, binding.FormPost)
if err != nil {
c.HTML(http.StatusBadRequest, "protocols-new.html", gin.H{
"PageTitle": "Protokollamt der Freitagsrunde - Neues Protokoll hochladen",
"MainTitle": "Neues Protokoll hochladen",
"FatalError": "Verarbeitungsfehler. Bitte erneut versuchen.",
})
c.Abort()
return
}
// Remove possibly surrounding whitespace.
Payload.Date = strings.TrimSpace(Payload.Date)
Payload.Content = strings.TrimSpace(Payload.Content)
// Construct list of errors, if present.
errs := make(map[string]string)
if Payload.Date == "" {
errs["Datum"] = "Bitte folgendes Feld ausfüllen"
}
if Payload.Content == "" {
errs["Protokoll"] = "Bitte folgendes Feld ausfüllen"
}
if len(errs) > 0 {
c.HTML(http.StatusBadRequest, "protocols-new.html", gin.H{
"PageTitle": "Protokollamt der Freitagsrunde - Neues Protokoll hochladen",
"MainTitle": "Neues Protokoll hochladen",
"Errors": errs,
})
c.Abort()
return
}
// Attempt to parse time from form.
meetingDate, err := time.Parse("02.01.2006", Payload.Date)
if err != nil {
c.HTML(http.StatusBadRequest, "protocols-new.html", gin.H{
"PageTitle": "Protokollamt der Freitagsrunde - Neues Protokoll hochladen",
"MainTitle": "Neues Protokoll hochladen",
"FatalError": "Verarbeitungsfehler. Bitte erneut versuchen.",
})
c.Abort()
return
}
// Fill new protocol element.
Protocol.ID = uuid.NewV4().String()
Protocol.UploadDate = time.Now()
Protocol.MeetingDate = meetingDate
Protocol.Category = models.CategoryFreitagssitzung
Protocol.InternalVersion = Payload.Content
Protocol.PublicVersion = Payload.Content
Protocol.Status = models.StatusInReview
// Save protocol to database.
dbConner.GetDBConn().Create(&Protocol)
c.Redirect(http.StatusFound, "/protocols")
}
}
// ProtocolsSingle provides detail and review
// capabilities for a specific protocol.
func ProtocolsSingle(dbConner DBConner) gin.HandlerFunc {
return func(c *gin.Context) {
// Obtain ID of requested protocol.
protocolID := c.Param("id")
var Protocol models.Protocol
dbConner.GetDBConn().Where("\"id\" = ?", protocolID).First(&Protocol)
// If no result could be found for provided
// protocol ID, redirect back to listing page.
if Protocol.ID == "" {
c.Redirect(http.StatusFound, "/protocols")
c.Abort()
return
}
// Create nicely formatted strings for print
// in template output.
Protocol.MeetingDateString = Protocol.MeetingDate.Format("02.01.2006")
c.HTML(http.StatusOK, "protocols-view.html", gin.H{
"PageTitle": fmt.Sprintf("Protokollamt der Freitagsrunde - Protokoll vom %s", Protocol.MeetingDateString),
"MainTitle": fmt.Sprintf("Protokoll vom %s", Protocol.MeetingDateString),
"Protocol": Protocol,
})
}
}
func ProtocolsSingleChange() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"hello": "lol",
})
}
}
func ProtocolsSingleReprocess() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"hello": "lol",
})
}
}
func ProtocolsSinglePublish() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"hello": "lol",
})
}
}