Skip to content

Commit 11672bb

Browse files
authored
Merge pull request #503 from compscidr/fix/tag-duplication-on-blur
Fix tag duplication on blur
2 parents 8101592 + ffe477d commit 11672bb

3 files changed

Lines changed: 38 additions & 7 deletions

File tree

admin/admin.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,23 @@ func (a *Admin) UpdatePost(c *gin.Context) {
187187
if err := tx.Model(&existingPost).Where("id = ?", requestPost.ID).Updates(&existingPost).Error; err != nil {
188188
return err
189189
}
190+
// Filter empty/whitespace tag names, deduplicate, and ensure each exists in the DB
191+
seen := make(map[string]bool)
192+
var validTags []blog.Tag
193+
for _, tag := range requestPost.Tags {
194+
name := strings.TrimSpace(tag.Name)
195+
if name == "" || seen[name] {
196+
continue
197+
}
198+
seen[name] = true
199+
var t blog.Tag
200+
if err := tx.Where("name = ?", name).FirstOrCreate(&t, blog.Tag{Name: name}).Error; err != nil {
201+
return err
202+
}
203+
validTags = append(validTags, t)
204+
}
190205
// Replace tag associations atomically to avoid duplication
191-
if err := tx.Model(&existingPost).Association("Tags").Replace(requestPost.Tags); err != nil {
206+
if err := tx.Model(&existingPost).Association("Tags").Replace(validTags); err != nil {
192207
return err
193208
}
194209
//https://stackoverflow.com/questions/56653423/gorm-doesnt-update-boolean-field-to-false

tools/migrate.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,26 @@ func Migrate(db *gorm.DB) error {
330330
seedBacklinks(db)
331331
seedDefaultPages(db)
332332
linkWritingPagesToPostType(db)
333+
cleanupEmptyTags(db)
333334

334335
return nil
335336
}
336337

338+
// cleanupEmptyTags removes empty-name tag associations and the empty tag itself.
339+
func cleanupEmptyTags(db *gorm.DB) {
340+
result := db.Exec("DELETE FROM post_tags WHERE tag_name = ''")
341+
if result.Error != nil {
342+
log.Printf("Warning: failed to clean up empty tag associations: %v", result.Error)
343+
return
344+
}
345+
if result.RowsAffected > 0 {
346+
log.Printf("Cleaned up %d empty tag associations", result.RowsAffected)
347+
}
348+
if err := db.Exec("DELETE FROM tags WHERE name = ''").Error; err != nil {
349+
log.Printf("Warning: failed to clean up empty tag: %v", err)
350+
}
351+
}
352+
337353
// seedDefaultPages creates the default pages (Writing, Research, About) if no pages exist.
338354
func seedDefaultPages(db *gorm.DB) {
339355
var count int64

www/js/admin-script.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ function uploadFile(fileInput) {
8686

8787
function updatePost(id, publish) {
8888
var tags = $("#tags").text().split(',')
89-
for (var i = 0; i < tags.length; i++) {
90-
tags[i] = {"name": tags[i].trim()}
91-
}
89+
.map(function(t) { return t.trim(); })
90+
.filter(function(t) { return t !== ""; })
91+
.map(function(t) { return {"name": t}; });
9292
var time = $("#created_at").val();
9393
var vtime = moment.utc(time)
9494

@@ -123,9 +123,9 @@ function updatePost(id, publish) {
123123

124124
function createPost(publish) {
125125
var tags = $("#tags").val().split(',')
126-
for (var i = 0; i < tags.length; i++) {
127-
tags[i] = {"name": tags[i].trim()}
128-
}
126+
.map(function(t) { return t.trim(); })
127+
.filter(function(t) { return t !== ""; })
128+
.map(function(t) { return {"name": t}; });
129129

130130
var time = $("#created_at").val();
131131
var vtime = moment.utc(time)

0 commit comments

Comments
 (0)