// 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 }) }, } }