Skip to content

Commit

Permalink
[pocketbase#80] fixed before hooks data and added optional intercepto…
Browse files Browse the repository at this point in the history
…r to upsert submit
  • Loading branch information
ganigeorgiev committed Jul 12, 2022
1 parent ce85798 commit 05a4071
Show file tree
Hide file tree
Showing 20 changed files with 547 additions and 161 deletions.
44 changes: 26 additions & 18 deletions apis/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,28 +166,32 @@ func (api *adminApi) create(c echo.Context) error {

// load request
if err := c.Bind(form); err != nil {
return rest.NewBadRequestError("Failed to read the submitted data due to invalid formatting.", err)
return rest.NewBadRequestError("Failed to load the submitted data due to invalid formatting.", err)
}

event := &core.AdminCreateEvent{
HttpContext: c,
Admin: admin,
}

handlerErr := api.app.OnAdminBeforeCreateRequest().Trigger(event, func(e *core.AdminCreateEvent) error {
// create the admin
if err := form.Submit(); err != nil {
return rest.NewBadRequestError("Failed to create admin.", err)
}
// create the admin
submitErr := form.Submit(func(next forms.InterceptorNextFunc) forms.InterceptorNextFunc {
return func() error {
return api.app.OnAdminBeforeCreateRequest().Trigger(event, func(e *core.AdminCreateEvent) error {
if err := next(); err != nil {
return rest.NewBadRequestError("Failed to create admin.", err)
}

return e.HttpContext.JSON(http.StatusOK, e.Admin)
return e.HttpContext.JSON(http.StatusOK, e.Admin)
})
}
})

if handlerErr == nil {
if submitErr == nil {
api.app.OnAdminAfterCreateRequest().Trigger(event)
}

return handlerErr
return submitErr
}

func (api *adminApi) update(c echo.Context) error {
Expand All @@ -205,28 +209,32 @@ func (api *adminApi) update(c echo.Context) error {

// load request
if err := c.Bind(form); err != nil {
return rest.NewBadRequestError("Failed to read the submitted data due to invalid formatting.", err)
return rest.NewBadRequestError("Failed to load the submitted data due to invalid formatting.", err)
}

event := &core.AdminUpdateEvent{
HttpContext: c,
Admin: admin,
}

handlerErr := api.app.OnAdminBeforeUpdateRequest().Trigger(event, func(e *core.AdminUpdateEvent) error {
// update the admin
if err := form.Submit(); err != nil {
return rest.NewBadRequestError("Failed to update admin.", err)
}
// update the admin
submitErr := form.Submit(func(next forms.InterceptorNextFunc) forms.InterceptorNextFunc {
return func() error {
return api.app.OnAdminBeforeUpdateRequest().Trigger(event, func(e *core.AdminUpdateEvent) error {
if err := next(); err != nil {
return rest.NewBadRequestError("Failed to update admin.", err)
}

return e.HttpContext.JSON(http.StatusOK, e.Admin)
return e.HttpContext.JSON(http.StatusOK, e.Admin)
})
}
})

if handlerErr == nil {
if submitErr == nil {
api.app.OnAdminAfterUpdateRequest().Trigger(event)
}

return handlerErr
return submitErr
}

func (api *adminApi) delete(c echo.Context) error {
Expand Down
9 changes: 0 additions & 9 deletions apis/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,6 @@ func TestAdminCreate(t *testing.T) {
},
ExpectedStatus: 400,
ExpectedContent: []string{`"data":{"email":{"code":"validation_required","message":"Cannot be blank."},"password":{"code":"validation_required","message":"Cannot be blank."}}`},
ExpectedEvents: map[string]int{
"OnAdminBeforeCreateRequest": 1,
},
},
{
Name: "authorized as admin + invalid data format",
Expand All @@ -532,9 +529,6 @@ func TestAdminCreate(t *testing.T) {
},
ExpectedStatus: 400,
ExpectedContent: []string{`"data":{"avatar":{"code":"validation_max_less_equal_than_required","message":"Must be no greater than 9."},"email":{"code":"validation_admin_email_exists","message":"Admin email already exists."},"password":{"code":"validation_length_out_of_range","message":"The length must be between 10 and 100."},"passwordConfirm":{"code":"validation_values_mismatch","message":"Values don't match."}}`},
ExpectedEvents: map[string]int{
"OnAdminBeforeCreateRequest": 1,
},
},
{
Name: "authorized as admin + valid data",
Expand Down Expand Up @@ -647,9 +641,6 @@ func TestAdminUpdate(t *testing.T) {
},
ExpectedStatus: 400,
ExpectedContent: []string{`"data":{"avatar":{"code":"validation_max_less_equal_than_required","message":"Must be no greater than 9."},"email":{"code":"validation_admin_email_exists","message":"Admin email already exists."},"password":{"code":"validation_length_out_of_range","message":"The length must be between 10 and 100."},"passwordConfirm":{"code":"validation_values_mismatch","message":"Values don't match."}}`},
ExpectedEvents: map[string]int{
"OnAdminBeforeUpdateRequest": 1,
},
},
{
Method: http.MethodPatch,
Expand Down
48 changes: 28 additions & 20 deletions apis/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,34 @@ func (api *collectionApi) create(c echo.Context) error {

form := forms.NewCollectionUpsert(api.app, collection)

// read
// load request
if err := c.Bind(form); err != nil {
return rest.NewBadRequestError("Failed to read the submitted data due to invalid formatting.", err)
return rest.NewBadRequestError("Failed to load the submitted data due to invalid formatting.", err)
}

event := &core.CollectionCreateEvent{
HttpContext: c,
Collection: collection,
}

handlerErr := api.app.OnCollectionBeforeCreateRequest().Trigger(event, func(e *core.CollectionCreateEvent) error {
// submit
if err := form.Submit(); err != nil {
return rest.NewBadRequestError("Failed to create the collection.", err)
}
// create the collection
submitErr := form.Submit(func(next forms.InterceptorNextFunc) forms.InterceptorNextFunc {
return func() error {
return api.app.OnCollectionBeforeCreateRequest().Trigger(event, func(e *core.CollectionCreateEvent) error {
if err := next(); err != nil {
return rest.NewBadRequestError("Failed to create the collection.", err)
}

return e.HttpContext.JSON(http.StatusOK, e.Collection)
return e.HttpContext.JSON(http.StatusOK, e.Collection)
})
}
})

if handlerErr == nil {
if submitErr == nil {
api.app.OnCollectionAfterCreateRequest().Trigger(event)
}

return handlerErr
return submitErr
}

func (api *collectionApi) update(c echo.Context) error {
Expand All @@ -110,30 +114,34 @@ func (api *collectionApi) update(c echo.Context) error {

form := forms.NewCollectionUpsert(api.app, collection)

// read
// load request
if err := c.Bind(form); err != nil {
return rest.NewBadRequestError("Failed to read the submitted data due to invalid formatting.", err)
return rest.NewBadRequestError("Failed to load the submitted data due to invalid formatting.", err)
}

event := &core.CollectionUpdateEvent{
HttpContext: c,
Collection: collection,
}

handlerErr := api.app.OnCollectionBeforeUpdateRequest().Trigger(event, func(e *core.CollectionUpdateEvent) error {
// submit
if err := form.Submit(); err != nil {
return rest.NewBadRequestError("Failed to update the collection.", err)
}
// update the collection
submitErr := form.Submit(func(next forms.InterceptorNextFunc) forms.InterceptorNextFunc {
return func() error {
return api.app.OnCollectionBeforeUpdateRequest().Trigger(event, func(e *core.CollectionUpdateEvent) error {
if err := next(); err != nil {
return rest.NewBadRequestError("Failed to update the collection.", err)
}

return e.HttpContext.JSON(http.StatusOK, e.Collection)
return e.HttpContext.JSON(http.StatusOK, e.Collection)
})
}
})

if handlerErr == nil {
if submitErr == nil {
api.app.OnCollectionAfterUpdateRequest().Trigger(event)
}

return handlerErr
return submitErr
}

func (api *collectionApi) delete(c echo.Context) error {
Expand Down
9 changes: 0 additions & 9 deletions apis/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,6 @@ func TestCollectionCreate(t *testing.T) {
`"name":{"code":"validation_required"`,
`"schema":{"code":"validation_required"`,
},
ExpectedEvents: map[string]int{
"OnCollectionBeforeCreateRequest": 1,
},
},
{
Name: "authorized as admin + invalid data (eg. existing name)",
Expand All @@ -315,9 +312,6 @@ func TestCollectionCreate(t *testing.T) {
`"name":{"code":"validation_collection_name_exists"`,
`"schema":{"0":{"name":{"code":"validation_required"`,
},
ExpectedEvents: map[string]int{
"OnCollectionBeforeCreateRequest": 1,
},
},
{
Name: "authorized as admin + valid data",
Expand Down Expand Up @@ -399,9 +393,6 @@ func TestCollectionUpdate(t *testing.T) {
`"data":{`,
`"name":{"code":"validation_collection_name_exists"`,
},
ExpectedEvents: map[string]int{
"OnCollectionBeforeUpdateRequest": 1,
},
},
{
Name: "authorized as admin + valid data",
Expand Down
46 changes: 27 additions & 19 deletions apis/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (api *recordApi) create(c echo.Context) error {
testRecord := models.NewRecord(collection)
testForm := forms.NewRecordUpsert(api.app, testRecord)
if err := testForm.LoadData(c.Request()); err != nil {
return rest.NewBadRequestError("Failed to read the submitted data due to invalid formatting.", err)
return rest.NewBadRequestError("Failed to load the submitted data due to invalid formatting.", err)
}

testErr := testForm.DrySubmit(func(txDao *daos.Dao) error {
Expand All @@ -210,28 +210,32 @@ func (api *recordApi) create(c echo.Context) error {

// load request
if err := form.LoadData(c.Request()); err != nil {
return rest.NewBadRequestError("Failed to read the submitted data due to invalid formatting.", err)
return rest.NewBadRequestError("Failed to load the submitted data due to invalid formatting.", err)
}

event := &core.RecordCreateEvent{
HttpContext: c,
Record: record,
}

handlerErr := api.app.OnRecordBeforeCreateRequest().Trigger(event, func(e *core.RecordCreateEvent) error {
// create the record
if err := form.Submit(); err != nil {
return rest.NewBadRequestError("Failed to create record.", err)
}
// create the record
submitErr := form.Submit(func(next forms.InterceptorNextFunc) forms.InterceptorNextFunc {
return func() error {
return api.app.OnRecordBeforeCreateRequest().Trigger(event, func(e *core.RecordCreateEvent) error {
if err := next(); err != nil {
return rest.NewBadRequestError("Failed to create record.", err)
}

return e.HttpContext.JSON(http.StatusOK, e.Record)
return e.HttpContext.JSON(http.StatusOK, e.Record)
})
}
})

if handlerErr == nil {
if submitErr == nil {
api.app.OnRecordAfterCreateRequest().Trigger(event)
}

return handlerErr
return submitErr
}

func (api *recordApi) update(c echo.Context) error {
Expand Down Expand Up @@ -276,28 +280,32 @@ func (api *recordApi) update(c echo.Context) error {

// load request
if err := form.LoadData(c.Request()); err != nil {
return rest.NewBadRequestError("Failed to read the submitted data due to invalid formatting.", err)
return rest.NewBadRequestError("Failed to load the submitted data due to invalid formatting.", err)
}

event := &core.RecordUpdateEvent{
HttpContext: c,
Record: record,
}

handlerErr := api.app.OnRecordBeforeUpdateRequest().Trigger(event, func(e *core.RecordUpdateEvent) error {
// update the record
if err := form.Submit(); err != nil {
return rest.NewBadRequestError("Failed to update record.", err)
}
// update the record
submitErr := form.Submit(func(next forms.InterceptorNextFunc) forms.InterceptorNextFunc {
return func() error {
return api.app.OnRecordBeforeUpdateRequest().Trigger(event, func(e *core.RecordUpdateEvent) error {
if err := next(); err != nil {
return rest.NewBadRequestError("Failed to update record.", err)
}

return e.HttpContext.JSON(http.StatusOK, e.Record)
return e.HttpContext.JSON(http.StatusOK, e.Record)
})
}
})

if handlerErr == nil {
if submitErr == nil {
api.app.OnRecordAfterUpdateRequest().Trigger(event)
}

return handlerErr
return submitErr
}

func (api *recordApi) delete(c echo.Context) error {
Expand Down
29 changes: 18 additions & 11 deletions apis/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (api *settingsApi) list(c echo.Context) error {

func (api *settingsApi) set(c echo.Context) error {
form := forms.NewSettingsUpsert(api.app)

// load request
if err := c.Bind(form); err != nil {
return rest.NewBadRequestError("An error occurred while reading the submitted data.", err)
}
Expand All @@ -50,22 +52,27 @@ func (api *settingsApi) set(c echo.Context) error {
NewSettings: form.Settings,
}

handlerErr := api.app.OnSettingsBeforeUpdateRequest().Trigger(event, func(e *core.SettingsUpdateEvent) error {
if err := form.Submit(); err != nil {
return rest.NewBadRequestError("An error occurred while submitting the form.", err)
}
// update the settings
submitErr := form.Submit(func(next forms.InterceptorNextFunc) forms.InterceptorNextFunc {
return func() error {
return api.app.OnSettingsBeforeUpdateRequest().Trigger(event, func(e *core.SettingsUpdateEvent) error {
if err := next(); err != nil {
return rest.NewBadRequestError("An error occurred while submitting the form.", err)
}

redactedSettings, err := api.app.Settings().RedactClone()
if err != nil {
return rest.NewBadRequestError("", err)
}
redactedSettings, err := api.app.Settings().RedactClone()
if err != nil {
return rest.NewBadRequestError("", err)
}

return e.HttpContext.JSON(http.StatusOK, redactedSettings)
return e.HttpContext.JSON(http.StatusOK, redactedSettings)
})
}
})

if handlerErr == nil {
if submitErr == nil {
api.app.OnSettingsAfterUpdateRequest().Trigger(event)
}

return handlerErr
return submitErr
}
3 changes: 0 additions & 3 deletions apis/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ func TestSettingsSet(t *testing.T) {
`"emailAuth":{"minPasswordLength":{"code":"validation_min_greater_equal_than_required","message":"Must be no less than 5."}}`,
`"meta":{"appName":{"code":"validation_required","message":"Cannot be blank."}}`,
},
ExpectedEvents: map[string]int{
"OnSettingsBeforeUpdateRequest": 1,
},
},
{
Name: "authorized as admin submitting valid data",
Expand Down
Loading

0 comments on commit 05a4071

Please sign in to comment.