Keep note ownership in frontmatter and shiz
This commit is contained in:
parent
25bcf4d706
commit
03b6bb12ca
6 changed files with 136 additions and 54 deletions
|
|
@ -3,14 +3,9 @@ package views
|
|||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
urls "forgejo.gwairfelin.com/max/gispatcho"
|
||||
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
||||
"forgejo.gwairfelin.com/max/gonotes/internal/middleware"
|
||||
"forgejo.gwairfelin.com/max/gonotes/internal/notes"
|
||||
"forgejo.gwairfelin.com/max/gonotes/internal/templ"
|
||||
|
|
@ -40,18 +35,19 @@ func GetRoutes(prefix string) *http.ServeMux {
|
|||
}
|
||||
|
||||
func view(w http.ResponseWriter, r *http.Request) {
|
||||
user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
// user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
|
||||
title := r.PathValue("note")
|
||||
note, err := notes.LoadNote(user, title)
|
||||
note, err := notes.LoadNote(title)
|
||||
urlEdit := myurls.Reverse("edit", urls.Repl{"note": title})
|
||||
urlNew := myurls.Reverse("new", urls.Repl{})
|
||||
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, "urlDelete": urlDelete, "urlNew": urlNew}
|
||||
note.Render()
|
||||
err = templ.RenderTemplate(w, r, "view.tmpl.html", context)
|
||||
if err != nil {
|
||||
|
|
@ -62,10 +58,10 @@ func view(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func edit(w http.ResponseWriter, r *http.Request) {
|
||||
user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
// user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
|
||||
encodedTitle := r.PathValue("note")
|
||||
note, err := notes.LoadNote(user, encodedTitle)
|
||||
note, err := notes.LoadNote(encodedTitle)
|
||||
if err != nil {
|
||||
title := notes.DecodeTitle(encodedTitle)
|
||||
note = ¬es.Note{Title: title}
|
||||
|
|
@ -94,10 +90,10 @@ func new(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func delete(w http.ResponseWriter, r *http.Request) {
|
||||
user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
// user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
|
||||
encodedTitle := r.PathValue("note")
|
||||
err := notes.DeleteNote(user, encodedTitle)
|
||||
err := notes.DeleteNote(encodedTitle)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Couldn't delete note", http.StatusInternalServerError)
|
||||
|
|
@ -109,24 +105,24 @@ func delete(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func save(w http.ResponseWriter, r *http.Request) {
|
||||
user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
// user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
|
||||
oldTitle := r.PathValue("note")
|
||||
title := r.FormValue("title")
|
||||
body := r.FormValue("body")
|
||||
|
||||
note := ¬es.Note{Title: title, Body: []byte(body), Owner: user}
|
||||
note := ¬es.Note{Title: title, Body: []byte(body)}
|
||||
note.Save()
|
||||
|
||||
if oldTitle != note.EncodedTitle() {
|
||||
notes.DeleteNote(user, oldTitle)
|
||||
notes.DeleteNote(oldTitle)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, myurls.Reverse("view", urls.Repl{"note": note.EncodedTitle()}), http.StatusFound)
|
||||
}
|
||||
|
||||
func togglebox(w http.ResponseWriter, r *http.Request) {
|
||||
user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
// user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
|
||||
title := r.PathValue("note")
|
||||
nthBox, err := strconv.Atoi(r.FormValue("box"))
|
||||
|
|
@ -135,7 +131,7 @@ func togglebox(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
note, err := notes.LoadNote(user, title)
|
||||
note, err := notes.LoadNote(title)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, myurls.Reverse("view", urls.Repl{"note": title}), http.StatusFound)
|
||||
return
|
||||
|
|
@ -154,39 +150,22 @@ type titleAndURL struct {
|
|||
func list(w http.ResponseWriter, r *http.Request) {
|
||||
user := r.Context().Value(middleware.ContextKey("user")).(string)
|
||||
|
||||
notesDir := path.Join(conf.Conf.NotesDir, user)
|
||||
|
||||
files, err := os.ReadDir(notesDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
os.MkdirAll(notesDir, os.FileMode(0750))
|
||||
} else {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
titlesAndUrls := make([]titleAndURL, 0)
|
||||
|
||||
for _, f := range files {
|
||||
if !f.IsDir() {
|
||||
encodedTitle := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
|
||||
title := notes.DecodeTitle(encodedTitle)
|
||||
ns := notes.Notes.Get(user)
|
||||
log.Printf("Notes: %+v", notes.Notes)
|
||||
log.Printf("Notes for %s: %+v", user, ns)
|
||||
|
||||
log.Printf("Found note %s (title '%s')", encodedTitle, title)
|
||||
|
||||
titlesAndUrls = append(
|
||||
titlesAndUrls,
|
||||
titleAndURL{Title: title, URL: myurls.Reverse("view", urls.Repl{"note": encodedTitle})},
|
||||
)
|
||||
log.Print(titlesAndUrls)
|
||||
}
|
||||
for _, note := range ns {
|
||||
titlesAndUrls = append(
|
||||
titlesAndUrls,
|
||||
titleAndURL{Title: note.Title, URL: myurls.Reverse("view", urls.Repl{"note": note.EncodedTitle()})},
|
||||
)
|
||||
}
|
||||
|
||||
urlNew := myurls.Reverse("new", urls.Repl{})
|
||||
|
||||
err = templ.RenderTemplate(w, r, "list.tmpl.html", templ.Ctx{"notes": titlesAndUrls, "urlNew": urlNew})
|
||||
err := templ.RenderTemplate(w, r, "list.tmpl.html", templ.Ctx{"notes": titlesAndUrls, "urlNew": urlNew})
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue