Blog deletion

This commit is contained in:
Caleb Gardner
2024-12-27 00:28:34 -06:00
parent aa180ae67e
commit 1229be1cee
4 changed files with 55 additions and 3 deletions
+48 -2
View File
@@ -51,8 +51,7 @@ const (
<input id="titleInput" name="title" value="{{.Blog.Title}}" type="text" onkeydown="return event.key != 'Enter';"/>
<textarea id="blogEditor" name="blog" oninput="blogEditorResize()">{{.Blog.RawBlog}}</textarea>
<div id="formResult">{{.Result}}</div>
<p style="margin-right:0px;">
<button class="formButton" type="submit">{{if eq .Blog.ID ""}}Create{{else}}Update{{end}}</button>
<p style="margin-right:0px;display:flex;">
<button class="formButton"
hx-get="/editor/edit"
hx-include="#blogSelect"
@@ -60,6 +59,16 @@ const (
hx-confirm="Undo all your changes??">
Cancel
</button>
<span style="flex-grow:1;"></span>
<button class="formButton"
hx-delete="/editor/edit"
hx-include="#blogSelect"
hx-target="#editor"
hx-confirm="Delete Page????">
Delete
</button>
<span style="flex-grow:1;"></span>
<button class="formButton" type="submit">{{if eq .Blog.ID ""}}Create{{else}}Update{{end}}</button>
<p>
</form>`
)
@@ -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, "<p>You are not allowed to perform this action. Sorry, not sorry.</p>", "", "")
return
}
err := r.ParseForm()
if err != nil {
sendContent(w, r, "<p>Bad request</p>", "", "")
return
}
id := r.FormValue("id")
if id == "" {
sendContent(w, r, "<p>Invalid ID</p>", "", "")
return
}
err = blogApp.RemoveBlog(r.Context(), id)
if err != nil {
log.Println("error updating blog:", err)
sendContent(w, r, "<p>Server error removing blog</p>", "", "")
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, "<p>Updated!</p>", "", "")
return
}
w.Header().Set("HX-Retarget", "#content")
pageTmpl.Execute(w, pageTmplStruct{Selected: "", Blogs: blogs, Editor: "<p>Blog removed!</p>"})
}
func verifyEditorCookie(r *http.Request) *backend.User {
authCookie, err := r.Cookie("blogAuthToken")
if err != nil {
+1 -1
View File
@@ -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"
+5
View File
@@ -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}).
+1
View File
@@ -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)