File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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.
338354func seedDefaultPages (db * gorm.DB ) {
339355 var count int64
Original file line number Diff line number Diff line change @@ -86,9 +86,9 @@ function uploadFile(fileInput) {
8686
8787function 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
124124function 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 )
You can’t perform that action at this time.
0 commit comments