Add append API

single-block-api
Peter Stuifzand 3 years ago
parent 36d42b14d4
commit dbc21eefba

@ -881,6 +881,55 @@ func main() {
mp = NewFilePages(dataDir, searchIndex)
http.Handle("/auth/", &authHandler{})
http.HandleFunc("/api/document/append", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), 400)
return
}
id := r.Form.Get("id")
if id == "" {
http.Error(w, "missing id", 400)
return
}
page := mp.Get(id)
log.Println(page.Content)
var listItems []ListItem
err = json.NewDecoder(strings.NewReader(page.Content)).Decode(&listItems)
if err != nil && err != io.EOF {
http.Error(w, fmt.Sprintf("while decoding: %s", err.Error()), 500)
return
}
newId := &ID{"1", true}
listItems = append(listItems, ListItem{
ID: newId.NewID(),
Indented: 0,
Text: r.Form.Get("text"),
Fleeting: false,
})
var buf bytes.Buffer
err = json.NewEncoder(&buf).Encode(&listItems)
if err != nil {
http.Error(w, fmt.Sprintf("while encoding: %s", err.Error()), 500)
return
}
page.Content = buf.String()
page.Name = id
page.Title = id
err = mp.Save(id, page, "", "")
if err != nil {
http.Error(w, fmt.Sprintf("while saving: %s", err.Error()), 500)
return
}
return
})
http.HandleFunc("/links.json", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
http.ServeFile(w, r, filepath.Join(dataDir, LinksFile))

Loading…
Cancel
Save