A component (models.Component, component.graphql) is a named HTML box. "+ New box" now lists components and places a copy, as it did with the hard-coded starters; the canvas's new Components button (ComponentPanel) can also place one linked. A linked box has Box.componentId, its row's html is nil, and the resolvers fill in the component's html before answering (fillLinked), so saving the html in the box editor saves the component and every linked box on every page changes. Only the html is shared: title, geometry, frame and visibility stay each box's own. The box editor gains "Save as component" (makes one from an html box and links it) and, on a linked box, a banner with Detach (gives the box its own copy). updateBox refuses html on a linked box. Deleting a component detaches its boxes first, so no page changes. Layouts store the link and the html; loading relinks only if the component still exists. The built-in starters in templates.js are no longer listed directly: "Import built-ins" in the panel creates them as components (skipping names already taken), so they can be edited and deleted like the rest. The html helpers there are unchanged. Everything is @admin. Co-Authored-By: Claude Opus 5.5 <[email protected]>
184 lines
5.6 KiB
Go
184 lines
5.6 KiB
Go
package graph
|
|
|
|
// This file will be automatically regenerated based on the schema, any resolver
|
|
// implementations
|
|
// will be copied through when generating and any unknown code will be moved to the end.
|
|
// Code generated by github.com/99designs/gqlgen version v0.17.95
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"adam-french.co.uk/backend/graph/model"
|
|
"adam-french.co.uk/backend/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Uses is the resolver for the uses field.
|
|
func (r *componentResolver) Uses(ctx context.Context, obj *models.Component) (int, error) {
|
|
// One count per component, which is fine for an admin-only list of a
|
|
// handful; soft-deleted boxes are excluded by GORM's default scope.
|
|
var count int64
|
|
if err := r.Store.DB.Model(&models.Box{}).Where("component_id = ?", obj.ID).Count(&count).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
return int(count), nil
|
|
}
|
|
|
|
// CreateComponent is the resolver for the createComponent field.
|
|
func (r *mutationResolver) CreateComponent(ctx context.Context, input model.CreateComponentInput) (*models.Component, error) {
|
|
// Framed defaults to true and the size to 300x200, as for a new box.
|
|
name, err := usableComponentName(r.Store.DB, input.Name, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c := models.Component{Name: name, Title: input.Title, HTML: input.HTML, W: 300, H: 200, Framed: true}
|
|
setInt(&c.W, input.W)
|
|
setInt(&c.H, input.H)
|
|
if input.Framed != nil {
|
|
c.Framed = *input.Framed
|
|
}
|
|
if input.AdminOnly != nil {
|
|
c.AdminOnly = *input.AdminOnly
|
|
}
|
|
c.W = max(c.W, minBoxSize)
|
|
c.H = max(c.H, minBoxSize)
|
|
if err := r.Store.DB.Create(&c).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &c, nil
|
|
}
|
|
|
|
// UpdateComponent is the resolver for the updateComponent field.
|
|
func (r *mutationResolver) UpdateComponent(ctx context.Context, id int, input model.UpdateComponentInput) (*models.Component, error) {
|
|
// Partial, like updateBox. Nothing to do for the linked boxes: they
|
|
// read the html from here every time they are fetched.
|
|
var c models.Component
|
|
if err := r.Store.DB.First(&c, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if input.Name != nil {
|
|
name, err := usableComponentName(r.Store.DB, *input.Name, c.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c.Name = name
|
|
}
|
|
if input.Title != nil {
|
|
c.Title = input.Title
|
|
}
|
|
if input.HTML != nil {
|
|
c.HTML = *input.HTML
|
|
}
|
|
setInt(&c.W, input.W)
|
|
setInt(&c.H, input.H)
|
|
if input.Framed != nil {
|
|
c.Framed = *input.Framed
|
|
}
|
|
if input.AdminOnly != nil {
|
|
c.AdminOnly = *input.AdminOnly
|
|
}
|
|
c.W = max(c.W, minBoxSize)
|
|
c.H = max(c.H, minBoxSize)
|
|
if err := r.Store.DB.Save(&c).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &c, nil
|
|
}
|
|
|
|
// DeleteComponent is the resolver for the deleteComponent field.
|
|
func (r *mutationResolver) DeleteComponent(ctx context.Context, id int) (*models.Component, error) {
|
|
// The linked boxes keep showing what they showed, as copies, so
|
|
// deleting a component never changes a page. One transaction, so a
|
|
// failure can't leave boxes linked to a deleted component.
|
|
var c models.Component
|
|
if err := r.Store.DB.First(&c, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
err := r.Store.DB.Transaction(func(tx *gorm.DB) error {
|
|
if err := detachFrom(tx, &c); err != nil {
|
|
return err
|
|
}
|
|
return tx.Delete(&c).Error
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &c, nil
|
|
}
|
|
|
|
// SaveBoxAsComponent is the resolver for the saveBoxAsComponent field.
|
|
func (r *mutationResolver) SaveBoxAsComponent(ctx context.Context, id int, name string) (*models.Box, error) {
|
|
// The box becomes the component's first linked instance, so carrying
|
|
// on editing it edits the component. A box that is already linked
|
|
// would make a component of a component's html; detach it first.
|
|
var box models.Box
|
|
if err := r.Store.DB.First(&box, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if box.Kind != "html" || box.HTML == nil {
|
|
return nil, errors.New("only an html box can be saved as a component")
|
|
}
|
|
if box.ComponentID != nil {
|
|
return nil, errors.New("that box is already linked to a component")
|
|
}
|
|
name, err := usableComponentName(r.Store.DB, name, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c := models.Component{
|
|
Name: name, Title: box.Title, HTML: *box.HTML,
|
|
W: box.W, H: box.H, Framed: box.Framed, AdminOnly: box.AdminOnly,
|
|
}
|
|
err = r.Store.DB.Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Create(&c).Error; err != nil {
|
|
return err
|
|
}
|
|
box.ComponentID = &c.ID
|
|
box.HTML = nil
|
|
return tx.Save(&box).Error
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := fillLinkedOne(r.Store.DB, &box); err != nil {
|
|
return nil, err
|
|
}
|
|
return &box, nil
|
|
}
|
|
|
|
// DetachBox is the resolver for the detachBox field.
|
|
func (r *mutationResolver) DetachBox(ctx context.Context, id int) (*models.Box, error) {
|
|
// The html is filled in from the component and then saved, which is
|
|
// exactly the copy fillLinked otherwise never lets reach the row.
|
|
var box models.Box
|
|
if err := r.Store.DB.First(&box, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if box.ComponentID == nil {
|
|
return nil, errors.New("that box isn't linked to a component")
|
|
}
|
|
if err := fillLinkedOne(r.Store.DB, &box); err != nil {
|
|
return nil, err
|
|
}
|
|
box.ComponentID = nil
|
|
if err := r.Store.DB.Save(&box).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &box, nil
|
|
}
|
|
|
|
// Components is the resolver for the components field.
|
|
func (r *queryResolver) Components(ctx context.Context) ([]*models.Component, error) {
|
|
var components []models.Component
|
|
if err := r.Store.DB.Order("name ASC").Find(&components).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return ptrs(components), nil
|
|
}
|
|
|
|
// Component returns ComponentResolver implementation.
|
|
func (r *Resolver) Component() ComponentResolver { return &componentResolver{r} }
|
|
|
|
type componentResolver struct{ *Resolver }
|