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/rest"
|
|
|
|
"github.com/pocketbase/pocketbase/tools/search"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BindCollectionApi registers the collection api endpoints and the corresponding handlers.
|
|
|
|
func BindCollectionApi(app core.App, rg *echo.Group) {
|
|
|
|
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-05 05:00:38 +02:00
|
|
|
subGroup.POST("/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(
|
|
|
|
"id", "created", "updated", "name", "system",
|
|
|
|
)
|
|
|
|
|
|
|
|
collections := []*models.Collection{}
|
|
|
|
|
|
|
|
result, err := search.NewProvider(fieldResolver).
|
|
|
|
Query(api.app.Dao().CollectionQuery()).
|
|
|
|
ParseAndExec(c.QueryString(), &collections)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return rest.NewBadRequestError("", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
event := &core.CollectionsListEvent{
|
|
|
|
HttpContext: c,
|
|
|
|
Collections: collections,
|
|
|
|
Result: result,
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.app.OnCollectionsListRequest().Trigger(event, func(e *core.CollectionsListEvent) error {
|
|
|
|
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 {
|
|
|
|
return rest.NewNotFoundError("", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
event := &core.CollectionViewEvent{
|
|
|
|
HttpContext: c,
|
|
|
|
Collection: collection,
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.app.OnCollectionViewRequest().Trigger(event, func(e *core.CollectionViewEvent) error {
|
|
|
|
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)
|
|
|
|
|
2022-07-12 12:42:06 +02:00
|
|
|
// load request
|
2022-07-06 23:19:05 +02:00
|
|
|
if err := c.Bind(form); err != nil {
|
2022-07-12 12:42:06 +02:00
|
|
|
return rest.NewBadRequestError("Failed to load the submitted data due to invalid formatting.", err)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
event := &core.CollectionCreateEvent{
|
|
|
|
HttpContext: c,
|
|
|
|
Collection: collection,
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:42:06 +02:00
|
|
|
// 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)
|
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2022-07-12 12:42:06 +02:00
|
|
|
return e.HttpContext.JSON(http.StatusOK, e.Collection)
|
|
|
|
})
|
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
2022-07-12 12:42:06 +02:00
|
|
|
if submitErr == nil {
|
2022-07-06 23:19:05 +02:00
|
|
|
api.app.OnCollectionAfterCreateRequest().Trigger(event)
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:42:06 +02:00
|
|
|
return submitErr
|
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 {
|
|
|
|
return rest.NewNotFoundError("", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
form := forms.NewCollectionUpsert(api.app, collection)
|
|
|
|
|
2022-07-12 12:42:06 +02:00
|
|
|
// load request
|
2022-07-06 23:19:05 +02:00
|
|
|
if err := c.Bind(form); err != nil {
|
2022-07-12 12:42:06 +02:00
|
|
|
return rest.NewBadRequestError("Failed to load the submitted data due to invalid formatting.", err)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
event := &core.CollectionUpdateEvent{
|
|
|
|
HttpContext: c,
|
|
|
|
Collection: collection,
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:42:06 +02:00
|
|
|
// 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)
|
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2022-07-12 12:42:06 +02:00
|
|
|
return e.HttpContext.JSON(http.StatusOK, e.Collection)
|
|
|
|
})
|
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
})
|
|
|
|
|
2022-07-12 12:42:06 +02:00
|
|
|
if submitErr == nil {
|
2022-07-06 23:19:05 +02:00
|
|
|
api.app.OnCollectionAfterUpdateRequest().Trigger(event)
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:42:06 +02:00
|
|
|
return submitErr
|
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 {
|
|
|
|
return rest.NewNotFoundError("", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
event := &core.CollectionDeleteEvent{
|
|
|
|
HttpContext: c,
|
|
|
|
Collection: collection,
|
|
|
|
}
|
|
|
|
|
|
|
|
handlerErr := api.app.OnCollectionBeforeDeleteRequest().Trigger(event, func(e *core.CollectionDeleteEvent) error {
|
|
|
|
if err := api.app.Dao().DeleteCollection(e.Collection); err != nil {
|
|
|
|
return rest.NewBadRequestError("Failed to delete collection. Make sure that the collection is not referenced by other collections.", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.HttpContext.NoContent(http.StatusNoContent)
|
|
|
|
})
|
|
|
|
|
|
|
|
if handlerErr == nil {
|
|
|
|
api.app.OnCollectionAfterDeleteRequest().Trigger(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return handlerErr
|
|
|
|
}
|
2022-08-05 05:00:38 +02:00
|
|
|
|
2022-08-06 07:03:34 +02:00
|
|
|
// @todo add event
|
2022-08-05 05:00:38 +02:00
|
|
|
func (api *collectionApi) bulkImport(c echo.Context) error {
|
|
|
|
form := forms.NewCollectionsImport(api.app)
|
|
|
|
|
|
|
|
// load request
|
|
|
|
if err := c.Bind(form); err != nil {
|
|
|
|
return rest.NewBadRequestError("Failed to load the submitted data due to invalid formatting.", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
submitErr := form.Submit()
|
|
|
|
if submitErr != nil {
|
|
|
|
return rest.NewBadRequestError("Failed to import the submitted collections.", submitErr)
|
|
|
|
}
|
|
|
|
|
2022-08-06 07:03:34 +02:00
|
|
|
return c.NoContent(http.StatusNoContent)
|
2022-08-05 05:00:38 +02:00
|
|
|
}
|