add endpoint to update blog article

This commit is contained in:
2026-05-24 16:06:52 +02:00
parent 617f4a7175
commit b22d328c75
3 changed files with 151 additions and 35 deletions
+104 -34
View File
@@ -55,6 +55,38 @@ func (db *Database) ValidateRootPassword(password string) (bool, error) {
return slices.Compare(passwordHash[:], rootPasswordHash) == 0, nil
}
func (db *Database) createBlogArticleSupplements(tx *sql.Tx, id int64, tags []string, files []ArticleFile) error {
for _, tag := range tags {
tagId, err := createOrGetTag(tx, tag)
if err != nil {
return err
}
_, err = tx.Exec(
"INSERT INTO blog_article_to_tag(tag_id, article_id) VALUES (?, ?)",
tagId,
id,
)
if err != nil {
return err
}
}
for _, file := range files {
_, err := tx.Exec(
"INSERT INTO blog_file(id, article_id, data) VALUES (?, ?, ?)",
file.Id,
id,
file.Data,
)
if err != nil {
return err
}
}
return nil
}
func (db *Database) CreateBlogArticle() (int64, func(*Article) error, error) {
tx, err := db.db.Begin()
if err != nil {
@@ -69,11 +101,13 @@ func (db *Database) CreateBlogArticle() (int64, func(*Article) error, error) {
"",
)
if err != nil {
_ = tx.Rollback()
return -1, nil, err
}
id, err := res.LastInsertId()
if err != nil {
_ = tx.Rollback()
return -1, nil, err
}
@@ -101,33 +135,10 @@ func (db *Database) CreateBlogArticle() (int64, func(*Article) error, error) {
return err
}
for _, tag := range article.Tags {
tagId, err := createOrGetTag(tx, tag)
if err != nil {
_ = tx.Rollback()
return err
}
_, err = tx.Exec(
"INSERT INTO blog_article_to_tag(tag_id, article_id) VALUES (?, ?)",
tagId,
id,
)
if err != nil {
return err
}
}
for _, file := range article.Files {
_, err = tx.Exec(
"INSERT INTO blog_file(id, article_id, data) VALUES (?, ?, ?)",
file.Id,
id,
file.Data,
)
if err != nil {
return err
}
err = db.createBlogArticleSupplements(tx, id, article.Tags, article.Files)
if err != nil {
_ = tx.Rollback()
return err
}
return tx.Commit()
@@ -354,19 +365,78 @@ func (db *Database) SetBlogArticleStatus(id int64, status ArticleStatus) error {
return nil
}
func (db *Database) UpdateBlogArticle(article *Article) error {
tx, err := db.db.Begin()
if err != nil {
return err
}
err = db.deleteBlogArticleSupplements(tx, article.Id)
if err != nil {
_ = tx.Rollback()
return err
}
var modificationDate *string
if article.ModificationDate != nil {
tmp := article.ModificationDate.Format(time.DateOnly)
modificationDate = &tmp
}
res, err := tx.Exec(
"UPDATE blog_article SET title = ?, date = ?, modification_date = ?, content = ? WHERE id = ?",
article.Title,
article.ReleaseDate.Format(time.DateOnly),
modificationDate,
article.Content,
article.Id,
)
if err != nil {
_ = tx.Rollback()
return err
}
affectedCount, err := res.RowsAffected()
if err != nil {
_ = tx.Rollback()
return err
}
if affectedCount == 0 {
_ = tx.Rollback()
return ErrNotFound
}
err = db.createBlogArticleSupplements(tx, article.Id, article.Tags, article.Files)
if err != nil {
_ = tx.Rollback()
return err
}
return tx.Commit()
}
func (db *Database) deleteBlogArticleSupplements(tx *sql.Tx, id int64) error {
_, err := tx.Exec("DELETE FROM blog_file WHERE article_id = ?", id)
if err != nil {
return err
}
_, err = tx.Exec("DELETE FROM blog_article_to_tag WHERE article_id = ?", id)
if err != nil {
return err
}
return nil
}
func (db *Database) DeleteBlogArticle(id int64) error {
tx, err := db.db.Begin()
if err != nil {
return err
}
_, err = tx.Exec("DELETE FROM blog_file WHERE article_id = ?", id)
if err != nil {
_ = tx.Rollback()
return err
}
_, err = tx.Exec("DELETE FROM blog_article_to_tag WHERE article_id = ?", id)
err = db.deleteBlogArticleSupplements(tx, id)
if err != nil {
_ = tx.Rollback()
return err