Skip to content

Commit c2243e1

Browse files
committed
Fix: filter entries deleteable with empty array
1 parent 6603b7b commit c2243e1

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

almanax.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ func handlePutAlmanax(w http.ResponseWriter, r *http.Request) {
473473
}
474474
}
475475

476-
if updateHook.BonusBlacklist != nil && len(updateHook.BonusBlacklist) > 0 {
476+
if updateHook.BonusBlacklist != nil {
477477
for _, blacklistEntry := range updateHook.BonusBlacklist {
478478
if !possibleBonuses.Has(blacklistEntry) {
479479
http.Error(w, "Unknown almanax bonus id: "+blacklistEntry+".", http.StatusBadRequest)
@@ -482,7 +482,7 @@ func handlePutAlmanax(w http.ResponseWriter, r *http.Request) {
482482
}
483483
}
484484

485-
if updateHook.BonusWhitelist != nil && len(updateHook.BonusWhitelist) > 0 {
485+
if updateHook.BonusWhitelist != nil {
486486
for _, blacklistEntry := range updateHook.BonusWhitelist {
487487
if !possibleBonuses.Has(blacklistEntry) {
488488
http.Error(w, "Unknown almanax bonus id: "+blacklistEntry+".", http.StatusBadRequest)

repository.go

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -431,16 +431,30 @@ func (r *Repository) UpdateAlmanaxHook(hook AlmanaxHookPut, id uuid.UUID) error
431431
}
432432

433433
if hook.BonusBlacklist != nil {
434-
_, err = r.conn.Exec(r.ctx, "update almanax_webhooks set blacklist = $1 where id = $2", hook.BonusBlacklist, id)
435-
if err != nil {
436-
return err
434+
if len(hook.BonusBlacklist) > 0 {
435+
_, err = r.conn.Exec(r.ctx, "update almanax_webhooks set blacklist = $1 where id = $2", hook.BonusBlacklist, id)
436+
if err != nil {
437+
return err
438+
}
439+
} else {
440+
_, err = r.conn.Exec(r.ctx, "update almanax_webhooks set blacklist = null where id = $1", id)
441+
if err != nil {
442+
return err
443+
}
437444
}
438445
}
439446

440447
if hook.BonusWhitelist != nil {
441-
_, err = r.conn.Exec(r.ctx, "update almanax_webhooks set whitelist = $1 where id = $2", hook.BonusWhitelist, id)
442-
if err != nil {
443-
return err
448+
if len(hook.BonusWhitelist) > 0 {
449+
_, err = r.conn.Exec(r.ctx, "update almanax_webhooks set whitelist = $1 where id = $2", hook.BonusWhitelist, id)
450+
if err != nil {
451+
return err
452+
}
453+
} else {
454+
_, err = r.conn.Exec(r.ctx, "update almanax_webhooks set whitelist = null where id = $1", id)
455+
if err != nil {
456+
return err
457+
}
444458
}
445459
}
446460
}
@@ -589,19 +603,38 @@ func (r *Repository) UpdateSocialHook(socialType string, hook ISocialHookUpdate)
589603
}
590604

591605
if hook.GetBlacklist() != nil {
592-
_, err = r.conn.Exec(r.ctx, "update "+tableName+" set blacklist = $1 where id = $2", hook.GetBlacklist(), hook.GetId())
606+
if len(hook.GetBlacklist()) > 0 {
607+
_, err = r.conn.Exec(r.ctx, "update "+tableName+" set blacklist = $1 where id = $2", hook.GetBlacklist(), hook.GetId())
608+
if err != nil {
609+
return err
610+
}
611+
} else {
612+
_, err = r.conn.Exec(r.ctx, "update "+tableName+" set blacklist = null where id = $1", hook.GetId())
613+
if err != nil {
614+
return err
615+
}
616+
}
593617
}
594618

595619
if hook.GetWhitelist() != nil {
596-
_, err = r.conn.Exec(r.ctx, "update "+tableName+" set whitelist = $1 where id = $2", hook.GetWhitelist(), hook.GetId())
620+
if len(hook.GetWhitelist()) > 0 {
621+
_, err = r.conn.Exec(r.ctx, "update "+tableName+" set whitelist = $1 where id = $2", hook.GetWhitelist(), hook.GetId())
622+
if err != nil {
623+
return err
624+
}
625+
} else {
626+
_, err = r.conn.Exec(r.ctx, "update "+tableName+" set whitelist = null where id = $1", hook.GetId())
627+
if err != nil {
628+
return err
629+
}
630+
}
597631
}
598632

599633
if hook.GetPreviewLength() != nil {
600634
_, err = r.conn.Exec(r.ctx, "update "+tableName+" set preview_length = $1 where id = $2", hook.GetPreviewLength(), hook.GetId())
601-
}
602-
603-
if err != nil {
604-
return err
635+
if err != nil {
636+
return err
637+
}
605638
}
606639

607640
return r.setUpdatedHookTimestamp(hook.GetId())

0 commit comments

Comments
 (0)