mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-22 13:29:21 +00:00
Improve code
This commit is contained in:
parent
c82e9d6e69
commit
ea1ed285f8
@ -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)?;
|
||||
|
@ -75,10 +75,8 @@ pub fn create(p: &Post) -> ResultBoxError<u64> {
|
||||
.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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user