From 26454d0209c5a37a9d47838893b74a8621b0d225 Mon Sep 17 00:00:00 2001 From: Serhii Potapov Date: Sun, 1 Dec 2024 10:42:18 +0100 Subject: [PATCH 1/2] Prefer let mut item: ActiveModel = Default::default(); --- .../content/docs/getting-started/guide.md | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/docs-site/content/docs/getting-started/guide.md b/docs-site/content/docs/getting-started/guide.md index 4f2f11465..ab5acdec1 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) @@ -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) } ``` From 6b04e38d14a396951d1a6216d77ad5610bc6af93 Mon Sep 17 00:00:00 2001 From: Serhii Potapov Date: Sun, 1 Dec 2024 10:56:08 +0100 Subject: [PATCH 2/2] Fix docs: Use patch method for update --- docs-site/content/docs/getting-started/guide.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs-site/content/docs/getting-started/guide.md b/docs-site/content/docs/getting-started/guide.md index ab5acdec1..da7dc4443 100644 --- a/docs-site/content/docs/getting-started/guide.md +++ b/docs-site/content/docs/getting-started/guide.md @@ -584,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)) } ``` @@ -676,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)) } ```