feat(19.3-04): purge the host site's page cache after FAQ saves and deletes
- FormAfterCreate/FormAfterUpdate run the locale check, then register a purge of kind faq through cooler.PurgeSiteCacheAfterCommit - new FormAfterDelete purges after the delete commits - purgeSiteCache helper takes the save transaction from cabana.TxFromContext and the purger from cooler.SiteCacheFromApp (site_cache.* keys) - TestFaqAdminPurgesSiteCache on Postgres
This commit is contained in:
@@ -49,16 +49,39 @@ func (faqsAdminController) AdminFieldRelations() []cabana.FieldRelationContract
|
||||
// errLocaleRequired is the field message of a FAQ saved without a locale.
|
||||
const errLocaleRequired = "golem15.faq::lang.errors.locale_required"
|
||||
|
||||
// siteCacheKind is the kind a FAQ save's site-cache purge carries.
|
||||
const siteCacheKind = "faq"
|
||||
|
||||
// FormAfterCreate refuses a FAQ without a locale. cabana writes locale_id
|
||||
// from the relation field after the model rules ran, so the check runs here,
|
||||
// inside the save's transaction: the refusal rolls the row back.
|
||||
func (faqsAdminController) FormAfterCreate(_ context.Context, model any) error {
|
||||
return requireLocale(model)
|
||||
// inside the save's transaction: the refusal rolls the row back. A FAQ that
|
||||
// passes purges the host site's page caches once the save commits.
|
||||
func (c faqsAdminController) FormAfterCreate(ctx context.Context, model any) error {
|
||||
return c.afterSave(ctx, model)
|
||||
}
|
||||
|
||||
// FormAfterUpdate applies the same check to an update.
|
||||
func (faqsAdminController) FormAfterUpdate(_ context.Context, model any) error {
|
||||
return requireLocale(model)
|
||||
// FormAfterUpdate applies the same check and purge to an update.
|
||||
func (c faqsAdminController) FormAfterUpdate(ctx context.Context, model any) error {
|
||||
return c.afterSave(ctx, model)
|
||||
}
|
||||
|
||||
// FormAfterDelete purges the host site's page caches once the delete
|
||||
// commits.
|
||||
func (c faqsAdminController) FormAfterDelete(ctx context.Context, model any) error {
|
||||
if faq, ok := model.(*models.FAQ); ok && faq != nil {
|
||||
purgeSiteCache(ctx, c.app, siteCacheKind, faq.Slug)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c faqsAdminController) afterSave(ctx context.Context, model any) error {
|
||||
if err := requireLocale(model); err != nil {
|
||||
return err
|
||||
}
|
||||
if faq, ok := model.(*models.FAQ); ok && faq != nil {
|
||||
purgeSiteCache(ctx, c.app, siteCacheKind, faq.Slug)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func requireLocale(model any) error {
|
||||
@@ -72,6 +95,7 @@ func requireLocale(model any) error {
|
||||
var (
|
||||
_ pact.FormAfterCreate = faqsAdminController{}
|
||||
_ pact.FormAfterUpdate = faqsAdminController{}
|
||||
_ pact.FormAfterDelete = faqsAdminController{}
|
||||
_ pact.AdminController = faqsAdminController{}
|
||||
_ pact.AdminPermissioned = faqsAdminController{}
|
||||
_ pact.AdminRecordSource = faqsAdminController{}
|
||||
|
||||
Reference in New Issue
Block a user