pocketbase/forms/settings_upsert.go

99 lines
2.6 KiB
Go
Raw Normal View History

2022-07-07 05:19:05 +08:00
package forms
import (
"os"
"time"
"github.com/pocketbase/pocketbase/core"
2022-08-07 20:38:21 +08:00
"github.com/pocketbase/pocketbase/daos"
2022-07-07 05:19:05 +08:00
"github.com/pocketbase/pocketbase/models"
)
2022-08-07 20:38:21 +08:00
// SettingsUpsert specifies a [core.Settings] upsert (create/update) form.
2022-07-07 05:19:05 +08:00
type SettingsUpsert struct {
*core.Settings
2022-08-07 20:38:21 +08:00
config SettingsUpsertConfig
2022-07-07 05:19:05 +08:00
}
2022-08-07 20:38:21 +08:00
// SettingsUpsertConfig is the [SettingsUpsert] factory initializer config.
//
// NB! App is required struct member.
type SettingsUpsertConfig struct {
App core.App
TxDao *daos.Dao
TxLogsDao *daos.Dao
}
// NewSettingsUpsert creates a new [SettingsUpsert] form with initializer
// config created from the provided [core.App] instance.
//
// If you want to submit the form as part of another transaction, use
// [NewSettingsUpsertWithConfig] with explicitly set TxDao.
2022-07-07 05:19:05 +08:00
func NewSettingsUpsert(app core.App) *SettingsUpsert {
2022-08-07 20:38:21 +08:00
return NewSettingsUpsertWithConfig(SettingsUpsertConfig{
App: app,
})
}
// NewSettingsUpsertWithConfig creates a new [SettingsUpsert] form
// with the provided config or panics on invalid configuration.
func NewSettingsUpsertWithConfig(config SettingsUpsertConfig) *SettingsUpsert {
form := &SettingsUpsert{config: config}
if form.config.App == nil {
panic("Missing required config.App instance.")
}
if form.config.TxDao == nil {
form.config.TxDao = form.config.App.Dao()
}
if form.config.TxLogsDao == nil {
form.config.TxLogsDao = form.config.App.LogsDao()
}
2022-07-07 05:19:05 +08:00
// load the application settings into the form
2022-08-07 20:38:21 +08:00
form.Settings, _ = config.App.Settings().Clone()
2022-07-07 05:19:05 +08:00
return form
}
// Validate makes the form validatable by implementing [validation.Validatable] interface.
func (form *SettingsUpsert) Validate() error {
return form.Settings.Validate()
}
// Submit validates the form and upserts the loaded settings.
//
// On success the app settings will be refreshed with the form ones.
//
// You can optionally provide a list of InterceptorFunc to further
// modify the form behavior before persisting it.
func (form *SettingsUpsert) Submit(interceptors ...InterceptorFunc) error {
2022-07-07 05:19:05 +08:00
if err := form.Validate(); err != nil {
return err
}
2022-08-07 20:38:21 +08:00
encryptionKey := os.Getenv(form.config.App.EncryptionEnv())
2022-07-07 05:19:05 +08:00
return runInterceptors(func() error {
2022-08-07 20:38:21 +08:00
saveErr := form.config.TxDao.SaveParam(
models.ParamAppSettings,
form.Settings,
encryptionKey,
)
if saveErr != nil {
return saveErr
}
2022-07-07 05:19:05 +08:00
// explicitly trigger old logs deletion
2022-08-07 20:38:21 +08:00
form.config.TxLogsDao.DeleteOldRequests(
time.Now().AddDate(0, 0, -1*form.Settings.Logs.MaxDays),
)
2022-07-07 05:19:05 +08:00
// merge the application settings with the form ones
2022-08-07 20:38:21 +08:00
return form.config.App.Settings().Merge(form.Settings)
}, interceptors...)
2022-07-07 05:19:05 +08:00
}