Add ability to delete articles
This commit is contained in:
parent
ccd63e7a2e
commit
c9aa3149ef
|
@ -0,0 +1,15 @@
|
||||||
|
{{ template "base.html" . }}
|
||||||
|
|
||||||
|
{{ define "content" }}
|
||||||
|
<div class="mw7 margin-center">
|
||||||
|
<h3 class="mb3">Are you sure you want to delete this article?</h3>
|
||||||
|
<div class="bg--dim pa3 br3 tl post-content">
|
||||||
|
<h1>{{ .Article.Title }}</h1>
|
||||||
|
{{ .Article.Content }}
|
||||||
|
</div>
|
||||||
|
<form action="{{ .SubmitUrl }}" method="POST" class="pv3 flex justify-end">
|
||||||
|
{{ csrftoken .Session }}
|
||||||
|
<input type="submit" value="Delete Article">
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{{ end }}
|
|
@ -191,13 +191,39 @@ func EducationArticleEditSubmit(c *RequestContext) ResponseData {
|
||||||
}
|
}
|
||||||
|
|
||||||
func EducationArticleDelete(c *RequestContext) ResponseData {
|
func EducationArticleDelete(c *RequestContext) ResponseData {
|
||||||
// TODO
|
article, err := fetchEduArticle(c, c.Conn, c.PathParams["slug"], 0)
|
||||||
panic("not implemented yet")
|
if errors.Is(err, db.NotFound) {
|
||||||
|
return FourOhFour(c)
|
||||||
|
} else if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
type deleteData struct {
|
||||||
|
templates.BaseData
|
||||||
|
Article templates.EduArticle
|
||||||
|
SubmitUrl string
|
||||||
|
}
|
||||||
|
|
||||||
|
baseData := getBaseData(c, fmt.Sprintf("Deleting \"%s\"", article.Title), nil)
|
||||||
|
|
||||||
|
var res ResponseData
|
||||||
|
res.MustWriteTemplate("education_article_delete.html", deleteData{
|
||||||
|
BaseData: baseData,
|
||||||
|
Article: templates.EducationArticleToTemplate(article),
|
||||||
|
SubmitUrl: hmnurl.BuildEducationArticleDelete(article.Slug),
|
||||||
|
}, c.Perf)
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func EducationArticleDeleteSubmit(c *RequestContext) ResponseData {
|
func EducationArticleDeleteSubmit(c *RequestContext) ResponseData {
|
||||||
// TODO
|
_, err := c.Conn.Exec(c, `DELETE FROM education_article WHERE slug = $1`, c.PathParams["slug"])
|
||||||
panic("not implemented yet")
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
res := c.Redirect(hmnurl.BuildEducationIndex(), http.StatusSeeOther)
|
||||||
|
res.AddFutureNotice("success", "Article deleted.")
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchEduArticle(ctx context.Context, dbConn db.ConnOrTx, slug string, t models.EduArticleType) (*models.EduArticle, error) {
|
func fetchEduArticle(ctx context.Context, dbConn db.ConnOrTx, slug string, t models.EduArticleType) (*models.EduArticle, error) {
|
||||||
|
|
|
@ -126,7 +126,7 @@ func NewWebsiteRoutes(conn *pgxpool.Pool) http.Handler {
|
||||||
hmnOnly.GET(hmnurl.RegexEducationArticleEdit, educationAuthorsOnly(EducationArticleEdit))
|
hmnOnly.GET(hmnurl.RegexEducationArticleEdit, educationAuthorsOnly(EducationArticleEdit))
|
||||||
hmnOnly.POST(hmnurl.RegexEducationArticleEdit, educationAuthorsOnly(EducationArticleEditSubmit))
|
hmnOnly.POST(hmnurl.RegexEducationArticleEdit, educationAuthorsOnly(EducationArticleEditSubmit))
|
||||||
hmnOnly.GET(hmnurl.RegexEducationArticleDelete, educationAuthorsOnly(EducationArticleDelete))
|
hmnOnly.GET(hmnurl.RegexEducationArticleDelete, educationAuthorsOnly(EducationArticleDelete))
|
||||||
hmnOnly.POST(hmnurl.RegexEducationArticleDelete, educationAuthorsOnly(EducationArticleDeleteSubmit))
|
hmnOnly.POST(hmnurl.RegexEducationArticleDelete, educationAuthorsOnly(csrfMiddleware(EducationArticleDeleteSubmit)))
|
||||||
|
|
||||||
hmnOnly.POST(hmnurl.RegexAPICheckUsername, csrfMiddleware(APICheckUsername))
|
hmnOnly.POST(hmnurl.RegexAPICheckUsername, csrfMiddleware(APICheckUsername))
|
||||||
|
|
||||||
|
|
Reference in New Issue