1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Improve code

This commit is contained in:
Pierre HUBERT 2020-07-07 19:19:18 +02:00
parent c82e9d6e69
commit ea1ed285f8
2 changed files with 9 additions and 8 deletions

View File

@ -91,7 +91,7 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult {
}; };
// Start to create post // Start to create post
let post = Post { let mut post = Post {
id: 0, id: 0,
user_id: r.user_id()?, user_id: r.user_id()?,
time_create: time(), time_create: time(),
@ -102,19 +102,22 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult {
}; };
// Handle different post types // Handle different post types
match r.post_string("kind")?.as_str() { post.kind = match r.post_string("kind")?.as_str() {
// Text posts // Text posts
"text" => { "text" => {
if !check_string_before_insert(post.content.as_ref().unwrap_or(&String::new())) { if !check_string_before_insert(post.content.as_ref().unwrap_or(&String::new())) {
r.forbidden("Specified post content is invalid!".to_string())?; 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 // Create the post
let post_id = posts_helper::create(&post)?; let post_id = posts_helper::create(&post)?;

View File

@ -75,10 +75,8 @@ pub fn create(p: &Post) -> ResultBoxError<u64> {
.add_opt_str("texte", p.content.as_ref()); .add_opt_str("texte", p.content.as_ref());
// Execute insertion // Execute insertion
let post_id = match insert_query.insert()? { let post_id = insert_query.insert()?
None => Err(ExecError::new("Insert post query did not return a result!")), .ok_or(ExecError::new("Insert post query did not return a result!"))?;
Some(id) => Ok(id),
}?;
Ok(post_id) Ok(post_id)
} }