diff --git a/editor.go b/editor.go index 302ca17..9862585 100644 --- a/editor.go +++ b/editor.go @@ -51,8 +51,7 @@ const (
- +
+ + + +
` ) @@ -273,6 +282,43 @@ func editorPost(w http.ResponseWriter, r *http.Request) { pageTmpl.Execute(w, pageTmplStruct{Selected: newBlog.ID, Blogs: blogs, Editor: newForm.String()}) } +func editorDelete(w http.ResponseWriter, r *http.Request) { + usr := verifyEditorCookie(r) + if usr == nil { + editorRedirect(w, r, "/login") + return + } + if usr.Perm["blog"] != "admin" { + sendContent(w, r, "
You are not allowed to perform this action. Sorry, not sorry.
", "", "") + return + } + err := r.ParseForm() + if err != nil { + sendContent(w, r, "Bad request
", "", "") + return + } + id := r.FormValue("id") + if id == "" { + sendContent(w, r, "Invalid ID
", "", "") + return + } + err = blogApp.RemoveBlog(r.Context(), id) + if err != nil { + log.Println("error updating blog:", err) + sendContent(w, r, "Server error removing blog
", "", "") + return + } + var blogs []blog.BlogListResult + blogs, err = blogApp.AllBlogsList(r.Context()) + if err != nil { + log.Println("error getting all blogs list:", err) + sendContent(w, r, "Updated!
", "", "") + return + } + w.Header().Set("HX-Retarget", "#content") + pageTmpl.Execute(w, pageTmplStruct{Selected: "", Blogs: blogs, Editor: "Blog removed!
"}) +} + func verifyEditorCookie(r *http.Request) *backend.User { authCookie, err := r.Cookie("blogAuthToken") if err != nil { diff --git a/internal/backend/user.go b/internal/backend/user.go index 9347125..4aa6f5a 100644 --- a/internal/backend/user.go +++ b/internal/backend/user.go @@ -307,7 +307,7 @@ func (b *Backend) login(w http.ResponseWriter, r *http.Request) { } else { if err == ErrLoginTimeout { ret.Error = "timeout" - ret.ErrorMsg = fmt.Sprint("Timed out for", time.Unix(u.Timeout, 0).Sub(time.Now()), "seconds") + ret.ErrorMsg = fmt.Sprint("Timed out for", time.Until(time.Unix(u.Timeout, 0)), "seconds") ret.Timeout = u.Timeout } else { ret.Error = "incorrect" diff --git a/internal/blog/blog.go b/internal/blog/blog.go index 6bd8e04..748f97f 100644 --- a/internal/blog/blog.go +++ b/internal/blog/blog.go @@ -262,6 +262,11 @@ func (b *BlogApp) UpdateBlog(ctx context.Context, ID string, updates bson.M) err return err } +func (b *BlogApp) RemoveBlog(ctx context.Context, ID string) error { + _, err := b.blogCol.DeleteOne(ctx, bson.M{"_id": ID}) + return err +} + func (b *BlogApp) LatestBlogs(ctx context.Context, page int64) ([]*Blog, error) { res, err := b.blogCol.Find(ctx, bson.M{"staticPage": false, "draft": false}, options.Find(). SetSort(bson.M{"createTime": -1}). diff --git a/main.go b/main.go index f722cfa..dae0cf1 100644 --- a/main.go +++ b/main.go @@ -160,6 +160,7 @@ func setupWebsite(mux *http.ServeMux) { // Editor stuff mux.HandleFunc("GET /login", loginPageRequest) mux.HandleFunc("GET /editor/", editorRequest) + mux.HandleFunc("DELETE /editor/edit", editorDelete) mux.HandleFunc("GET /editor/edit", editorEdit) mux.HandleFunc("POST /editor/post", editorPost) mux.HandleFunc("POST /login", trueLoginRequest)