-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategoryAddHandlers.go
More file actions
104 lines (89 loc) · 2.87 KB
/
categoryAddHandlers.go
File metadata and controls
104 lines (89 loc) · 2.87 KB
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
package gobookmarks
import (
"fmt"
"github.com/gorilla/sessions"
"golang.org/x/oauth2"
"net/http"
"strconv"
)
func AddCategoryPage(w http.ResponseWriter, r *http.Request) error {
session := r.Context().Value(ContextValues("session")).(*sessions.Session)
githubUser, _ := session.Values["GithubUser"].(*User)
token, _ := session.Values["Token"].(*oauth2.Token)
ref := r.URL.Query().Get("ref")
login := ""
if githubUser != nil {
login = githubUser.Login
}
_, sha, err := GetBookmarks(r.Context(), login, ref, token)
if err != nil {
return fmt.Errorf("GetBookmarks: %w", err)
}
col, _ := strconv.Atoi(r.URL.Query().Get("col"))
data := struct {
*CoreData
Error string
Index int
Text string
Sha string
Col int
}{
CoreData: r.Context().Value(ContextValues("coreData")).(*CoreData),
Error: r.URL.Query().Get("error"),
Index: -1,
Text: "Category: ",
Sha: sha,
Col: col,
}
if err := GetCompiledTemplates(NewFuncs(r)).ExecuteTemplate(w, "editCategory.gohtml", data); err != nil {
return fmt.Errorf("template: %w", err)
}
return nil
}
func CategoryAddSaveAction(w http.ResponseWriter, r *http.Request) error {
text := r.PostFormValue("text")
branch := r.PostFormValue("branch")
ref := r.PostFormValue("ref")
sha := r.PostFormValue("sha")
tabIdx, _ := strconv.Atoi(r.PostFormValue("tab"))
pageIdx, _ := strconv.Atoi(r.PostFormValue("page"))
colIdx, _ := strconv.Atoi(r.PostFormValue("col"))
session := r.Context().Value(ContextValues("session")).(*sessions.Session)
githubUser, _ := session.Values["GithubUser"].(*User)
token, _ := session.Values["Token"].(*oauth2.Token)
login := ""
if githubUser != nil {
login = githubUser.Login
}
currentBookmarks, curSha, err := GetBookmarks(r.Context(), login, ref, token)
if err != nil {
return fmt.Errorf("GetBookmarks: %w", err)
}
if sha != "" && curSha != sha {
return fmt.Errorf("bookmark modified concurrently")
}
list := ParseBookmarks(currentBookmarks)
if tabIdx < 0 || tabIdx >= len(list) {
tabIdx = 0
}
if pageIdx < 0 || pageIdx >= len(list[tabIdx].Pages) {
pageIdx = len(list[tabIdx].Pages) - 1
}
page := list[tabIdx].Pages[pageIdx]
lastBlock := page.Blocks[len(page.Blocks)-1]
if colIdx < 0 || colIdx >= len(lastBlock.Columns) {
colIdx = len(lastBlock.Columns) - 1
}
column := lastBlock.Columns[colIdx]
parsed := ParseBookmarks(text)
if len(parsed) == 0 || len(parsed[0].Pages) == 0 || len(parsed[0].Pages[0].Blocks) == 0 || len(parsed[0].Pages[0].Blocks[0].Columns) == 0 || len(parsed[0].Pages[0].Blocks[0].Columns[0].Categories) == 0 {
return fmt.Errorf("invalid category text")
}
cat := parsed[0].Pages[0].Blocks[0].Columns[0].Categories[0]
column.AddCategory(cat)
updated := list.String()
if err := UpdateBookmarks(r.Context(), login, token, ref, branch, updated, curSha); err != nil {
return fmt.Errorf("updateBookmark error: %w", err)
}
return nil
}