gonotes/internal/notes/views/views.go

172 lines
4.6 KiB
Go
Raw Normal View History

package views
import (
"log"
"net/http"
2025-01-27 22:28:18 +00:00
"os"
"path/filepath"
2025-07-25 13:44:21 +01:00
"strconv"
2025-01-27 22:28:18 +00:00
"strings"
2025-06-01 21:27:08 +01:00
urls "forgejo.gwairfelin.com/max/gispatcho"
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
"forgejo.gwairfelin.com/max/gonotes/internal/notes"
"forgejo.gwairfelin.com/max/gonotes/internal/templ"
)
var myurls urls.URLs
func GetRoutes(prefix string) *http.ServeMux {
myurls = urls.URLs{
Prefix: prefix,
URLs: map[string]urls.URL{
2025-07-25 13:44:21 +01:00
"list": {Path: "/", Protocol: "GET", Handler: list},
"new": {Path: "/new", Protocol: "GET", Handler: new},
"view_": {Path: "/{note}", Protocol: "GET", Handler: view},
"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},
"togglebox": {Path: "/{note}/togglebox/", Protocol: "POST", Handler: togglebox},
},
}
return myurls.GetRouter()
}
2025-01-27 22:28:18 +00:00
func view(w http.ResponseWriter, r *http.Request) {
title := r.PathValue("note")
note, err := notes.LoadNote(title)
2025-01-28 21:22:11 +00:00
urlEdit := myurls.Reverse("edit", urls.Repl{"note": title})
2025-06-18 21:40:42 +01:00
urlDelete := myurls.Reverse("delete", urls.Repl{"note": title})
if err != nil {
2025-01-28 21:22:11 +00:00
http.Redirect(w, r, urlEdit, http.StatusFound)
return
}
2025-06-18 21:40:42 +01:00
context := templ.Ctx{"note": note, "urlEdit": urlEdit, "urlDelete": urlDelete}
note.Render()
err = templ.RenderTemplate(w, "view.tmpl.html", context)
if err != nil {
2025-01-27 22:28:18 +00:00
log.Print(err.Error())
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
return
}
}
2025-01-27 22:28:18 +00:00
func edit(w http.ResponseWriter, r *http.Request) {
2025-06-27 22:56:50 +01:00
encodedTitle := r.PathValue("note")
note, err := notes.LoadNote(encodedTitle)
if err != nil {
2025-06-27 22:56:50 +01:00
title := notes.DecodeTitle(encodedTitle)
note = &notes.Note{Title: title}
}
2025-06-27 22:56:50 +01:00
urlSave := myurls.Reverse("save", urls.Repl{"note": encodedTitle})
2025-06-01 21:27:08 +01:00
context := templ.Ctx{"note": note, "urlSave": urlSave, "text": string(note.Body)}
err = templ.RenderTemplate(w, "edit.tmpl.html", context)
if err != nil {
2025-01-27 22:28:18 +00:00
log.Print(err.Error())
http.Error(w, "Couldn't load template", http.StatusInternalServerError)
return
}
}
2025-06-27 22:56:50 +01:00
func new(w http.ResponseWriter, r *http.Request) {
title := r.FormValue("title")
2025-07-25 10:48:41 +01:00
if len(title) == 0 {
title = "<New Note>"
}
2025-06-27 22:56:50 +01:00
note := &notes.Note{Title: title}
urlEdit := myurls.Reverse("edit", urls.Repl{"note": note.EncodedTitle()})
http.Redirect(w, r, urlEdit, http.StatusFound)
}
2025-06-18 21:40:42 +01:00
func delete(w http.ResponseWriter, r *http.Request) {
2025-06-27 22:56:50 +01:00
encodedTitle := r.PathValue("note")
err := notes.DeleteNote(encodedTitle)
2025-06-18 21:40:42 +01:00
if err != nil {
log.Print(err.Error())
http.Error(w, "Couldn't delete note", http.StatusInternalServerError)
return
}
2025-06-27 22:56:50 +01:00
urlList := myurls.Reverse("list", urls.Repl{})
http.Redirect(w, r, urlList, http.StatusFound)
2025-06-18 21:40:42 +01:00
}
2025-01-27 22:28:18 +00:00
func save(w http.ResponseWriter, r *http.Request) {
2025-06-18 21:51:46 +01:00
oldTitle := r.PathValue("note")
title := r.FormValue("title")
body := r.FormValue("body")
2025-06-18 21:51:46 +01:00
note := &notes.Note{Title: title, Body: []byte(body)}
note.Save()
2025-06-18 21:51:46 +01:00
2025-06-27 22:56:50 +01:00
if oldTitle != note.EncodedTitle() {
2025-06-18 21:51:46 +01:00
notes.DeleteNote(oldTitle)
}
2025-06-27 22:56:50 +01:00
http.Redirect(w, r, myurls.Reverse("view", urls.Repl{"note": note.EncodedTitle()}), http.StatusFound)
}
2025-07-25 13:44:21 +01:00
func togglebox(w http.ResponseWriter, r *http.Request) {
title := r.PathValue("note")
nthBox, err := strconv.Atoi(r.FormValue("box"))
if err != nil {
log.Fatal("You fucked up boy")
return
}
note, err := notes.LoadNote(title)
if err != nil {
http.Redirect(w, r, myurls.Reverse("view", urls.Repl{"note": title}), http.StatusFound)
return
}
note.ToggleBox(nthBox)
http.Redirect(w, r, myurls.Reverse("view", urls.Repl{"note": note.EncodedTitle()}), http.StatusFound)
}
type titleAndURL struct {
2025-06-27 22:56:50 +01:00
Title string
2025-07-25 13:44:21 +01:00
URL string
}
2025-01-27 22:28:18 +00:00
func list(w http.ResponseWriter, r *http.Request) {
files, err := os.ReadDir(conf.Conf.NotesDir)
if err != nil {
log.Print(err.Error())
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
2025-07-25 13:44:21 +01:00
titlesAndUrls := make([]titleAndURL, 0)
2025-01-27 22:28:18 +00:00
for _, f := range files {
if !f.IsDir() {
2025-06-27 22:56:50 +01:00
encodedTitle := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
title := notes.DecodeTitle(encodedTitle)
log.Printf("Found note %s (title '%s')", encodedTitle, title)
titlesAndUrls = append(
titlesAndUrls,
2025-07-25 13:44:21 +01:00
titleAndURL{Title: title, URL: myurls.Reverse("view", urls.Repl{"note": encodedTitle})},
2025-06-27 22:56:50 +01:00
)
log.Print(titlesAndUrls)
2025-01-27 22:28:18 +00:00
}
}
2025-06-27 22:56:50 +01:00
urlNew := myurls.Reverse("new", urls.Repl{})
err = templ.RenderTemplate(w, "list.tmpl.html", templ.Ctx{"notes": titlesAndUrls, "urlNew": urlNew})
2025-01-27 22:28:18 +00:00
if err != nil {
log.Print(err.Error())
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}