- model, migration, command, job and admin-controller stubs start with //summer:make <kind>; registry.gen.go keeps its generated-code header - the registry scan accepts the directive and the older header - command functions may take one *backpack.App; generatedCommands takes a variadic app, so plugin.go files calling generatedCommands() still compile - scaffolded plugins keep the app from Boot and return generatedCommands(p.app) - make:command notes the nil app for an older plugin.go - docs, help text and the blog example follow
48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
//summer:make model
|
|
|
|
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/lagoon"
|
|
)
|
|
|
|
// Post is a blog post, the Go form of the WinterCMS Acme\Blog\Models\Post
|
|
// model.
|
|
type Post struct {
|
|
ID uint `gorm:"column:id;primaryKey"`
|
|
Title string `gorm:"column:title"`
|
|
Slug string `gorm:"column:slug"`
|
|
Body string `gorm:"column:body"`
|
|
PublishedAt *time.Time `gorm:"column:published_at"`
|
|
CreatedAt time.Time `gorm:"column:created_at"`
|
|
UpdatedAt time.Time `gorm:"column:updated_at"`
|
|
}
|
|
|
|
// TableName keeps the WinterCMS table name.
|
|
func (Post) TableName() string { return "acme_blog_posts" }
|
|
|
|
// Fillable is the Go form of $fillable: the only columns lagoon.Fill may
|
|
// set from a request.
|
|
func (Post) Fillable() []string { return []string{"title", "slug", "body"} }
|
|
|
|
// Rules is the Go form of $rules. The admin API checks them with
|
|
// lagoon.Validate on every save; unique ignores the post being updated.
|
|
func (Post) Rules() map[string]string {
|
|
return map[string]string{
|
|
"title": "required|max:255",
|
|
"slug": "required|max:255|unique:acme_blog_posts",
|
|
}
|
|
}
|
|
|
|
// NewPost is the Go form of Post::make($input): it copies only the fillable
|
|
// keys of input onto a new post and drops the rest, such as id.
|
|
func NewPost(input map[string]any) (*Post, error) {
|
|
post := &Post{}
|
|
if err := lagoon.Fill(post, post.Fillable(), input, true); err != nil {
|
|
return nil, err
|
|
}
|
|
return post, nil
|
|
}
|