1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-11-28 18:11:17 +02:00
pocketbase/apis/collection.go

211 lines
6.3 KiB
Go
Raw Normal View History

2022-07-06 23:19:05 +02:00
package apis
import (
"net/http"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/forms"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tools/search"
)
2022-10-30 10:28:14 +02:00
// bindCollectionApi registers the collection api endpoints and the corresponding handlers.
func bindCollectionApi(app core.App, rg *echo.Group) {
2022-07-06 23:19:05 +02:00
api := collectionApi{app: app}
subGroup := rg.Group("/collections", ActivityLogger(app), RequireAdminAuth())
subGroup.GET("", api.list)
subGroup.POST("", api.create)
subGroup.GET("/:collection", api.view)
subGroup.PATCH("/:collection", api.update)
subGroup.DELETE("/:collection", api.delete)
2022-08-06 21:16:58 +02:00
subGroup.PUT("/import", api.bulkImport)
2022-07-06 23:19:05 +02:00
}
type collectionApi struct {
app core.App
}
func (api *collectionApi) list(c echo.Context) error {
fieldResolver := search.NewSimpleFieldResolver(
2022-10-30 10:28:14 +02:00
"id", "created", "updated", "name", "system", "type",
2022-07-06 23:19:05 +02:00
)
collections := []*models.Collection{}
result, err := search.NewProvider(fieldResolver).
Query(api.app.Dao().CollectionQuery()).
ParseAndExec(c.QueryParams().Encode(), &collections)
2022-07-06 23:19:05 +02:00
if err != nil {
2022-10-30 10:28:14 +02:00
return NewBadRequestError("", err)
2022-07-06 23:19:05 +02:00
}
event := new(core.CollectionsListEvent)
event.HttpContext = c
event.Collections = collections
event.Result = result
2022-07-06 23:19:05 +02:00
return api.app.OnCollectionsListRequest().Trigger(event, func(e *core.CollectionsListEvent) error {
2023-07-20 09:40:03 +02:00
if e.HttpContext.Response().Committed {
return nil
}
2022-07-06 23:19:05 +02:00
return e.HttpContext.JSON(http.StatusOK, e.Result)
})
}
func (api *collectionApi) view(c echo.Context) error {
collection, err := api.app.Dao().FindCollectionByNameOrId(c.PathParam("collection"))
if err != nil || collection == nil {
2022-10-30 10:28:14 +02:00
return NewNotFoundError("", err)
2022-07-06 23:19:05 +02:00
}
event := new(core.CollectionViewEvent)
event.HttpContext = c
event.Collection = collection
2022-07-06 23:19:05 +02:00
return api.app.OnCollectionViewRequest().Trigger(event, func(e *core.CollectionViewEvent) error {
2023-07-20 09:40:03 +02:00
if e.HttpContext.Response().Committed {
return nil
}
2022-07-06 23:19:05 +02:00
return e.HttpContext.JSON(http.StatusOK, e.Collection)
})
}
func (api *collectionApi) create(c echo.Context) error {
collection := &models.Collection{}
form := forms.NewCollectionUpsert(api.app, collection)
// load request
2022-07-06 23:19:05 +02:00
if err := c.Bind(form); err != nil {
2022-10-30 10:28:14 +02:00
return NewBadRequestError("Failed to load the submitted data due to invalid formatting.", err)
2022-07-06 23:19:05 +02:00
}
event := new(core.CollectionCreateEvent)
event.HttpContext = c
event.Collection = collection
2022-07-06 23:19:05 +02:00
// create the collection
2023-05-29 20:50:07 +02:00
return form.Submit(func(next forms.InterceptorNextFunc[*models.Collection]) forms.InterceptorNextFunc[*models.Collection] {
return func(m *models.Collection) error {
event.Collection = m
return api.app.OnCollectionBeforeCreateRequest().Trigger(event, func(e *core.CollectionCreateEvent) error {
if err := next(e.Collection); err != nil {
2022-10-30 10:28:14 +02:00
return NewBadRequestError("Failed to create the collection.", err)
}
2022-07-06 23:19:05 +02:00
2023-07-18 14:31:36 +02:00
return api.app.OnCollectionAfterCreateRequest().Trigger(event, func(e *core.CollectionCreateEvent) error {
2023-07-20 09:40:03 +02:00
if e.HttpContext.Response().Committed {
return nil
}
2023-07-18 14:31:36 +02:00
return e.HttpContext.JSON(http.StatusOK, e.Collection)
})
})
}
2022-07-06 23:19:05 +02:00
})
}
func (api *collectionApi) update(c echo.Context) error {
collection, err := api.app.Dao().FindCollectionByNameOrId(c.PathParam("collection"))
if err != nil || collection == nil {
2022-10-30 10:28:14 +02:00
return NewNotFoundError("", err)
2022-07-06 23:19:05 +02:00
}
form := forms.NewCollectionUpsert(api.app, collection)
// load request
2022-07-06 23:19:05 +02:00
if err := c.Bind(form); err != nil {
2022-10-30 10:28:14 +02:00
return NewBadRequestError("Failed to load the submitted data due to invalid formatting.", err)
2022-07-06 23:19:05 +02:00
}
event := new(core.CollectionUpdateEvent)
event.HttpContext = c
event.Collection = collection
2022-07-06 23:19:05 +02:00
// update the collection
2023-05-29 20:50:07 +02:00
return form.Submit(func(next forms.InterceptorNextFunc[*models.Collection]) forms.InterceptorNextFunc[*models.Collection] {
return func(m *models.Collection) error {
event.Collection = m
return api.app.OnCollectionBeforeUpdateRequest().Trigger(event, func(e *core.CollectionUpdateEvent) error {
if err := next(e.Collection); err != nil {
2022-10-30 10:28:14 +02:00
return NewBadRequestError("Failed to update the collection.", err)
}
2022-07-06 23:19:05 +02:00
2023-07-18 14:31:36 +02:00
return api.app.OnCollectionAfterUpdateRequest().Trigger(event, func(e *core.CollectionUpdateEvent) error {
2023-07-20 09:40:03 +02:00
if e.HttpContext.Response().Committed {
return nil
}
2023-07-18 14:31:36 +02:00
return e.HttpContext.JSON(http.StatusOK, e.Collection)
})
})
}
2022-07-06 23:19:05 +02:00
})
}
func (api *collectionApi) delete(c echo.Context) error {
collection, err := api.app.Dao().FindCollectionByNameOrId(c.PathParam("collection"))
if err != nil || collection == nil {
2022-10-30 10:28:14 +02:00
return NewNotFoundError("", err)
2022-07-06 23:19:05 +02:00
}
event := new(core.CollectionDeleteEvent)
event.HttpContext = c
event.Collection = collection
2022-07-06 23:19:05 +02:00
2023-05-29 20:50:07 +02:00
return api.app.OnCollectionBeforeDeleteRequest().Trigger(event, func(e *core.CollectionDeleteEvent) error {
2022-07-06 23:19:05 +02:00
if err := api.app.Dao().DeleteCollection(e.Collection); err != nil {
return NewBadRequestError("Failed to delete collection due to existing dependency.", err)
2022-07-06 23:19:05 +02:00
}
2023-07-18 14:31:36 +02:00
return api.app.OnCollectionAfterDeleteRequest().Trigger(event, func(e *core.CollectionDeleteEvent) error {
2023-07-20 09:40:03 +02:00
if e.HttpContext.Response().Committed {
return nil
}
2023-07-18 14:31:36 +02:00
return e.HttpContext.NoContent(http.StatusNoContent)
})
2023-05-29 20:50:07 +02:00
})
2022-07-06 23:19:05 +02:00
}
2022-08-05 05:00:38 +02:00
func (api *collectionApi) bulkImport(c echo.Context) error {
form := forms.NewCollectionsImport(api.app)
2022-08-07 19:58:21 +02:00
// load request data
2022-08-05 05:00:38 +02:00
if err := c.Bind(form); err != nil {
2022-10-30 10:28:14 +02:00
return NewBadRequestError("Failed to load the submitted data due to invalid formatting.", err)
2022-08-05 05:00:38 +02:00
}
event := new(core.CollectionsImportEvent)
event.HttpContext = c
event.Collections = form.Collections
2022-08-07 19:58:21 +02:00
// import collections
2023-05-29 20:50:07 +02:00
return form.Submit(func(next forms.InterceptorNextFunc[[]*models.Collection]) forms.InterceptorNextFunc[[]*models.Collection] {
return func(imports []*models.Collection) error {
event.Collections = imports
2022-08-07 19:58:21 +02:00
return api.app.OnCollectionsBeforeImportRequest().Trigger(event, func(e *core.CollectionsImportEvent) error {
if err := next(e.Collections); err != nil {
2022-10-30 10:28:14 +02:00
return NewBadRequestError("Failed to import the submitted collections.", err)
2022-08-07 19:58:21 +02:00
}
2023-07-18 14:31:36 +02:00
return api.app.OnCollectionsAfterImportRequest().Trigger(event, func(e *core.CollectionsImportEvent) error {
2023-07-20 09:40:03 +02:00
if e.HttpContext.Response().Committed {
return nil
}
2023-07-18 14:31:36 +02:00
return e.HttpContext.NoContent(http.StatusNoContent)
})
2022-08-07 19:58:21 +02:00
})
}
})
2022-08-05 05:00:38 +02:00
}