Skip to content

Commit

Permalink
Merge pull request #604 from loco-rs/generator-html-delete-entity
Browse files Browse the repository at this point in the history
Generator html delete entity
  • Loading branch information
kaplanelad authored Jun 8, 2024
2 parents 355675f + 5322b51 commit 6d7bec3
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs-site/content/docs/getting-started/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
70 changes: 70 additions & 0 deletions docs-site/content/docs/getting-started/scaffold.md
Original file line number Diff line number Diff line change
@@ -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:
<!-- <snip id="scaffold-help-command" inject_from="yaml" action="exec" template="sh"> -->
```sh
Generates a CRUD scaffold, model and controller

Usage: blo-cli generate scaffold [OPTIONS] <NAME> [FIELDS]...

Arguments:
<NAME> Name of the thing to generate
[FIELDS]... Model fields, eg. title:string hits:int

Options:
-k, --kind <KIND> The kind of scaffold to generate [default: api] [possible values: api, html, htmx]
-e, --environment <ENVIRONMENT> Specify the environment [default: development]
-h, --help Print help
-V, --version Print version

```
<!-- </snip> -->

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:
<!-- <snip id="scaffold-post-command" inject_from="yaml" template="sh"> -->
```sh
cargo loco generate scaffold posts name:string title:string content:text
```
<!-- </snip> -->

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. |
7 changes: 7 additions & 0 deletions snipdoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 9 additions & 2 deletions src/gen/templates/scaffold/html/controller.t
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub async fn list(
#[debug_handler]
pub async fn new(
ViewEngine(v): ViewEngine<TeraView>,
State(ctx): State<AppContext>,
State(_ctx): State<AppContext>,
) -> Result<Response> {
views::{{file_name}}::create(&v)
}
Expand Down Expand Up @@ -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<i32>, State(ctx): State<AppContext>) -> Result<Response> {
load_item(&ctx, id).await?.delete(&ctx.db).await?;
format::empty()
}

pub fn routes() -> Routes {
Routes::new()
.prefix("{{file_name | plural}}")
Expand All @@ -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))
}
20 changes: 18 additions & 2 deletions src/gen/templates/scaffold/html/view_edit.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ message: "{{file_name}} edit view was added successfully."

<head>
<script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio,line-clamp"></script>
<script>
function confirmDelete(event) {
event.preventDefault();
if (confirm("Are you sure you want to delete this item?")) {
var xhr = new XMLHttpRequest();
xhr.open("DELETE", "/movies/{% raw %}{{ item.id }}{% endraw %}", true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
window.location.href = "/movies";
}
};
xhr.send();
}
}
</script>
</head>

<body class="prose p-10">
Expand Down Expand Up @@ -48,11 +63,12 @@ message: "{{file_name}} edit view was added successfully."
</div>
{% endfor -%}
<div>
<div>
<div class="mt-5">
<button class=" text-xs py-3 px-6 rounded-lg bg-gray-900 text-white" type="submit">Submit</button>
<button class="text-xs py-3 px-6 rounded-lg bg-red-600 text-white"
onclick="confirmDelete(event)">Delete</button>
</div>
</form>
<br />
<a href="/{{name | plural}}">Back to {{name | plural}}</a>
</div>
</body>
Expand Down

0 comments on commit 6d7bec3

Please sign in to comment.