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