Check duplicates, redirect to the right place

This commit is contained in:
Ben Visness 2022-07-19 20:49:46 -05:00
parent a508c4bf6e
commit a17ac21f49
1 changed files with 18 additions and 8 deletions

View File

@ -140,6 +140,8 @@ func EducationArticleNewSubmit(c *RequestContext) ResponseData {
return c.ErrorResponse(http.StatusBadRequest, err)
}
var redirectUrl string
title := form.Get("title")
slug := form.Get("slug")
description := form.Get("description")
@ -147,8 +149,10 @@ func EducationArticleNewSubmit(c *RequestContext) ResponseData {
switch form.Get("type") {
case "article":
eduType = models.EduArticleTypeArticle
redirectUrl = hmnurl.BuildEducationArticle(slug)
case "glossary":
eduType = models.EduArticleTypeGlossary
redirectUrl = hmnurl.BuildEducationGlossary(slug)
default:
panic(fmt.Errorf("unknown education article type: %s", form.Get("type")))
}
@ -164,19 +168,25 @@ func EducationArticleNewSubmit(c *RequestContext) ResponseData {
}
defer tx.Rollback(c)
{
// articleID := db.MustQueryOneScalar[int](c, tx,
var articleID int
err := tx.QueryRow(c,
dupe := 0 < db.MustQueryOneScalar[int](c, tx,
`
SELECT COUNT(*) FROM education_article
WHERE slug = $1 AND type = $2
`,
slug, eduType,
)
if dupe {
return c.RejectRequest("A resource already exists with that slug and type.")
}
articleID := db.MustQueryOneScalar[int](c, tx,
`
INSERT INTO education_article (title, slug, description, published, type, current_version)
VALUES ($1, $2, $3, $4, $5, -1)
RETURNING id
`,
title, slug, description, published, eduType,
).Scan(&articleID)
if err != nil {
panic(err)
}
)
versionID := db.MustQueryOneScalar[int](c, tx,
`
INSERT INTO education_article_version (article_id, date, editor_id, content_raw, content_html)
@ -195,7 +205,7 @@ func EducationArticleNewSubmit(c *RequestContext) ResponseData {
panic(err)
}
return c.Redirect(hmnurl.BuildEducationArticle(slug), http.StatusSeeOther)
return c.Redirect(redirectUrl, http.StatusSeeOther)
}
func EducationArticleEdit(c *RequestContext) ResponseData {