From 75044eef0331bb9448da813288aafc6735ce7c22 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Fri, 20 May 2022 10:13:46 -0600 Subject: Implement edit post page --- srv/src/api/api.go | 3 +- srv/src/api/assets.go | 4 +-- srv/src/api/posts.go | 32 ++++++++++++++++- srv/src/api/tpl.go | 4 +++ srv/src/api/tpl/edit-post.html | 82 ++++++++++++++++++++++++++++++++++++++++++ srv/src/api/tpl/index.html | 4 +-- srv/src/api/tpl/post.html | 4 +-- srv/src/api/tpl/posts.html | 2 +- 8 files changed, 126 insertions(+), 9 deletions(-) create mode 100644 srv/src/api/tpl/edit-post.html (limited to 'srv/src') diff --git a/srv/src/api/api.go b/srv/src/api/api.go index fc6f6a2..bc56164 100644 --- a/srv/src/api/api.go +++ b/srv/src/api/api.go @@ -214,7 +214,8 @@ func (a *api) handler() http.Handler { v2Mux.Handle("/follow.html", a.renderDumbTplHandler("follow.html")) v2Mux.Handle("/posts/", http.StripPrefix("/posts", apiutil.MethodMux(map[string]http.Handler{ - "GET": a.renderPostHandler(), + "GET": a.renderPostHandler(), + "EDIT": a.editPostHandler(), "DELETE": authMiddleware(auther, formMiddleware(a.deletePostHandler()), ), diff --git a/srv/src/api/assets.go b/srv/src/api/assets.go index c1cd75e..47be14c 100644 --- a/srv/src/api/assets.go +++ b/srv/src/api/assets.go @@ -149,7 +149,7 @@ func (a *api) postPostAssetHandler() http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { id := r.PostFormValue("id") - if id == "" { + if id == "/" { apiutil.BadRequest(rw, r, errors.New("id is required")) return } @@ -176,7 +176,7 @@ func (a *api) deletePostAssetHandler() http.Handler { id := filepath.Base(r.URL.Path) - if id == "" { + if id == "/" { apiutil.BadRequest(rw, r, errors.New("id is required")) return } diff --git a/srv/src/api/posts.go b/srv/src/api/posts.go index a906f14..e5916e1 100644 --- a/srv/src/api/posts.go +++ b/srv/src/api/posts.go @@ -138,13 +138,43 @@ func (a *api) renderPostsIndexHandler() http.Handler { }) } +func (a *api) editPostHandler() http.Handler { + + tpl := a.mustParseBasedTpl("edit-post.html") + + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + + id := filepath.Base(r.URL.Path) + + var storedPost post.StoredPost + + if id != "/" { + + var err error + storedPost, err = a.params.PostStore.GetByID(id) + + if errors.Is(err, post.ErrPostNotFound) { + http.Error(rw, "Post not found", 404) + return + } else if err != nil { + apiutil.InternalServerError( + rw, r, fmt.Errorf("fetching post with id %q: %w", id, err), + ) + return + } + } + + executeTemplate(rw, r, tpl, storedPost) + }) +} + func (a *api) deletePostHandler() http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { id := filepath.Base(r.URL.Path) - if id == "" { + if id == "/" { apiutil.BadRequest(rw, r, errors.New("id is required")) return } diff --git a/srv/src/api/tpl.go b/srv/src/api/tpl.go index 5e33bea..9818b10 100644 --- a/srv/src/api/tpl.go +++ b/srv/src/api/tpl.go @@ -8,6 +8,7 @@ import ( "net/http" "path/filepath" "strings" + "time" "github.com/mediocregopher/blog.mediocregopher.com/srv/api/apiutil" ) @@ -50,6 +51,9 @@ func (a *api) mustParseTpl(name string) *template.Template { path := filepath.Join("posts", id) return blogURL(path) }, + "DateTimeFormat": func(t time.Time) string { + return t.Format("2006-01-02") + }, }) tpl = template.Must(tpl.Parse(mustReadTplFile(name))) diff --git a/srv/src/api/tpl/edit-post.html b/srv/src/api/tpl/edit-post.html new file mode 100644 index 0000000..9e30d4d --- /dev/null +++ b/srv/src/api/tpl/edit-post.html @@ -0,0 +1,82 @@ +{{ define "body" }} + +
+ +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+ +
+ + +
+ +
+ + +
+ +
+ +
+
+ +
+
+ + + +
+ +{{ end }} + +{{ template "base.html" . }} diff --git a/srv/src/api/tpl/index.html b/srv/src/api/tpl/index.html index 946a3e4..e27cbef 100644 --- a/srv/src/api/tpl/index.html +++ b/srv/src/api/tpl/index.html @@ -7,9 +7,9 @@

{{ .Title }}

- {{ .PublishedAt.Format "2006-01-02" }} + {{ DateTimeFormat .PublishedAt }} {{ if not .LastUpdatedAt.IsZero }} - (Updated {{ .LastUpdatedAt.Format "2006-01-02" }}) + (Updated {{ DateTimeFormat .LastUpdatedAt }}) {{ end }}

{{ .Description }}

diff --git a/srv/src/api/tpl/post.html b/srv/src/api/tpl/post.html index fadab3c..474d7c2 100644 --- a/srv/src/api/tpl/post.html +++ b/srv/src/api/tpl/post.html @@ -5,10 +5,10 @@ {{ .Payload.Title }}
- {{ .Payload.PublishedAt.Format "2006-01-02" }} + {{ DateTimeFormat .Payload.PublishedAt }}  •  {{ if not .Payload.LastUpdatedAt.IsZero }} - (Updated {{ .Payload.LastUpdatedAt.Format "2006-01-02" }}) + (Updated {{ DateTimeFormat .Payload.LastUpdatedAt }})  •  {{ end }} {{ .Payload.Description }} diff --git a/srv/src/api/tpl/posts.html b/srv/src/api/tpl/posts.html index e701f59..714cf07 100644 --- a/srv/src/api/tpl/posts.html +++ b/srv/src/api/tpl/posts.html @@ -22,7 +22,7 @@

- +

-- cgit v1.2.3