From 41270e98db97ab5acb06e1dcdeda36072c9b63e7 Mon Sep 17 00:00:00 2001 From: Serhii Potapov Date: Thu, 5 Dec 2024 12:35:35 +0100 Subject: [PATCH] Tiny doc improvement: prefer let mut item: ActiveModel = Default::default(); (#1042) * Prefer let mut item: ActiveModel = Default::default(); --- .../content/docs/getting-started/guide.md | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/docs-site/content/docs/getting-started/guide.md b/docs-site/content/docs/getting-started/guide.md index 4f2f11465..da7dc4443 100644 --- a/docs-site/content/docs/getting-started/guide.md +++ b/docs-site/content/docs/getting-started/guide.md @@ -550,9 +550,7 @@ pub async fn list(State(ctx): State) -> Result { } pub async fn add(State(ctx): State, Json(params): Json) -> Result { - 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) @@ -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)) } ``` @@ -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)) } ``` @@ -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, Json(params): Json) -> Result { - 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) @@ -872,16 +868,14 @@ async fn add( State(ctx): State, Json(params): Json, ) -> Result { - // 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) } ```