-
Notifications
You must be signed in to change notification settings - Fork 0
/
swaphandler.go
277 lines (220 loc) · 8.06 KB
/
swaphandler.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"errors"
"github.com/gorilla/mux"
"github.com/joemahmah/gopher-write/story"
"net/http"
"strconv"
)
func moveItemInSlice(slice []int, targetIndex int, moveBeforeIndex int) ([]int, error) {
if targetIndex == moveBeforeIndex {
//Don't do anything, but don't error because
//the operation is already done...
return slice, nil
}
if targetIndex >= len(slice) {
return nil, errors.New("targetIndex out of bounds.")
}
if moveBeforeIndex > len(slice) {
return nil, errors.New("moveBeforeIndex out of bounds.")
}
if len(slice) == moveBeforeIndex {
left := slice[:targetIndex]
var right []int
//edge case, targetIndex is at right
if targetIndex == len(slice)-1 {
//Do noting since nothing is there
} else {
right = slice[targetIndex+1:]
}
//create new slice (eliminates slice BS)
var newSlice []int
newSlice = append(newSlice, left...)
newSlice = append(newSlice, right...)
newSlice = append(newSlice, slice[targetIndex])
//set stories to new slice
return newSlice, nil
} else if targetIndex < moveBeforeIndex {
left := slice[:targetIndex]
right := slice[moveBeforeIndex:]
between := slice[targetIndex+1 : moveBeforeIndex]
//create new slice (eliminates slice BS)
var newSlice []int
newSlice = append(newSlice, left...)
newSlice = append(newSlice, between...)
newSlice = append(newSlice, slice[targetIndex])
newSlice = append(newSlice, right...)
//set stories to new slice
return newSlice, nil
} else {
left := slice[:moveBeforeIndex]
right := slice[targetIndex+1:]
between := slice[moveBeforeIndex:targetIndex]
//create new slice (eliminates slice BS)
var newSlice []int
newSlice = append(newSlice, left...)
newSlice = append(newSlice, slice[targetIndex])
newSlice = append(newSlice, between...)
newSlice = append(newSlice, right...)
//set stories to new slice
return newSlice, nil
}
}
func moveItemFromSlice(slice []int, targetIndex int) (int, []int, error) {
targetAtEnd, err := moveItemInSlice(slice, targetIndex, len(slice))
if err != nil {
return 0, nil, err
}
return targetAtEnd[len(targetAtEnd)-1], targetAtEnd[:len(targetAtEnd)-1], nil
}
func insertItemIntoSlice(slice []int, item int, moveBeforeIndex int) ([]int, error) {
appendedSlice := append(slice, item)
newSlice, err := moveItemInSlice(appendedSlice, len(appendedSlice)-1, moveBeforeIndex)
if err != nil {
return nil, err
}
return newSlice, nil
}
func StoryMoveHandler(w http.ResponseWriter, r *http.Request) {
//Get the story uids
firstStoryIndex, _ := strconv.Atoi(mux.Vars(r)["first"])
secondStoryIndex, _ := strconv.Atoi(mux.Vars(r)["second"])
//If story indices are out of bounds
if secondStoryIndex > len(ActiveProject.Stories) || firstStoryIndex >= len(ActiveProject.Stories) || firstStoryIndex == secondStoryIndex {
w.WriteHeader(http.StatusInternalServerError)
LogError.Println("Story index out of bounds.")
return
}
//If moving story to end
if len(ActiveProject.Stories) == secondStoryIndex {
left := ActiveProject.Stories[:firstStoryIndex]
var right []*story.Story
//edge case, first is at right
if firstStoryIndex == len(ActiveProject.Stories)-1 {
//Do noting since nothing is there
} else {
right = ActiveProject.Stories[firstStoryIndex+1:]
}
//create new slice (eliminates slice BS)
var newStorySlice []*story.Story
newStorySlice = append(newStorySlice, left...)
newStorySlice = append(newStorySlice, right...)
newStorySlice = append(newStorySlice, ActiveProject.Stories[firstStoryIndex])
//set stories to new slice
ActiveProject.Stories = newStorySlice
} else if firstStoryIndex < secondStoryIndex {
left := ActiveProject.Stories[:firstStoryIndex]
right := ActiveProject.Stories[secondStoryIndex:]
between := ActiveProject.Stories[firstStoryIndex+1 : secondStoryIndex]
//create new slice (eliminates slice BS)
var newStorySlice []*story.Story
newStorySlice = append(newStorySlice, left...)
newStorySlice = append(newStorySlice, between...)
newStorySlice = append(newStorySlice, ActiveProject.Stories[firstStoryIndex])
newStorySlice = append(newStorySlice, right...)
//set stories to new slice
ActiveProject.Stories = newStorySlice
} else {
left := ActiveProject.Stories[:secondStoryIndex]
right := ActiveProject.Stories[firstStoryIndex+1:]
between := ActiveProject.Stories[secondStoryIndex:firstStoryIndex]
//create new slice (eliminates slice BS)
var newStorySlice []*story.Story
newStorySlice = append(newStorySlice, left...)
newStorySlice = append(newStorySlice, ActiveProject.Stories[firstStoryIndex])
newStorySlice = append(newStorySlice, between...)
newStorySlice = append(newStorySlice, right...)
//set stories to new slice
ActiveProject.Stories = newStorySlice
}
//Recalculate story UID
//Note: this is fairly slow, but realistically, there
//won't be that many stories that the process is too slow...
for index, story := range ActiveProject.Stories {
story.UID = index
}
//Write response
w.WriteHeader(http.StatusOK)
}
func IntraChapterMoveHandler(w http.ResponseWriter, r *http.Request) {
//Get the story uids
firstChapterIndex, _ := strconv.Atoi(mux.Vars(r)["first"])
secondChapterIndex, _ := strconv.Atoi(mux.Vars(r)["second"])
suid, _ := strconv.Atoi(mux.Vars(r)["fsuid"])
if len(ActiveProject.Stories) <= suid {
w.WriteHeader(http.StatusInternalServerError)
LogError.Println("Story index out of bounds.")
return
}
newChapterSlice, err := moveItemInSlice(ActiveProject.Stories[suid].Chapters, firstChapterIndex, secondChapterIndex)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
LogError.Println(err)
return
}
ActiveProject.Stories[suid].Chapters = newChapterSlice
w.WriteHeader(http.StatusOK)
}
func InterChapterMoveHandler(w http.ResponseWriter, r *http.Request) {
//Get the story uids
firstChapterIndex, _ := strconv.Atoi(mux.Vars(r)["first"])
secondChapterIndex, _ := strconv.Atoi(mux.Vars(r)["second"])
fsuid, _ := strconv.Atoi(mux.Vars(r)["fsuid"])
ssuid, _ := strconv.Atoi(mux.Vars(r)["ssuid"])
//Check if this is actually an intra story move
if fsuid == ssuid {
IntraChapterMoveHandler(w, r)
return
}
if len(ActiveProject.Stories) <= fsuid || len(ActiveProject.Stories) <= ssuid {
w.WriteHeader(http.StatusInternalServerError)
LogError.Println("Story index out of bounds.")
return
}
//Make new first chapter slice and get value (remove and get)
value, newFirstChapterSlice, err := moveItemFromSlice(ActiveProject.Stories[fsuid].Chapters, firstChapterIndex)
//Check for errors
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
LogError.Println(err)
return
}
//Make new second chapter slice (insert and move)
newSecondChapterSlice, err2 := insertItemIntoSlice(ActiveProject.Stories[ssuid].Chapters, value, secondChapterIndex)
//Check for errors
if err2 != nil {
w.WriteHeader(http.StatusInternalServerError)
LogError.Println(err2)
return
}
ActiveProject.Stories[fsuid].Chapters = newFirstChapterSlice
ActiveProject.Stories[ssuid].Chapters = newSecondChapterSlice
w.WriteHeader(http.StatusOK)
}
func IntraSectionMoveHandler(w http.ResponseWriter, r *http.Request) {
//Get the story uids
firstSectionIndex, _ := strconv.Atoi(mux.Vars(r)["first"])
secondSectionIndex, _ := strconv.Atoi(mux.Vars(r)["second"])
suid, _ := strconv.Atoi(mux.Vars(r)["fsuid"])
cuidRel, _ := strconv.Atoi(mux.Vars(r)["fcuid"])
//Process relative uids
cuid := ActiveProject.Stories[suid].Chapters[cuidRel]
if len(ActiveProject.Stories) <= suid {
w.WriteHeader(http.StatusInternalServerError)
LogError.Println("Story index out of bounds.")
return
}
if selectedChapter, exists := ActiveProject.Chapters[cuid]; exists {
newChapterSlice, err := moveItemInSlice(selectedChapter.Sections, firstSectionIndex, secondSectionIndex)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
LogError.Println(err)
return
}
selectedChapter.Sections = newChapterSlice
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusNotFound)
LogWarning.Println("Chapter with uid " + strconv.Itoa(cuid) + " does not exist in project " + ActiveProject.Name + ".")
}
}