/
history.go
179 lines (154 loc) · 4.74 KB
/
history.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
package main
import (
"net/http"
. "github.com/Deleplace/programming-idioms/idioms"
"github.com/gorilla/mux"
)
// IdiomHistoryFacade is the Facade for the Idiom History page.
type IdiomHistoryFacade struct {
PageMeta PageMeta
UserProfile UserProfile
Idiom *Idiom
// HistoryList contains incomplete IdiomHistory objects: just a few fields.
HistoryList []*IdiomHistory
}
func idiomHistory(w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
ctx := r.Context()
idiomIDStr := vars["idiomId"]
idiomID := String2Int(idiomIDStr)
_, list, err := dao.getIdiomHistoryList(ctx, idiomID)
if err != nil {
return err
}
if len(list) == 0 {
return PiErrorf(http.StatusInternalServerError, "No history entries found for idiom %q", idiomIDStr)
}
revertedVersionStr := r.FormValue("reverted")
revertedVersion := String2Int(revertedVersionStr)
if revertedVersion == list[0].Version {
// Most often, the deletion has not been taken into
// account by the datastore yet, so we remove manually.
list = list[1:]
}
_, idiom, err := dao.getIdiom(ctx, idiomID)
if err != nil {
return err
}
userProfile := readUserProfile(r)
myToggles := copyToggles(toggles)
myToggles["actionEditIdiom"] = false
myToggles["actionIdiomHistory"] = false
myToggles["actionAddImpl"] = false
data := &IdiomHistoryFacade{
PageMeta: PageMeta{
PageTitle: "Idiom " + idiomIDStr + " history",
Toggles: myToggles,
PreventIndexingRobots: true,
},
UserProfile: userProfile,
Idiom: idiom,
HistoryList: list,
}
return templates.ExecuteTemplate(w, "page-idiom-history", data)
}
// ImplHistoryFacade is the Facade for the Impl History page.
type ImplHistoryFacade struct {
PageMeta PageMeta
UserProfile UserProfile
Idiom *Idiom
HistoryList []*IdiomHistory
// ImplID because we're interested only in a specific implementation
ImplID int
}
func implHistory(w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
ctx := r.Context()
idiomIDStr := vars["idiomId"]
idiomID := String2Int(idiomIDStr)
implIDStr := vars["implId"]
implID := String2Int(implIDStr)
_, list, err := dao.getDenseHistoryList(ctx, idiomID)
if err != nil {
return err
}
if len(list) == 0 {
return PiErrorf(http.StatusInternalServerError, "No history entries found for idiom %q", idiomIDStr)
}
revertedVersionStr := r.FormValue("reverted")
revertedVersion := String2Int(revertedVersionStr)
if revertedVersion == list[0].Version {
// Most often, the deletion has not been taken into
// account by the datastore yet, so we remove manually.
list = list[1:]
}
sublist := make([]*IdiomHistory, 0, len(list))
for i := 0; i < len(list)-1; i++ {
h1, h2 := list[i], list[i+1]
_, impl1, ok1 := h1.FindImplInIdiom(implID)
_, impl2, ok2 := h2.FindImplInIdiom(implID)
if (ok1 != ok2) || (ok1 && ok2 && impl1.Version != impl2.Version) {
sublist = append(sublist, h1)
}
}
if len(list) > 0 {
h := list[len(list)-1]
if _, _, ok := h.FindImplInIdiom(implID); ok {
sublist = append(sublist, h)
}
}
list = sublist
_, idiom, err := dao.getIdiom(ctx, idiomID)
if err != nil {
return err
}
userProfile := readUserProfile(r)
myToggles := copyToggles(toggles)
myToggles["actionEditIdiom"] = false
myToggles["actionIdiomHistory"] = false
myToggles["actionAddImpl"] = false
data := &ImplHistoryFacade{
PageMeta: PageMeta{
PageTitle: "Idiom " + idiomIDStr + " history",
Toggles: myToggles,
PreventIndexingRobots: true,
},
UserProfile: userProfile,
Idiom: idiom,
HistoryList: list,
ImplID: implID,
}
return templates.ExecuteTemplate(w, "page-impl-history", data)
}
func revertIdiomVersion(w http.ResponseWriter, r *http.Request) error {
idiomIDStr := r.FormValue("idiomId")
idiomID := String2Int(idiomIDStr)
versionStr := r.FormValue("version")
version := String2Int(versionStr)
ctx := r.Context()
_, err := dao.revert(ctx, idiomID, version)
if err != nil {
return err
}
redirUrl := hostPrefix() + "/history/" + idiomIDStr + "?reverted=" + versionStr
http.Redirect(w, r, redirUrl, http.StatusFound)
return nil
// Unfortunately, the redirect page doesn't see the history deletion, yet.
}
func restoreIdiomVersion(w http.ResponseWriter, r *http.Request) error {
idiomIDStr := r.FormValue("idiomId")
idiomID := String2Int(idiomIDStr)
versionStr := r.FormValue("version")
version := String2Int(versionStr)
why := r.FormValue("why")
ctx := r.Context()
restoreUser := lookForNickname(r)
idiom, err := dao.historyRestore(ctx, idiomID, version, restoreUser, why)
if err != nil {
return err
}
redirUrl := NiceIdiomURL(idiom)
http.Redirect(w, r, redirUrl, http.StatusFound)
return nil
// Unfortunately, the redirect page doesn't see the history deletion, yet.
}