From ea1ed285f816ea90dec0acd88d900319da6a2250 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 7 Jul 2020 19:19:18 +0200 Subject: [PATCH] Improve code --- src/controllers/posts_controller.rs | 11 +++++++---- src/helpers/posts_helper.rs | 6 ++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index cb8c046..6a237cc 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -91,7 +91,7 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult { }; // Start to create post - let post = Post { + let mut post = Post { id: 0, user_id: r.user_id()?, time_create: time(), @@ -102,19 +102,22 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult { }; // Handle different post types - match r.post_string("kind")?.as_str() { + post.kind = match r.post_string("kind")?.as_str() { // Text posts "text" => { if !check_string_before_insert(post.content.as_ref().unwrap_or(&String::new())) { r.forbidden("Specified post content is invalid!".to_string())?; } + + PostKind::POST_KIND_TEXT } _ => { - return r.internal_error(ExecError::boxed_new("Unsupported kind of post!")); + r.internal_error(ExecError::boxed_new("Unsupported kind of post!"))?; + unreachable!(); } - } + }; // Create the post let post_id = posts_helper::create(&post)?; diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 5759a27..6743a09 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -75,10 +75,8 @@ pub fn create(p: &Post) -> ResultBoxError { .add_opt_str("texte", p.content.as_ref()); // Execute insertion - let post_id = match insert_query.insert()? { - None => Err(ExecError::new("Insert post query did not return a result!")), - Some(id) => Ok(id), - }?; + let post_id = insert_query.insert()? + .ok_or(ExecError::new("Insert post query did not return a result!"))?; Ok(post_id) }