From fdedf1e7b8fcf1145e6b7a0543c8b98a27a3dc40 Mon Sep 17 00:00:00 2001 From: nagee Date: Mon, 4 Jul 2022 04:29:09 +0000 Subject: [PATCH] Add page for single entry TODO: need to handle id that doesn't exist --- server/server.go | 34 ++++++++++++++++++++++++++++++++++ server/templates/entry.html | 18 ++++++++++++++++++ server/templates/index.html | 2 +- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 server/templates/entry.html diff --git a/server/server.go b/server/server.go index 57ecab7..6c42b3e 100644 --- a/server/server.go +++ b/server/server.go @@ -12,6 +12,7 @@ import ( "os/exec" "path" "runtime" + "strconv" "git.nagee.dev/isthisnagee/diary/model" ) @@ -23,6 +24,11 @@ var templates map[string]*template.Template type App struct{ *model.App } +type DiaryEntryTemplateData struct { + Entry model.DiaryEntry + Notes []*model.DiaryEntryNote +} + func LoadTemplates() error { if templates == nil { templates = make(map[string]*template.Template) @@ -90,6 +96,33 @@ func (app *App) ListDiaryEntries(w http.ResponseWriter, r *http.Request) { } } +func (app *App) DiaryEntry(w http.ResponseWriter, r *http.Request) { + var tmpl, ok = templates["entry.html"] + if !ok { + http.Error(w, "could not find template index", http.StatusInternalServerError) + return + } + + var base = path.Base(r.URL.Path) + var id, err = strconv.Atoi(base) + if err != nil { + http.Error(w, "Cannot convert base to int: "+base, http.StatusBadRequest) + } + + entry, err := app.GetDiaryEntry(int64(id)) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + + var notes = app.GetDiaryEntryNotes(int64(id)) + + var templateData = DiaryEntryTemplateData{*entry, notes} + + if err := tmpl.Execute(w, templateData); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } +} + func setup() *App { home, err := os.UserHomeDir() if err != nil { @@ -111,6 +144,7 @@ func Run(openBrowserAtUrl bool, defaultPort int) { app := setup() http.HandleFunc("/", app.ListDiaryEntries) + http.HandleFunc("/entry/", app.DiaryEntry) var port int if defaultPort <= 0 { diff --git a/server/templates/entry.html b/server/templates/entry.html new file mode 100644 index 0000000..91fcd68 --- /dev/null +++ b/server/templates/entry.html @@ -0,0 +1,18 @@ + + + + + + Entry {{ .Entry.Id }} + + +

{{ .Entry.Title }}

+ {{ range .Notes }} +
+
+                {{ .Body }}
+            
+
+ {{ end }} + + diff --git a/server/templates/index.html b/server/templates/index.html index 04f430b..46f4bc7 100644 --- a/server/templates/index.html +++ b/server/templates/index.html @@ -9,7 +9,7 @@

diary