Each canvas page key (the landing page, /stp, shrines and other admin-made pages) can have its own background: a colour, an image and how the image fills the window. Admins set it from a Background button beside Edit layout, with live preview and image upload or drop; DefaultLayout paints it behind the whole window and falls back to the halftone. Backed by a new Background GraphQL type keyed by page key, validated server-side, hidden with admin-only pages, and moved or deleted along with its page. Co-Authored-By: Claude Opus 5.5 <[email protected]>
64 lines
2.0 KiB
Go
64 lines
2.0 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"
|
|
)
|
|
|
|
// SetBackground is the resolver for the setBackground field.
|
|
func (r *mutationResolver) SetBackground(ctx context.Context, page string, input model.BackgroundInput) (*models.Background, error) {
|
|
// One row per page: replaced in place if there is one, so its id stays
|
|
// stable for the store on the front end.
|
|
bg, err := backgroundFrom(page, input)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var existing models.Background
|
|
err = r.Store.DB.Where("page = ?", page).First(&existing).Error
|
|
switch {
|
|
case err == nil:
|
|
bg.ID, bg.CreatedAt = existing.ID, existing.CreatedAt
|
|
err = r.Store.DB.Save(&bg).Error
|
|
case errors.Is(err, gorm.ErrRecordNotFound):
|
|
err = r.Store.DB.Create(&bg).Error
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &bg, nil
|
|
}
|
|
|
|
// ClearBackground is the resolver for the clearBackground field.
|
|
func (r *mutationResolver) ClearBackground(ctx context.Context, page string) (bool, error) {
|
|
res := r.Store.DB.Where("page = ?", page).Delete(&models.Background{})
|
|
return res.RowsAffected > 0, res.Error
|
|
}
|
|
|
|
// Background is the resolver for the background field.
|
|
func (r *queryResolver) Background(ctx context.Context, page string) (*models.Background, error) {
|
|
// Null, not an error, when the page has none: that is the default look.
|
|
// Hidden along with an adminOnly page, like its boxes.
|
|
hidden, err := pageHiddenFrom(ctx, r.Store.DB, page)
|
|
if err != nil || hidden {
|
|
return nil, err
|
|
}
|
|
var bg models.Background
|
|
err = r.Store.DB.Where("page = ?", page).First(&bg).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &bg, nil
|
|
}
|