Drop narration, history and restated-code comments across the repo, keep short ones for security boundaries, upstream quirks and ordering constraints, and record the convention in CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
159 lines
4.2 KiB
Go
159 lines
4.2 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.98
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"adam-french.co.uk/backend/graph/model"
|
|
"adam-french.co.uk/backend/models"
|
|
)
|
|
|
|
// CreateBox is the resolver for the createBox field.
|
|
func (r *mutationResolver) CreateBox(ctx context.Context, input model.CreateBoxInput) (*models.Box, error) {
|
|
// Framed defaults here, not in the gorm tag; see models.Box.Framed.
|
|
box := models.Box{
|
|
Page: input.Page,
|
|
Kind: input.Kind,
|
|
Widget: input.Widget,
|
|
HTML: input.HTML,
|
|
Title: input.Title,
|
|
W: 300,
|
|
H: 200,
|
|
Framed: true,
|
|
}
|
|
setInt(&box.X, input.X)
|
|
setInt(&box.Y, input.Y)
|
|
setInt(&box.W, input.W)
|
|
setInt(&box.H, input.H)
|
|
setInt(&box.Z, input.Z)
|
|
if input.Framed != nil {
|
|
box.Framed = *input.Framed
|
|
}
|
|
if input.Shadow != nil {
|
|
box.Shadow = *input.Shadow
|
|
}
|
|
if input.HideOnMobile != nil {
|
|
box.HideOnMobile = *input.HideOnMobile
|
|
}
|
|
if input.AdminOnly != nil {
|
|
box.AdminOnly = *input.AdminOnly
|
|
}
|
|
// A linked box's own html would only go stale.
|
|
if input.ComponentID != nil {
|
|
id := uint(*input.ComponentID)
|
|
exists, err := componentExists(r.Store.DB, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !exists {
|
|
return nil, errors.New("there is no such component")
|
|
}
|
|
box.Kind = "html"
|
|
box.Widget = nil
|
|
box.HTML = nil
|
|
box.ComponentID = &id
|
|
}
|
|
if err := validateBox(&box); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.Store.DB.Create(&box).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := fillLinkedOne(r.Store.DB, &box); err != nil {
|
|
return nil, err
|
|
}
|
|
return &box, nil
|
|
}
|
|
|
|
// UpdateBox is the resolver for the updateBox field.
|
|
func (r *mutationResolver) UpdateBox(ctx context.Context, id int, input model.UpdateBoxInput) (*models.Box, error) {
|
|
// Partial: the canvas sends only geometry after a drag and only html after
|
|
// an edit, so neither overwrites the other.
|
|
var box models.Box
|
|
if err := r.Store.DB.First(&box, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if input.Widget != nil {
|
|
box.Widget = input.Widget
|
|
}
|
|
if input.HTML != nil {
|
|
// Edit linked html via updateComponent, or it forks a copy.
|
|
if box.ComponentID != nil {
|
|
return nil, errors.New("that box is linked to a component: edit the component, or detach the box first")
|
|
}
|
|
box.HTML = input.HTML
|
|
}
|
|
if input.Title != nil {
|
|
box.Title = input.Title
|
|
}
|
|
setInt(&box.X, input.X)
|
|
setInt(&box.Y, input.Y)
|
|
setInt(&box.W, input.W)
|
|
setInt(&box.H, input.H)
|
|
setInt(&box.Z, input.Z)
|
|
if input.Framed != nil {
|
|
box.Framed = *input.Framed
|
|
}
|
|
if input.Shadow != nil {
|
|
box.Shadow = *input.Shadow
|
|
}
|
|
if input.HideOnMobile != nil {
|
|
box.HideOnMobile = *input.HideOnMobile
|
|
}
|
|
if input.AdminOnly != nil {
|
|
box.AdminOnly = *input.AdminOnly
|
|
}
|
|
if err := validateBox(&box); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.Store.DB.Save(&box).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := fillLinkedOne(r.Store.DB, &box); err != nil {
|
|
return nil, err
|
|
}
|
|
return &box, nil
|
|
}
|
|
|
|
// DeleteBox is the resolver for the deleteBox field.
|
|
func (r *mutationResolver) DeleteBox(ctx context.Context, id int) (*models.Box, error) {
|
|
var box models.Box
|
|
if err := r.Store.DB.First(&box, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.Store.DB.Delete(&box).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &box, nil
|
|
}
|
|
|
|
// Boxes is the resolver for the boxes field.
|
|
func (r *queryResolver) Boxes(ctx context.Context, page string) ([]*models.Box, error) {
|
|
// Ordered by z so DOM and tab order match stacking. Admin-only content is
|
|
// filtered server-side so it never reaches anyone else.
|
|
hidden, err := pageHiddenFrom(ctx, r.Store.DB, page)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if hidden {
|
|
return []*models.Box{}, nil
|
|
}
|
|
query := r.Store.DB.Where("page = ?", page)
|
|
if !IsAdminFromCtx(ctx) {
|
|
query = query.Where("admin_only = ?", false)
|
|
}
|
|
var boxes []models.Box
|
|
if err := query.Order("z ASC, id ASC").Find(&boxes).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := fillLinked(r.Store.DB, boxes); err != nil {
|
|
return nil, err
|
|
}
|
|
return ptrs(boxes), nil
|
|
}
|