feat(11.1-05): add the walkthrough's admin controller, publish command and published_at migration

- make:admin-controller, make:command and make:migration output, finished:
  the Posts controller serves models.Post behind acme.blog.access_posts,
  WinterCMS-style form and list YAML embedded through pact.AdminAssets,
  blog:publish sets published_at by slug with a bound parameter
- the posts route lists published posts only, newest first
- Docker tests migrate an ICU pl-PL database, roll back published_at, serve
  the route and run blog:publish with a published and an opened database
- the page gains the admin controller, console command and added-column
  sections
This commit is contained in:
Jakub Zych
2026-09-30 23:35:44 +02:00
parent 41a3190956
commit dd82b8a2ad
17 changed files with 1223 additions and 59 deletions

View File

@@ -0,0 +1,43 @@
// Code generated by summer make. DO NOT EDIT.
package console
import (
"context"
"fmt"
"git.golem15.com/golem15/summercms/docs/examples/blog/models"
"git.golem15.com/golem15/summercms/modules/bonfire"
"gorm.io/gorm"
)
// PublishCommand returns the blog:publish console command. withDB runs the
// command's work with the application's database; the plugin supplies it.
func PublishCommand(withDB func(ctx context.Context, fn func(*gorm.DB) error) error) bonfire.Command {
return bonfire.Command{
Name: "blog:publish",
Description: "Publish a blog post by its slug",
Args: []bonfire.Arg{{Name: "slug", Description: "Slug of the post to publish", Required: true}},
Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error {
slug, _ := in.Argument("slug")
if slug == "" {
return fmt.Errorf("blog:publish: a slug is required")
}
return withDB(ctx, func(db *gorm.DB) error {
// A bound parameter, never the slug spliced into SQL. Publishing
// twice keeps the first publication time.
res := db.WithContext(ctx).Model(&models.Post{}).
Where("slug = ?", slug).
Update("published_at", gorm.Expr("COALESCE(published_at, NOW())"))
if res.Error != nil {
return res.Error
}
if res.RowsAffected == 0 {
return fmt.Errorf("blog:publish: no post has the slug %q", slug)
}
out.Printf("published %s\n", slug)
return nil
})
},
}
}

View File

@@ -0,0 +1,44 @@
package console_test
import (
"bytes"
"context"
"errors"
"testing"
"git.golem15.com/golem15/summercms/docs/examples/blog/console"
"git.golem15.com/golem15/summercms/modules/bonfire"
"gorm.io/gorm"
)
func TestPublishCommandShape(t *testing.T) {
called := false
withDB := func(ctx context.Context, fn func(*gorm.DB) error) error {
called = true
return errors.New("no database in this test")
}
cmd := console.PublishCommand(withDB)
if cmd.Name != "blog:publish" {
t.Errorf("Name = %q, want blog:publish", cmd.Name)
}
if len(cmd.Args) != 1 || cmd.Args[0].Name != "slug" || !cmd.Args[0].Required {
t.Errorf("Args = %+v, want one required slug argument", cmd.Args)
}
if cmd.Description == "" {
t.Error("Description is empty")
}
cmds := []bonfire.Command{cmd}
var out bytes.Buffer
if err := bonfire.Call(context.Background(), cmds, "blog:publish", nil, &out); err == nil {
t.Error("blog:publish without a slug returned no error")
}
if called {
t.Error("blog:publish without a slug reached the database")
}
err := bonfire.Call(context.Background(), cmds, "blog:publish", []string{"hello-world"}, &out)
if err == nil || !called {
t.Errorf("blog:publish hello-world: err = %v, called = %v; want the withDB error", err, called)
}
}