Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiny doc improvement: prefer let mut item: ActiveModel = Default::default(); #1042

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions docs-site/content/docs/getting-started/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,7 @@ pub async fn list(State(ctx): State<AppContext>) -> Result<Response> {
}

pub async fn add(State(ctx): State<AppContext>, Json(params): Json<Params>) -> Result<Response> {
let mut item = ActiveModel {
..Default::default()
};
let mut item: ActiveModel = Default::default();
params.update(&mut item);
let item = item.insert(&ctx.db).await?;
format::json(item)
Expand Down Expand Up @@ -586,7 +584,7 @@ pub fn routes() -> Routes {
.add("/", post(add))
.add("/:id", get(get_one))
.add("/:id", delete(remove))
.add("/:id", post(update))
.add("/:id", patch(update))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scaffold with --api option also uses patch for update.

}
```

Expand Down Expand Up @@ -678,7 +676,7 @@ pub fn routes() -> Routes {
// .add("/", get(list))
// .add("/:id", get(get_one))
// .add("/:id", delete(remove))
// .add("/:id", post(update))
// .add("/:id", patch(update))
}
```

Expand Down Expand Up @@ -855,9 +853,7 @@ Go back to `src/controllers/comments.rs` and take a look at the `add` function:

```rust
pub async fn add(State(ctx): State<AppContext>, Json(params): Json<Params>) -> Result<Response> {
let mut item = ActiveModel {
..Default::default()
};
let mut item: ActiveModel = Default::default();
params.update(&mut item);
let item = item.insert(&ctx.db).await?;
format::json(item)
Expand All @@ -872,16 +868,14 @@ async fn add(
State(ctx): State<AppContext>,
Json(params): Json<Params>,
) -> Result<Response> {
// we only want to make sure it exists
let _current_user = crate::models::users::Model::find_by_pid(&ctx.db, &auth.claims.pid).await?;

// next, update
// homework/bonus: make a comment _actually_ belong to user (user_id)
let mut item = ActiveModel {
..Default::default()
};
params.update(&mut item);
let item = item.insert(&ctx.db).await?;
format::json(item)
// we only want to make sure it exists
let _current_user = crate::models::users::Model::find_by_pid(&ctx.db, &auth.claims.pid).await?;

// next, update
// homework/bonus: make a comment _actually_ belong to user (user_id)
let mut item: ActiveModel = Default::default();
params.update(&mut item);
let item = item.insert(&ctx.db).await?;
format::json(item)
}
```
Loading