Blog deletion
This commit is contained in:
@@ -51,8 +51,7 @@ const (
|
|||||||
<input id="titleInput" name="title" value="{{.Blog.Title}}" type="text" onkeydown="return event.key != 'Enter';"/>
|
<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>
|
<textarea id="blogEditor" name="blog" oninput="blogEditorResize()">{{.Blog.RawBlog}}</textarea>
|
||||||
<div id="formResult">{{.Result}}</div>
|
<div id="formResult">{{.Result}}</div>
|
||||||
<p style="margin-right:0px;">
|
<p style="margin-right:0px;display:flex;">
|
||||||
<button class="formButton" type="submit">{{if eq .Blog.ID ""}}Create{{else}}Update{{end}}</button>
|
|
||||||
<button class="formButton"
|
<button class="formButton"
|
||||||
hx-get="/editor/edit"
|
hx-get="/editor/edit"
|
||||||
hx-include="#blogSelect"
|
hx-include="#blogSelect"
|
||||||
@@ -60,6 +59,16 @@ const (
|
|||||||
hx-confirm="Undo all your changes??">
|
hx-confirm="Undo all your changes??">
|
||||||
Cancel
|
Cancel
|
||||||
</button>
|
</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>
|
<p>
|
||||||
</form>`
|
</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()})
|
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 {
|
func verifyEditorCookie(r *http.Request) *backend.User {
|
||||||
authCookie, err := r.Cookie("blogAuthToken")
|
authCookie, err := r.Cookie("blogAuthToken")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -307,7 +307,7 @@ func (b *Backend) login(w http.ResponseWriter, r *http.Request) {
|
|||||||
} else {
|
} else {
|
||||||
if err == ErrLoginTimeout {
|
if err == ErrLoginTimeout {
|
||||||
ret.Error = "timeout"
|
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
|
ret.Timeout = u.Timeout
|
||||||
} else {
|
} else {
|
||||||
ret.Error = "incorrect"
|
ret.Error = "incorrect"
|
||||||
|
|||||||
@@ -262,6 +262,11 @@ func (b *BlogApp) UpdateBlog(ctx context.Context, ID string, updates bson.M) err
|
|||||||
return 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) {
|
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().
|
res, err := b.blogCol.Find(ctx, bson.M{"staticPage": false, "draft": false}, options.Find().
|
||||||
SetSort(bson.M{"createTime": -1}).
|
SetSort(bson.M{"createTime": -1}).
|
||||||
|
|||||||
@@ -160,6 +160,7 @@ func setupWebsite(mux *http.ServeMux) {
|
|||||||
// Editor stuff
|
// Editor stuff
|
||||||
mux.HandleFunc("GET /login", loginPageRequest)
|
mux.HandleFunc("GET /login", loginPageRequest)
|
||||||
mux.HandleFunc("GET /editor/", editorRequest)
|
mux.HandleFunc("GET /editor/", editorRequest)
|
||||||
|
mux.HandleFunc("DELETE /editor/edit", editorDelete)
|
||||||
mux.HandleFunc("GET /editor/edit", editorEdit)
|
mux.HandleFunc("GET /editor/edit", editorEdit)
|
||||||
mux.HandleFunc("POST /editor/post", editorPost)
|
mux.HandleFunc("POST /editor/post", editorPost)
|
||||||
mux.HandleFunc("POST /login", trueLoginRequest)
|
mux.HandleFunc("POST /login", trueLoginRequest)
|
||||||
|
|||||||
Reference in New Issue
Block a user