/
pipeline.go
180 lines (140 loc) · 4.74 KB
/
pipeline.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
package handlers
import (
"time"
"net/http"
"github.com/freitagsrunde/protokollamt/models"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/satori/go.uuid"
)
// NewRemovalPayload represents the form data
// needed to create a new removal element in the
// analysis pipeline.
type NewRemovalPayload struct {
StartTag string `form:"removal-start"`
EndTag string `form:"removal-end"`
}
// NewReplacementPayload represents the form data
// needed to create a new replacement element in the
// analysis pipeline.
type NewReplacementPayload struct {
SearchString string `form:"replacement-search"`
ReplaceString string `form:"replacement-replace"`
}
// Pipeline provides an overview list of all
// configured removal and replacement elements
// executed during the analysis pipeline.
func Pipeline(dbConner DBConner) gin.HandlerFunc {
return func(c *gin.Context) {
var Removals []models.Removal
dbConner.GetDBConn().Order("\"created\" asc").Find(&Removals)
var Replacements []models.Replacement
dbConner.GetDBConn().Order("\"created\" asc").Find(&Replacements)
c.HTML(http.StatusOK, "pipeline-list.html", gin.H{
"PageTitle": "Protokollamt der Freitagsrunde - Analyse-Pipeline",
"MainTitle": "Analyse-Pipeline",
"Removals": Removals,
"Replacements": Replacements,
})
}
}
// PipelineRemovalsAdd expects a NewRemovalPayload
// and adds described removal element to database.
func PipelineRemovalsAdd(dbConner DBConner) gin.HandlerFunc {
return func(c *gin.Context) {
var Payload NewRemovalPayload
var Removal models.Removal
// Parse removal form data into above defined payload.
err := c.BindWith(&Payload, binding.FormPost)
if err != nil {
c.Redirect(http.StatusFound, "/pipeline")
c.Abort()
return
}
// Do not allow empty removal elements.
if Payload.StartTag == "" || Payload.EndTag == "" {
c.Redirect(http.StatusFound, "/pipeline")
c.Abort()
return
}
// Fill new removal element.
Removal.ID = uuid.NewV4().String()
Removal.Created = time.Now()
Removal.StartTag = Payload.StartTag
Removal.EndTag = Payload.EndTag
// Save removal element to database.
dbConner.GetDBConn().Create(&Removal)
c.Redirect(http.StatusFound, "/pipeline")
}
}
// PipelineRemovalsDelete removes the specified
// removal element from database, if existent.
func PipelineRemovalsDelete(dbConner DBConner) gin.HandlerFunc {
return func(c *gin.Context) {
// Obtain ID of requested removal element.
removalID := c.Param("id")
var Removal models.Removal
dbConner.GetDBConn().Where("\"id\" = ?", removalID).First(&Removal)
// If no result could be found for provided
// removal ID, redirect back to pipeline page.
if Removal.ID == "" {
c.Redirect(http.StatusFound, "/pipeline")
c.Abort()
return
}
// Now we are sure that requested removal element
// exists, therefore we can safely delete it.
dbConner.GetDBConn().Delete(&Removal)
c.Redirect(http.StatusFound, "/pipeline")
}
}
// PipelineReplacementsAdd expects a NewReplacementPayload
// and adds described replacement element to database.
func PipelineReplacementsAdd(dbConner DBConner) gin.HandlerFunc {
return func(c *gin.Context) {
var Payload NewReplacementPayload
var Replacement models.Replacement
// Parse replacement form data into above defined payload.
err := c.BindWith(&Payload, binding.FormPost)
if err != nil {
c.Redirect(http.StatusFound, "/pipeline")
c.Abort()
return
}
// Do not allow empty replacement elements.
if Payload.SearchString == "" || Payload.ReplaceString == "" {
c.Redirect(http.StatusFound, "/pipeline")
c.Abort()
return
}
// Fill new replacement element.
Replacement.ID = uuid.NewV4().String()
Replacement.Created = time.Now()
Replacement.SearchString = Payload.SearchString
Replacement.ReplaceString = Payload.ReplaceString
// Save replacement element to database.
dbConner.GetDBConn().Create(&Replacement)
c.Redirect(http.StatusFound, "/pipeline")
}
}
// PipelineReplacementsDelete removes the specified
// replacement element from database, if existent.
func PipelineReplacementsDelete(dbConner DBConner) gin.HandlerFunc {
return func(c *gin.Context) {
// Obtain ID of requested replacement element.
replacementID := c.Param("id")
var Replacement models.Replacement
dbConner.GetDBConn().Where("\"id\" = ?", replacementID).First(&Replacement)
// If no result could be found for provided
// replacement ID, redirect back to pipeline page.
if Replacement.ID == "" {
c.Redirect(http.StatusFound, "/pipeline")
c.Abort()
return
}
// Now we are sure that requested replacement element
// exists, therefore we can safely delete it.
dbConner.GetDBConn().Delete(&Replacement)
c.Redirect(http.StatusFound, "/pipeline")
}
}