Add delete view
This commit is contained in:
parent
cb6c12354a
commit
e2c59d5f28
3 changed files with 26 additions and 5 deletions
|
|
@ -58,3 +58,8 @@ 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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ func GetRoutes(prefix string) *http.ServeMux {
|
|||
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},
|
||||
|
|
@ -32,12 +33,13 @@ 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}
|
||||
context := templ.Ctx{"note": note, "urlEdit": urlEdit, "urlDelete": urlDelete}
|
||||
note.Render()
|
||||
err = templ.RenderTemplate(w, "view.tmpl.html", context)
|
||||
if err != nil {
|
||||
|
|
@ -64,6 +66,19 @@ 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) {
|
||||
title := r.PathValue("note")
|
||||
body := r.FormValue("body")
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
</div>
|
||||
<div>
|
||||
<a href="{{.urlEdit}}">Edit</a>
|
||||
<a href="{{.urlDelete}}">Delete</a>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue