- cabana: config_reorder.yaml (title, nameFrom, modelClass, parentColumn,
sortColumn, maxDepth, partition) with fail-loud boot checks and the
built-in reorder toolbar button; list schema carries reorder
- GET/POST {controller}/reorder: scoped set (ListExtendQuery plus the
partition) locked FOR UPDATE, 409 reorder_stale on any set difference,
422 for duplicates, foreign parents, cycles and maxDepth, both columns
written without model hooks
- pact.AdminSingleton: GET {controller}/singleton (first or create, form
scope checked), form schema singleton, list/create/bulk/delete 404,
no config_list.yaml needed
- swag, inventories, token classes, CSRF walk, conformance cases, backend
lang reorder.stale and load_failed, reorder and singleton smoke tests, docs
85 lines
2.3 KiB
Go
85 lines
2.3 KiB
Go
package cabana
|
|
|
|
import (
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/pact"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// AdminSingletonResult is the data of the singleton route: the id of the one
|
|
// record a pact.AdminSingleton controller edits.
|
|
type AdminSingletonResult struct {
|
|
ID uint `json:"id"`
|
|
}
|
|
|
|
// singletonRefuses answers 404 for a list, create, bulk or delete request on
|
|
// a singleton controller (D-11) and reports whether it did.
|
|
func singletonRefuses(w http.ResponseWriter, r *http.Request, cc *CompiledController) bool {
|
|
if cc == nil || !cc.singleton {
|
|
return false
|
|
}
|
|
writeNotFound(w, r)
|
|
return true
|
|
}
|
|
|
|
// singletonRoute serves GET {vendor}/{plugin}/{controller}/singleton: in one
|
|
// transaction it asks the controller's SingletonRecord for the record
|
|
// (created when absent) and answers its id, after checking that the form
|
|
// scope (FormExtendQuery) can load it. It is guarded like the form schema; a
|
|
// controller that is not a singleton answers 404.
|
|
func (s *service) singletonRoute(w http.ResponseWriter, r *http.Request) {
|
|
s.protect(w, r, func(cc *CompiledController) {
|
|
provider, ok := cc.Controller.(pact.AdminSingleton)
|
|
if !cc.singleton || !ok || provider == nil {
|
|
writeNotFound(w, r)
|
|
return
|
|
}
|
|
db, err := s.db()
|
|
if err != nil {
|
|
WriteError(w, http.StatusInternalServerError, "error", msgServerError)
|
|
return
|
|
}
|
|
ctx := r.Context()
|
|
var result AdminSingletonResult
|
|
err = db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
txCtx := withTx(ctx, tx)
|
|
record, err := provider.SingletonRecord(txCtx, tx)
|
|
if err != nil {
|
|
return lifecycleFailure(cc, err)
|
|
}
|
|
var id uint
|
|
if record != nil {
|
|
id = pkUint(record)
|
|
}
|
|
if id == 0 {
|
|
return lifecycleFailure(cc, errors.New("cabana: SingletonRecord returned no record with an id"))
|
|
}
|
|
target, err := newWritableModel(cc)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pk, err := coercePK(target, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := loadRecord(txCtx, tx, cc, target, pk); err != nil {
|
|
return err
|
|
}
|
|
result.ID = id
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
var missing recordNotFound
|
|
if !errors.As(err, &missing) {
|
|
slog.Error("cabana: singleton record failed", "controller", controllerID(cc), "error", err)
|
|
}
|
|
writeCRUDError(w, err)
|
|
return
|
|
}
|
|
WriteData(w, http.StatusOK, result, nil)
|
|
})
|
|
}
|