forked from boombuler/knowledgedb
/
serve_entries.go
119 lines (106 loc) · 2.78 KB
/
serve_entries.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
package main
import (
"github.com/boombuler/knowledgedb/log"
"github.com/boombuler/knowledgedb/storage"
"github.com/gorilla/mux"
"github.com/nu7hatch/gouuid"
"net/http"
"time"
)
func (s *httpServer) withDoc(fn func(id string, entry *storage.Entry, w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, ok := mux.Vars(r)["id"]
if !ok {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
if _, err := uuid.ParseHex(id); err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
log.Warningf("Bad Document ID Request: %v", id)
return
}
entry := storage.LoadEntry(id)
if entry == nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
log.Warningf("Document %v not found", id)
return
}
fn(id, entry, w, r)
}
}
func (s *httpServer) serveCreateEntry(w http.ResponseWriter, r *http.Request) {
showPreview := false
entry := new(storage.Entry)
if r.Method == "POST" {
r.ParseForm()
entry.Body = r.Form["body"][0]
entry.Title = r.Form["title"][0]
entry.Tags = r.Form["tags"]
entry.UnifyTags()
if _, ok := r.Form["save"]; ok {
id := storage.CreateEntry(entry)
http.Redirect(w, r, "/view/"+id, http.StatusSeeOther)
} else if _, ok := r.Form["preview"]; ok {
showPreview = true
}
}
renderTemplate(w, "create_document", struct {
Title string
Content string
Tags []string
ShowPreview bool
}{
entry.Title,
entry.Body,
entry.Tags,
showPreview,
})
}
func (s *httpServer) serveDeleteEntry(entryId string, entry *storage.Entry, w http.ResponseWriter, r *http.Request) {
storage.DeleteEntry(entryId, entry)
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (s *httpServer) serveEditEntry(entryId string, entry *storage.Entry, w http.ResponseWriter, r *http.Request) {
showPreview := false
if r.Method == "POST" {
r.ParseForm()
entry.Tags = r.Form["tags"]
entry.Body = r.Form["body"][0]
entry.Title = r.Form["title"][0]
entry.UnifyTags()
if _, ok := r.Form["save"]; ok {
storage.UpdateEntry(entryId, entry)
http.Redirect(w, r, "/view/"+entryId, http.StatusSeeOther)
} else if _, ok := r.Form["preview"]; ok {
showPreview = true
}
}
renderTemplate(w, "edit_document", struct {
Title string
Id string
Content string
Tags []string
ShowPreview bool
}{
entry.Title,
entryId,
entry.Body,
entry.Tags,
showPreview,
})
}
func (s *httpServer) serveViewEntry(entryId string, entry *storage.Entry, w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "view_document", struct {
Id string
Title string
Content string
Tags []string
LastModified time.Time
}{
entryId,
entry.Title,
entry.Body,
entry.Tags,
entry.LastModified,
})
}