1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-01-30 14:03:00 +00:00

Can get a user id included in a POST request

This commit is contained in:
Pierre HUBERT 2020-05-29 18:15:24 +02:00
parent 8e2a4a8b71
commit cf2d9606d9
4 changed files with 31 additions and 1 deletions

View File

@ -48,5 +48,8 @@ pub fn get_multiple(request: &mut HttpRequestHandler) -> RequestResult {
/// Get advanced information about a user
pub fn get_advanced_info(request: &mut HttpRequestHandler) -> RequestResult {
let user_id = request.post_user_id("userID")?;
request.success("get user info")
}

View File

@ -12,7 +12,7 @@ use crate::data::api_client::APIClient;
use crate::data::config::conf;
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::user::UserID;
use crate::helpers::{account_helper, api_helper};
use crate::helpers::{account_helper, api_helper, user_helper};
/// Http request handler
///
@ -303,4 +303,19 @@ impl HttpRequestHandler {
Ok(list)
}
/// Get the ID of a user included in a POST request
pub fn post_user_id(&mut self, name: &str) -> ResultBoxError<UserID> {
let user_id = self.post_i64(name)?;
if user_id < 1 {
self.bad_request(format!("Invalid user specified in '{}'!", name))?;
}
if !user_helper::exists(user_id)? {
self.not_found(format!("User with ID {} not found!", user_id))?;
}
Ok(user_id)
}
}

View File

@ -107,6 +107,11 @@ impl QueryInfo {
-> Result<Vec<E>, Box<dyn Error>> {
query(self, process_function)
}
/// Execute count query
pub fn exec_count(self) -> ResultBoxError<usize> {
count(self)
}
}
/// Struct used to read the result of a request

View File

@ -52,4 +52,11 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError<User> {
account_image_visibility,
})
})
}
/// Check out whether a given id maps to a user or not
pub fn exists(id: UserID) -> ResultBoxError<bool> {
Ok(database::QueryInfo::new(USERS_TABLE)
.cond_i64("ID", id)
.exec_count()? > 0)
}