From 56e02021c7bc622cfe2af5a9315d729eca805355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=CC=81scar=20M=2E=20Lage?= Date: Fri, 19 Nov 2021 19:09:41 +0100 Subject: [PATCH] Refactor: stamp add() stop() and update() are now api_post() + added update branch too --- src/main.rs | 36 ++++++++++-------------------------- src/models/stamps.rs | 4 ++-- 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0c53508..f660f71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -112,16 +112,8 @@ fn main() { 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); + let stamp = Stamp::new(&config_file, dstart, dend, description, task); + let added = stamp.api_post(&config_file, "stamp/add"); match added.status() { reqwest::StatusCode::OK => println!("OK"), err => println!("KO: {:?}, something happened", err), @@ -132,16 +124,8 @@ fn main() { // gst stamp --stop else if stop == 1 { console::info("Stop last stamp"); - let stamp = Stamp { - id: 0, - user_id: 0, - project_id: 0, - start: Some("".to_string()), - end: Some("".to_string()), - description: Some("".to_string()), - task_id: Some(0), - }; - let stopped = stamp.stop(&config_file); + let stamp = Stamp::new(&config_file, "", "", "", 0); + let stopped = stamp.api_post(&config_file, "stamp/stop"); match stopped.status() { reqwest::StatusCode::OK => println!("OK"), err => println!("KO: {:?}, something happened", err), @@ -150,12 +134,12 @@ fn main() { // gst stamp --update --description "desc" --dstart "20120101" --dend "20120101" else if update == 1 { console::info("Update last stamp"); - // let stamp = { - // // description - // // dstart - // // dend - // }; - // stamp.update(); + let stamp = Stamp::new(&config_file, dstart, dend, description, task); + let updated = stamp.api_post(&config_file, "stamp/update"); + match updated.status() { + reqwest::StatusCode::OK => println!("OK"), + err => println!("KO: {:?}, something happened", err), + } } } diff --git a/src/models/stamps.rs b/src/models/stamps.rs index fab962d..14f84eb 100644 --- a/src/models/stamps.rs +++ b/src/models/stamps.rs @@ -36,13 +36,13 @@ impl Stamp { } #[tokio::main] - pub async fn stop(&self, config_file: &PathBuf) -> reqwest::Response { + pub async fn api_post(&self, config_file: &PathBuf, endpoint: &str) -> 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/stop"); + let endpoint = format!("{}{}", &config.url, String::from(endpoint)); let client = reqwest::Client::new(); let res = client.post(endpoint) .header("Authorization", &config.key)