diff --git a/src/main.rs b/src/main.rs index a291cc8..a523d8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use projects::Projects; #[path = "./models/stamps.rs"] mod stamps; -use stamps::Stamps; +use stamps::{Stamp, Stamps}; fn main() { // Get cli matches @@ -95,6 +95,43 @@ fn main() { } } + // gst stamp --start --task NUM --description "desc" --dstart "20120101" --dend "20120101" + // gst stamp --stop + // gst stamp --update --description "desc" --dstart "20120101" --dend "20120101" + ("stamp", Some(_matches)) => { + let start = _matches.occurrences_of("start"); + let stop = _matches.occurrences_of("stop"); + let update = _matches.occurrences_of("update"); + let task: u32 = _matches.value_of("task") + .unwrap_or("0").trim().parse() + .expect("Type a number!"); + let description: &str = _matches.value_of("description").unwrap_or("").trim(); + let dstart: &str = _matches.value_of("dstart").unwrap_or("").trim(); + let dend: &str = _matches.value_of("dend").unwrap_or("").trim(); + if start == 1 { + console::info("Add a new stamp"); + match task { + 0 => panic!("Need some task number to add the stamp"), + _ => { + let stamp = Stamp { + id: 0, + user_id: 0, + project_id: 0, + start: Some(dstart.to_string()), + end: Some(dend.to_string()), + description: Some(description.to_string()), + task_id: Some(task), + }; + let added = stamp.add(&config_file); + match added.status() { + reqwest::StatusCode::OK => println!("OK"), + other => println!("KO: {:?}, something happened", other), + } + } + } + } + } + _ => console::error("Whut!!!"), } } diff --git a/src/models/stamps.rs b/src/models/stamps.rs index a94262b..d0f4afc 100644 --- a/src/models/stamps.rs +++ b/src/models/stamps.rs @@ -11,6 +11,24 @@ pub struct Stamp { pub start: Option, pub end: Option, pub description: Option, +impl Stamp { + #[tokio::main] + pub async fn add(&self, config_file: &PathBuf) -> reqwest::Response { + // Config + let mut default_config = config::Config::default(); + let config = default_config.parse(&config_file); + + // Call api + let endpoint = format!("{}{}", &config.url, "stamp/add"); + let client = reqwest::Client::new(); + let res = client.post(endpoint) + .header("Authorization", &config.key) + .json(&self) + .send() + .await; + + res.unwrap() + } } #[derive(Serialize, Deserialize, Debug)]