Commit fb75ec02 authored by Vasili Novikov's avatar Vasili Novikov
Browse files

Merge branch 'fix-get_item' into 'dev'

Fix get_item endpoint

See merge request !194
parents 59df391a e6e34b03
Pipeline #2190 passed with stages
in 14 minutes and 53 seconds
Showing with 21 additions and 25 deletions
+21 -25
......@@ -997,7 +997,6 @@ dependencies = [
"sha2",
"structopt",
"tokio",
"uuid",
"warp",
"zeroize",
]
......@@ -1759,16 +1758,6 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "uuid"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
dependencies = [
"getrandom",
"serde",
]
[[package]]
name = "vcpkg"
version = "0.2.11"
......
......@@ -29,7 +29,6 @@ serde_json = "1.0.64"
sha2 = "0.9.3"
structopt = { version = "0.3.21", features = ["color"] }
tokio = { version = "1.2.0", features = ["full"] }
uuid = { version = "0.8.2", features = ["serde", "v4"] }
warp = { version = "0.3.0", default-features = false, features = ["tls"] }
zeroize = "1.2.0"
......
......@@ -22,6 +22,7 @@ use crate::triggers;
use chrono::Utc;
use log::info;
use log::warn;
use rand::Rng;
use rusqlite::Transaction;
use serde_json::Map;
use serde_json::Value;
......@@ -138,8 +139,17 @@ pub fn get_item_tx(tx: &Transaction, schema: &Schema, id: &str) -> Result<Vec<Va
Ok(result)
}
fn new_uuid() -> String {
uuid::Uuid::new_v4().to_simple().to_string()
const DEFAULT_ITEM_ID_CHARSET: &[u8] = b"0123456789abcdef";
/// Generate a new random item id.
/// This implementation chooses to generate 32 random hex characters.
fn new_item_id() -> String {
let mut rng = rand::thread_rng();
(0..32)
.map(|_| {
let idx = rng.gen_range(0..DEFAULT_ITEM_ID_CHARSET.len());
DEFAULT_ITEM_ID_CHARSET[idx] as char
})
.collect()
}
fn insert_property(
......@@ -226,22 +236,20 @@ pub fn create_item_tx(
pod_owner: &str,
cli: &CliOptions,
database_key: &DatabaseKey,
) -> Result<i64> {
let default_id: String;
let id = if let Some(id) = &item.id {
id
) -> Result<String> {
let id: String = if let Some(id) = &item.id {
id.to_string()
} else {
default_id = new_uuid();
&default_id
new_item_id()
};
if let Err(err) = schema::validate_create_item_id(id) {
if let Err(err) = schema::validate_create_item_id(&id) {
return Err(err);
}
let time_now = Utc::now().timestamp_millis();
triggers::trigger_before_item_create(tx, &item)?;
let rowid = database_api::insert_item_base(
tx,
id,
&id,
&item._type,
item.date_created.unwrap_or(time_now),
item.date_modified.unwrap_or(time_now),
......@@ -255,13 +263,13 @@ pub fn create_item_tx(
tx,
schema,
rowid,
id,
&id,
&item,
pod_owner,
cli,
database_key,
)?;
Ok(rowid)
Ok(id)
}
pub fn update_item_tx(
......
......@@ -55,7 +55,7 @@ pub fn create_item(
init_db: &RwLock<HashSet<String>>,
body: PayloadWrapper<CreateItem>,
cli: &CliOptions,
) -> Result<i64> {
) -> Result<String> {
let auth = body.auth;
let payload = body.payload;
let database_key = auth_to_database_key(auth)?;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment