1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-01-10 00:43:36 +02:00
pocketbase/forms/admin_upsert.go

123 lines
3.3 KiB
Go
Raw Normal View History

2022-07-06 23:19:05 +02:00
package forms
import (
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/pocketbase/pocketbase/core"
2022-08-06 21:16:58 +02:00
"github.com/pocketbase/pocketbase/daos"
2022-07-06 23:19:05 +02:00
"github.com/pocketbase/pocketbase/forms/validators"
"github.com/pocketbase/pocketbase/models"
)
2022-10-30 10:28:14 +02:00
// AdminUpsert is a [models.Admin] upsert (create/update) form.
2022-07-06 23:19:05 +02:00
type AdminUpsert struct {
2022-10-30 10:28:14 +02:00
app core.App
dao *daos.Dao
admin *models.Admin
2022-07-06 23:19:05 +02:00
2022-08-06 21:16:58 +02:00
Id string `form:"id" json:"id"`
2022-07-06 23:19:05 +02:00
Avatar int `form:"avatar" json:"avatar"`
Email string `form:"email" json:"email"`
Password string `form:"password" json:"password"`
PasswordConfirm string `form:"passwordConfirm" json:"passwordConfirm"`
}
2022-08-06 21:16:58 +02:00
// NewAdminUpsert creates a new [AdminUpsert] form with initializer
// config created from the provided [core.App] and [models.Admin] instances
// (for create you could pass a pointer to an empty Admin - `&models.Admin{}`).
//
2022-10-30 10:28:14 +02:00
// If you want to submit the form as part of a transaction,
// you can change the default Dao via [SetDao()].
2022-07-06 23:19:05 +02:00
func NewAdminUpsert(app core.App, admin *models.Admin) *AdminUpsert {
form := &AdminUpsert{
2022-10-30 10:28:14 +02:00
app: app,
dao: app.Dao(),
admin: admin,
2022-08-07 14:38:21 +02:00
}
2022-07-06 23:19:05 +02:00
// load defaults
2022-08-06 21:16:58 +02:00
form.Id = admin.Id
2022-07-06 23:19:05 +02:00
form.Avatar = admin.Avatar
form.Email = admin.Email
return form
}
2022-10-30 10:28:14 +02:00
// SetDao replaces the default form Dao instance with the provided one.
func (form *AdminUpsert) SetDao(dao *daos.Dao) {
form.dao = dao
}
2022-07-06 23:19:05 +02:00
// Validate makes the form validatable by implementing [validation.Validatable] interface.
func (form *AdminUpsert) Validate() error {
return validation.ValidateStruct(form,
2022-08-06 21:16:58 +02:00
validation.Field(
&form.Id,
validation.When(
form.admin.IsNew(),
validation.Length(models.DefaultIdLength, models.DefaultIdLength),
2022-08-11 09:29:01 +02:00
validation.Match(idRegex),
2022-08-06 21:16:58 +02:00
).Else(validation.In(form.admin.Id)),
),
2022-07-06 23:19:05 +02:00
validation.Field(
&form.Avatar,
validation.Min(0),
validation.Max(9),
),
validation.Field(
&form.Email,
validation.Required,
validation.Length(1, 255),
is.EmailFormat,
2022-07-06 23:19:05 +02:00
validation.By(form.checkUniqueEmail),
),
validation.Field(
&form.Password,
2022-08-06 21:16:58 +02:00
validation.When(form.admin.IsNew(), validation.Required),
2022-10-30 10:28:14 +02:00
validation.Length(10, 72),
2022-07-06 23:19:05 +02:00
),
validation.Field(
&form.PasswordConfirm,
validation.When(form.Password != "", validation.Required),
validation.By(validators.Compare(form.Password)),
),
)
}
func (form *AdminUpsert) checkUniqueEmail(value any) error {
v, _ := value.(string)
2022-10-30 10:28:14 +02:00
if form.dao.IsAdminEmailUnique(v, form.admin.Id) {
2022-07-06 23:19:05 +02:00
return nil
}
return validation.NewError("validation_admin_email_exists", "Admin email already exists.")
}
// Submit validates the form and upserts the form admin model.
//
// You can optionally provide a list of InterceptorFunc to further
// modify the form behavior before persisting it.
func (form *AdminUpsert) Submit(interceptors ...InterceptorFunc) error {
2022-07-06 23:19:05 +02:00
if err := form.Validate(); err != nil {
return err
}
2022-08-06 21:16:58 +02:00
// custom insertion id can be set only on create
if form.admin.IsNew() && form.Id != "" {
form.admin.MarkAsNew()
form.admin.SetId(form.Id)
}
2022-07-06 23:19:05 +02:00
form.admin.Avatar = form.Avatar
form.admin.Email = form.Email
if form.Password != "" {
form.admin.SetPassword(form.Password)
}
return runInterceptors(func() error {
2022-10-30 10:28:14 +02:00
return form.dao.SaveAdmin(form.admin)
}, interceptors...)
2022-07-06 23:19:05 +02:00
}