diff --git a/docs-site/content/docs/getting-started/cli.md b/docs-site/content/docs/getting-started/cli.md index 155229060..321efd22c 100644 --- a/docs-site/content/docs/getting-started/cli.md +++ b/docs-site/content/docs/getting-started/cli.md @@ -3,7 +3,7 @@ title = "CLI" date = 2021-05-01T08:00:00+00:00 updated = 2021-05-01T08:00:00+00:00 draft = false -weight = 4 +weight = 5 sort_by = "weight" template = "docs/page.html" diff --git a/docs-site/content/docs/getting-started/scaffold.md b/docs-site/content/docs/getting-started/scaffold.md new file mode 100644 index 000000000..5796b627b --- /dev/null +++ b/docs-site/content/docs/getting-started/scaffold.md @@ -0,0 +1,70 @@ ++++ +title = "Scaffold" +date = 2024-06-06T08:00:00+00:00 +updated = 2021-06-06T08:00:00+00:00 +draft = false +weight = 4 +sort_by = "weight" +template = "docs/page.html" + +[extra] +toc = true +top = false +flair =[] ++++ + +Scaffolding is an efficient and speedy method for generating key components of an application. By utilizing scaffolding, you can create models, views, and controllers for a new resource all in one go. + + +See scaffold command: + +```sh +Generates a CRUD scaffold, model and controller + +Usage: blo-cli generate scaffold [OPTIONS] [FIELDS]... + +Arguments: + Name of the thing to generate + [FIELDS]... Model fields, eg. title:string hits:int + +Options: + -k, --kind The kind of scaffold to generate [default: api] [possible values: api, html, htmx] + -e, --environment Specify the environment [default: development] + -h, --help Print help + -V, --version Print version + +``` + + +You can begin by generating a scaffold for the Post resource, which will represent a single blog posting. To accomplish this, open your terminal and enter the following command: + +```sh +cargo loco generate scaffold posts name:string title:string content:text +``` + + +The scaffold generate command support API, HTML or HTMX by adding `--template` flag to scaffold command. + + +The scaffold generator will build several files in your application: + +| File | Purpose | +| ------------------------------------------ | ------------------------------------------------------------------------------------------------------- | +| `migration/src/lib.rs` | Include Post migration. | +| `migration/src/m20240606_102031_posts.rs` | Posts migration. | +| `src/app.rs` | Adding Posts to application router. | +| `src/controllers/mod.rs` | Include the Posts controller. | +| `src/controllers/posts.rs` | The Posts controller. | +| `tests/requests/posts.rs` | Functional testing. | +| `src/models/mod.rs` | Including Posts model. | +| `src/models/posts.rs` | Posts model, | +| `src/models/_entities/mod.rs` | Includes Posts Sea-orm entity model. | +| `src/models/_entities/posts.rs` | Sea-orm entity model. | +| `src/views/mod.rs` | Including Posts views. only for HTML and HTMX templates. | +| `src/views/posts.rs` | Posts template generator. only for HTML and HTMX templates. | +| `assets/views/posts/create.html` | Create post template. only for HTML and HTMX templates. | +| `assets/views/posts/edit.html` | Edit post template. only for HTML and HTMX templates. | +| `assets/views/posts/edit.html` | Edit post template. only for HTML and HTMX templates. | +| `assets/views/posts/list.html` | List post template. only for HTML and HTMX templates. | +| `assets/views/posts/show.html` | Show post template. only for HTML and HTMX templates. | + \ No newline at end of file diff --git a/snipdoc.yml b/snipdoc.yml index 0b5bdcebf..355914c62 100644 --- a/snipdoc.yml +++ b/snipdoc.yml @@ -86,3 +86,10 @@ snippets: skipped (exists): "dockerfile" added: ".dockerignore" path: ./snipdoc.yml + scaffold-help-command: + content: cd ./examples/demo && cargo loco generate scaffold --help + path: ./snipdoc.yml + scaffold-post-command: + content: cargo loco generate scaffold posts + name:string title:string content:text + path: ./snipdoc.yml diff --git a/src/gen/templates/scaffold/html/controller.t b/src/gen/templates/scaffold/html/controller.t index fe4a47f96..4be3fe4ec 100644 --- a/src/gen/templates/scaffold/html/controller.t +++ b/src/gen/templates/scaffold/html/controller.t @@ -60,7 +60,7 @@ pub async fn list( #[debug_handler] pub async fn new( ViewEngine(v): ViewEngine, - State(ctx): State, + State(_ctx): State, ) -> Result { views::{{file_name}}::create(&v) } @@ -108,9 +108,15 @@ pub async fn add( }; params.update(&mut item); item.insert(&ctx.db).await?; - // views::post::show(&v, &item) Ok(Redirect::to("{{file_name | plural}}")) } + +#[debug_handler] +pub async fn remove(Path(id): Path, State(ctx): State) -> Result { + load_item(&ctx, id).await?.delete(&ctx.db).await?; + format::empty() +} + pub fn routes() -> Routes { Routes::new() .prefix("{{file_name | plural}}") @@ -119,5 +125,6 @@ pub fn routes() -> Routes { .add("/:id", get(show)) .add("/:id/edit", get(edit)) .add("/:id", post(update)) + .add("/:id", delete(remove)) .add("/", post(add)) } diff --git a/src/gen/templates/scaffold/html/view_edit.t b/src/gen/templates/scaffold/html/view_edit.t index cb8974981..e45d03c4e 100644 --- a/src/gen/templates/scaffold/html/view_edit.t +++ b/src/gen/templates/scaffold/html/view_edit.t @@ -9,6 +9,21 @@ message: "{{file_name}} edit view was added successfully." + @@ -48,11 +63,12 @@ message: "{{file_name}} edit view was added successfully." {% endfor -%}
-
+
+
-
Back to {{name | plural}}