1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-02-15 17:14:03 +02:00
pocketbase/apis/collection.go

191 lines
6.0 KiB
Go
Raw Normal View History

2022-07-07 00:19:05 +03:00
package apis
import (
2024-09-29 19:23:19 +03:00
"errors"
2022-07-07 00:19:05 +03:00
"net/http"
2024-09-29 19:23:19 +03:00
"strings"
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
validation "github.com/go-ozzo/ozzo-validation/v4"
2022-07-07 00:19:05 +03:00
"github.com/pocketbase/pocketbase/core"
2024-09-29 19:23:19 +03:00
"github.com/pocketbase/pocketbase/tools/router"
2022-07-07 00:19:05 +03:00
"github.com/pocketbase/pocketbase/tools/search"
)
2022-10-30 10:28:14 +02:00
// bindCollectionApi registers the collection api endpoints and the corresponding handlers.
2024-09-29 19:23:19 +03:00
func bindCollectionApi(app core.App, rg *router.RouterGroup[*core.RequestEvent]) {
subGroup := rg.Group("/collections").Bind(RequireSuperuserAuth())
subGroup.GET("", collectionsList)
subGroup.POST("", collectionCreate)
subGroup.GET("/{collection}", collectionView)
subGroup.PATCH("/{collection}", collectionUpdate)
subGroup.DELETE("/{collection}", collectionDelete)
subGroup.DELETE("/{collection}/truncate", collectionTruncate)
subGroup.PUT("/import", collectionsImport)
subGroup.GET("/meta/scaffolds", collectionScaffolds)
2022-07-07 00:19:05 +03:00
}
2024-09-29 19:23:19 +03:00
func collectionsList(e *core.RequestEvent) error {
2022-07-07 00:19:05 +03:00
fieldResolver := search.NewSimpleFieldResolver(
2022-10-30 10:28:14 +02:00
"id", "created", "updated", "name", "system", "type",
2022-07-07 00:19:05 +03:00
)
2024-09-29 19:23:19 +03:00
collections := []*core.Collection{}
2022-07-07 00:19:05 +03:00
result, err := search.NewProvider(fieldResolver).
2024-09-29 19:23:19 +03:00
Query(e.App.CollectionQuery()).
ParseAndExec(e.Request.URL.Query().Encode(), &collections)
2022-07-07 00:19:05 +03:00
if err != nil {
2024-09-29 19:23:19 +03:00
return e.BadRequestError("", err)
2022-07-07 00:19:05 +03:00
}
2024-09-29 19:23:19 +03:00
event := new(core.CollectionsListRequestEvent)
event.RequestEvent = e
event.Collections = collections
event.Result = result
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
return event.App.OnCollectionsListRequest().Trigger(event, func(e *core.CollectionsListRequestEvent) error {
return e.JSON(http.StatusOK, e.Result)
2022-07-07 00:19:05 +03:00
})
}
2024-09-29 19:23:19 +03:00
func collectionView(e *core.RequestEvent) error {
collection, err := e.App.FindCachedCollectionByNameOrId(e.Request.PathValue("collection"))
2022-07-07 00:19:05 +03:00
if err != nil || collection == nil {
2024-09-29 19:23:19 +03:00
return e.NotFoundError("", err)
2022-07-07 00:19:05 +03:00
}
2024-09-29 19:23:19 +03:00
event := new(core.CollectionRequestEvent)
event.RequestEvent = e
event.Collection = collection
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
return e.App.OnCollectionViewRequest().Trigger(event, func(e *core.CollectionRequestEvent) error {
return e.JSON(http.StatusOK, e.Collection)
2022-07-07 00:19:05 +03:00
})
}
2024-09-29 19:23:19 +03:00
func collectionCreate(e *core.RequestEvent) error {
// populate the minimal required factory collection data (if any)
factoryExtract := struct {
Type string `form:"type" json:"type"`
Name string `form:"name" json:"name"`
}{}
if err := e.BindBody(&factoryExtract); err != nil {
return e.BadRequestError("Failed to load the collection type data due to invalid formatting.", err)
}
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
// create scaffold
collection := core.NewCollection(factoryExtract.Type, factoryExtract.Name)
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
// merge the scaffold with the submitted request data
if err := e.BindBody(collection); err != nil {
return e.BadRequestError("Failed to load the submitted data due to invalid formatting.", err)
2022-07-07 00:19:05 +03:00
}
2024-09-29 19:23:19 +03:00
event := new(core.CollectionRequestEvent)
event.RequestEvent = e
event.Collection = collection
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
return e.App.OnCollectionCreateRequest().Trigger(event, func(e *core.CollectionRequestEvent) error {
if err := e.App.Save(e.Collection); err != nil {
// validation failure
var validationErrors validation.Errors
if errors.As(err, &validationErrors) {
return e.BadRequestError("Failed to create collection.", validationErrors)
}
2023-07-20 10:40:03 +03:00
2024-09-29 19:23:19 +03:00
// other generic db error
return e.BadRequestError("Failed to create collection. Raw error: \n"+err.Error(), nil)
}
2024-09-29 19:23:19 +03:00
return e.JSON(http.StatusOK, e.Collection)
2022-07-07 00:19:05 +03:00
})
}
2024-09-29 19:23:19 +03:00
func collectionUpdate(e *core.RequestEvent) error {
collection, err := e.App.FindCollectionByNameOrId(e.Request.PathValue("collection"))
2022-07-07 00:19:05 +03:00
if err != nil || collection == nil {
2024-09-29 19:23:19 +03:00
return e.NotFoundError("", err)
2022-07-07 00:19:05 +03:00
}
2024-09-29 19:23:19 +03:00
if err := e.BindBody(collection); err != nil {
return e.BadRequestError("Failed to load the submitted data due to invalid formatting.", err)
2022-07-07 00:19:05 +03:00
}
2024-09-29 19:23:19 +03:00
event := new(core.CollectionRequestEvent)
event.RequestEvent = e
event.Collection = collection
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
return event.App.OnCollectionUpdateRequest().Trigger(event, func(e *core.CollectionRequestEvent) error {
if err := e.App.Save(e.Collection); err != nil {
// validation failure
var validationErrors validation.Errors
if errors.As(err, &validationErrors) {
return e.BadRequestError("Failed to update collection.", validationErrors)
}
2023-07-20 10:40:03 +03:00
2024-09-29 19:23:19 +03:00
// other generic db error
return e.BadRequestError("Failed to update collection. Raw error: \n"+err.Error(), nil)
}
2024-09-29 19:23:19 +03:00
return e.JSON(http.StatusOK, e.Collection)
2022-07-07 00:19:05 +03:00
})
}
2024-09-29 19:23:19 +03:00
func collectionDelete(e *core.RequestEvent) error {
collection, err := e.App.FindCachedCollectionByNameOrId(e.Request.PathValue("collection"))
2022-07-07 00:19:05 +03:00
if err != nil || collection == nil {
2024-09-29 19:23:19 +03:00
return e.NotFoundError("", err)
2022-07-07 00:19:05 +03:00
}
2024-09-29 19:23:19 +03:00
event := new(core.CollectionRequestEvent)
event.RequestEvent = e
event.Collection = collection
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
return e.App.OnCollectionDeleteRequest().Trigger(event, func(e *core.CollectionRequestEvent) error {
if err := e.App.Delete(e.Collection); err != nil {
msg := "Failed to delete collection"
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
// check fo references
refs, _ := e.App.FindCollectionReferences(e.Collection, e.Collection.Id)
if len(refs) > 0 {
names := make([]string, 0, len(refs))
for ref := range refs {
names = append(names, ref.Name)
}
msg += " probably due to existing reference in " + strings.Join(names, ", ")
2023-07-20 10:40:03 +03:00
}
2024-09-29 19:23:19 +03:00
return e.BadRequestError(msg, err)
}
return e.NoContent(http.StatusNoContent)
2023-05-29 21:50:07 +03:00
})
2022-07-07 00:19:05 +03:00
}
2022-08-05 06:00:38 +03:00
2024-09-29 19:23:19 +03:00
func collectionTruncate(e *core.RequestEvent) error {
collection, err := e.App.FindCachedCollectionByNameOrId(e.Request.PathValue("collection"))
if err != nil || collection == nil {
return e.NotFoundError("", err)
2022-08-05 06:00:38 +03:00
}
if collection.IsView() {
return e.BadRequestError("View collections cannot be truncated since they don't store their own records.", nil)
}
2024-09-29 19:23:19 +03:00
err = e.App.TruncateCollection(collection)
if err != nil {
return e.BadRequestError("Failed to truncate collection (most likely due to required cascade delete record references).", err)
}
2022-08-07 20:58:21 +03:00
2024-09-29 19:23:19 +03:00
return e.NoContent(http.StatusNoContent)
}
2023-07-20 10:40:03 +03:00
2024-09-29 19:23:19 +03:00
func collectionScaffolds(e *core.RequestEvent) error {
return e.JSON(http.StatusOK, map[string]*core.Collection{
core.CollectionTypeBase: core.NewBaseCollection(""),
core.CollectionTypeAuth: core.NewAuthCollection(""),
core.CollectionTypeView: core.NewViewCollection(""),
2022-08-07 20:58:21 +03:00
})
2022-08-05 06:00:38 +03:00
}