Compare commits
No commits in common. "fd8e1408977cbab695212fe8a2b2998398dc2570" and "cb6c12354a01034308ac3156666da04a421b4958" have entirely different histories.
fd8e140897
...
cb6c12354a
4 changed files with 7 additions and 38 deletions
|
|
@ -58,8 +58,3 @@ func LoadNote(title string) (*Note, error) {
|
|||
}
|
||||
return &Note{Title: title, Body: body}, nil
|
||||
}
|
||||
|
||||
func DeleteNote(title string) error {
|
||||
filename := filepath.Join(conf.Conf.NotesDir, fmtPath(title))
|
||||
return os.Remove(filename)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,11 +19,10 @@ func GetRoutes(prefix string) *http.ServeMux {
|
|||
myurls = urls.URLs{
|
||||
Prefix: prefix,
|
||||
URLs: map[string]urls.URL{
|
||||
"view": {Path: "/{note}/", Protocol: "GET", Handler: view},
|
||||
"delete": {Path: "/{note}/delete/", Protocol: "GET", Handler: delete},
|
||||
"edit": {Path: "/{note}/edit/", Protocol: "GET", Handler: edit},
|
||||
"save": {Path: "/{note}/edit/save/", Protocol: "POST", Handler: save},
|
||||
"list": {Path: "/", Protocol: "GET", Handler: list},
|
||||
"view": {Path: "/{note}/", Protocol: "GET", Handler: view},
|
||||
"edit": {Path: "/{note}/edit/", Protocol: "GET", Handler: edit},
|
||||
"save": {Path: "/{note}/edit/save/", Protocol: "POST", Handler: save},
|
||||
"list": {Path: "/", Protocol: "GET", Handler: list},
|
||||
},
|
||||
}
|
||||
return myurls.GetRouter()
|
||||
|
|
@ -33,13 +32,12 @@ func view(w http.ResponseWriter, r *http.Request) {
|
|||
title := r.PathValue("note")
|
||||
note, err := notes.LoadNote(title)
|
||||
urlEdit := myurls.Reverse("edit", urls.Repl{"note": title})
|
||||
urlDelete := myurls.Reverse("delete", urls.Repl{"note": title})
|
||||
if err != nil {
|
||||
http.Redirect(w, r, urlEdit, http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
context := templ.Ctx{"note": note, "urlEdit": urlEdit, "urlDelete": urlDelete}
|
||||
context := templ.Ctx{"note": note, "urlEdit": urlEdit}
|
||||
note.Render()
|
||||
err = templ.RenderTemplate(w, "view.tmpl.html", context)
|
||||
if err != nil {
|
||||
|
|
@ -66,31 +64,11 @@ func edit(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
func delete(w http.ResponseWriter, r *http.Request) {
|
||||
title := r.PathValue("note")
|
||||
err := notes.DeleteNote(title)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Couldn't delete note", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
urlDelete := myurls.Reverse("list", urls.Repl{})
|
||||
http.Redirect(w, r, urlDelete, http.StatusFound)
|
||||
}
|
||||
|
||||
func save(w http.ResponseWriter, r *http.Request) {
|
||||
oldTitle := r.PathValue("note")
|
||||
title := r.FormValue("title")
|
||||
title := r.PathValue("note")
|
||||
body := r.FormValue("body")
|
||||
|
||||
note := ¬es.Note{Title: title, Body: []byte(body)}
|
||||
note.Save()
|
||||
|
||||
if oldTitle != title {
|
||||
notes.DeleteNote(oldTitle)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, myurls.Reverse("view", urls.Repl{"note": title}), http.StatusFound)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,10 +3,7 @@
|
|||
{{define "main"}}
|
||||
<form action="{{.urlSave}}" method="POST">
|
||||
<div class="mb-3">
|
||||
<input type="text" class="form-control" id="noteTitleInput" name="title" aria-described-by="titleHelp" value="{{.note.Title}}"/>
|
||||
<div id="titleHelp" class="form-text">Enter your note title</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="noteBodyInput" class="form-label">Note</label>
|
||||
<div class="border rounded rounded-1">
|
||||
<div id="toolbar"></div>
|
||||
<textarea class="form-control" id="noteBodyInput" name="body" aria-described-by="bodyHelp">{{.text}}</textarea>
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
</div>
|
||||
<div>
|
||||
<a href="{{.urlEdit}}">Edit</a>
|
||||
<a href="{{.urlDelete}}">Delete</a>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue