Add ability to update tags when resubmitting article
This commit is contained in:
parent
548d2c6023
commit
e82a0c6abe
@ -6,10 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func (db *DB) WriteArticleTags(articleID int64, tagIDs []int64) error {
|
||||
query := `
|
||||
INSERT INTO articles_tags (article_id, tag_id)
|
||||
VALUES (?, ?)
|
||||
`
|
||||
query := "INSERT INTO articles_tags (article_id, tag_id) VALUES (?, ?)"
|
||||
|
||||
for i := 0; i < TxMaxRetries; i++ {
|
||||
err := func() error {
|
||||
@ -68,8 +65,8 @@ func (db *DB) GetArticleTags(articleID int64) ([]*Tag, error) {
|
||||
}
|
||||
|
||||
func (db *DB) UpdateArticleTags(articleID int64, tagIDs []int64) error {
|
||||
query := `
|
||||
`
|
||||
deleteQuery := "DELETE FROM articles_tags WHERE article_id = ?"
|
||||
insertQuery := "INSERT INTO articles_tags (article_id, tag_id) VALUES (?, ?)"
|
||||
|
||||
for i := 0; i < TxMaxRetries; i++ {
|
||||
err := func() error {
|
||||
@ -78,6 +75,22 @@ func (db *DB) UpdateArticleTags(articleID int64, tagIDs []int64) error {
|
||||
return fmt.Errorf("error starting transaction: %v", err)
|
||||
}
|
||||
|
||||
if _, err := tx.Exec(deleteQuery, articleID); err != nil {
|
||||
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
||||
log.Fatalf("transaction error: %v, rollback error: %v", err, rollbackErr)
|
||||
}
|
||||
return fmt.Errorf("error deleting entries from articles_tags before inserting new ones: %v", err)
|
||||
}
|
||||
|
||||
for _, tagID := range tagIDs {
|
||||
if _, err := tx.Exec(insertQuery, articleID, tagID); err != nil {
|
||||
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
||||
log.Fatalf("transaction error: %v, rollback error: %v", err, rollbackErr)
|
||||
}
|
||||
return fmt.Errorf("error inserting new entries into articles_tags: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
return fmt.Errorf("error committing transaction: %v", err)
|
||||
}
|
||||
|
@ -100,7 +100,7 @@ func ResubmitArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
description := r.PostFormValue("article-description")
|
||||
content := r.PostFormValue("article-content")
|
||||
|
||||
if err := db.UpdateAttributes(
|
||||
if err = db.UpdateAttributes(
|
||||
&model.Attribute{Table: "articles", ID: id, AttName: "title", Value: title},
|
||||
&model.Attribute{Table: "articles", ID: id, AttName: "description", Value: description},
|
||||
&model.Attribute{Table: "articles", ID: id, AttName: "content", Value: content},
|
||||
@ -111,6 +111,23 @@ func ResubmitArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
r.ParseForm()
|
||||
tags := make([]int64, 0)
|
||||
for _, tag := range r.Form["tags"] {
|
||||
tagID, err := strconv.ParseInt(tag, 10, 64)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
tags = append(tags, tagID)
|
||||
}
|
||||
if err = db.UpdateArticleTags(id, tags); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
session, err := s.Get(r, "cookie")
|
||||
if err != nil {
|
||||
tmpl, err := template.ParseFiles("web/templates/login.html")
|
||||
|
Loading…
x
Reference in New Issue
Block a user