From 7cb5079e1d8e5fbd3659ff0d6115ad6d0f1960b1 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Fri, 23 Sep 2022 20:45:39 +0200 Subject: Edit model --- src/models/model.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/models/model.rs') diff --git a/src/models/model.rs b/src/models/model.rs index 9d463b1..0a9ea0a 100644 --- a/src/models/model.rs +++ b/src/models/model.rs @@ -122,6 +122,35 @@ impl Model { Ok(rec) } + /// Edit a model + pub async fn edit(id: i32, model: Model) -> Result { + let pool = unsafe { get_client() }; + + model + .validate() + .map_err(|error| AppError::BadRequest(error.to_string()))?; + + let rec: Model = sqlx::query_as( + r#" + UPDATE models SET name = $1, description = $2, duration = $3, height = $4, weight = $5, printer = $6, material = $7, updated = $8 + WHERE id = $9 + RETURNING * + "#) + .bind(model.name) + .bind(model.description) + .bind(model.duration) + .bind(model.height) + .bind(model.weight) + .bind(model.printer) + .bind(model.material) + .bind(model.updated) + .bind(id) + .fetch_one(pool) + .await?; + + Ok(rec) + } + /// Returns the model with id = `model_id` pub async fn find_by_id(model_id: i32) -> Result { let pool = unsafe { get_client() }; -- cgit v1.2.3-71-g8e6c