diff --git a/src/cmd.rs b/src/cmd.rs deleted file mode 100644 index aa7abe3..0000000 --- a/src/cmd.rs +++ /dev/null @@ -1,134 +0,0 @@ -use std::path::PathBuf; -use serde::{Serialize, Deserialize}; - -#[path = "./config.rs"] -mod config; - -#[derive(Serialize, Deserialize, Debug)] -struct Project { - id: u32, - name: String, - slug: String, - description: Option, - status: String, - user_id: u32, - paused: u32, -} - -#[derive(Serialize, Deserialize, Debug)] -struct ProjectsResponse { - data: Vec, - error: String, -} - -#[derive(Serialize, Deserialize, Debug)] -struct Task { - id: u32, - name: String, - description: Option, - project_id: u32, -} - -#[derive(Serialize, Deserialize, Debug)] -struct TasksResponse { - data: Vec, - error: String, -} - -#[derive(Serialize, Deserialize, Debug)] -struct Stamp { - id: u32, - user_id: u32, - project_id: u32, - start: Option, - end: Option, - description: Option, -} - -#[derive(Serialize, Deserialize, Debug)] -struct StampsResponse { - data: Vec, - error: String, -} - -pub fn api_call(config_file: &PathBuf, endpoint: String) -> Result { - // Config - let mut default_config = config::Config::default(); - let config = default_config.parse(&config_file); - - // Call api - let endpoint = format!("{}{}", &config.url, endpoint); - let client = reqwest::blocking::Client::new(); - let res = client.get(endpoint) - .header("Content-type", "application/json") - .header("Accept", "application/json") - .header("Authorization", &config.key) - .send(); - res -} - -pub fn get_projects(config_file: &PathBuf) { - let response = api_call(&config_file, String::from("projects")); - // eprintln!("{:#?}", response); - match response { - Ok(parsed) => { - let projects = parsed.json::().unwrap(); - for project in projects.data { - println!("➡️ ({id}) {name}", - id=project.id, - name=project.name); - } - } - Err(e) => println!("Error happened: {}", e), - } -} - -pub fn get_tasks(config_file: &PathBuf, project: u32) { - let response = api_call(&config_file, String::from("tasks")); - // eprintln!("{:#?}", response); - match response { - Ok(parsed) => { - // println!("{:?}", parsed); - let tasks = parsed.json::().unwrap(); - for task in tasks.data { - match project { - // 0 means no project, print them all - 0 => { - println!("✳️ (pr: {projectid}) ({id}) {name}", - projectid=task.project_id, - id=task.id, - name=task.name); - }, - // Otherwise but 0, print only task with that project.id - _ => { - if project == task.project_id { - println!("✳️ (pr: {projectid}) ({id}) {name}", - projectid=task.project_id, - id=task.id, - name=task.name); - } - } - } - } - } - Err(e) => println!("Error happened: {}", e), - } -} - -pub fn get_stamps(config_file: &PathBuf, last: u64) { - let response = api_call(&config_file, String::from("stamps")); - // eprintln!("{:#?}", response); - match response { - Ok(parsed) => { - let stamps = parsed.json::().unwrap(); - // eprintln!("{:#?}", stamps); - for stamp in stamps.data { - println!("⏳ ({id}) {description}", - id=stamp.id, - description=stamp.description.unwrap()); - if last != 0 { break; } - } - } - Err(e) => println!("Error happened: {}", e), - } -} diff --git a/src/main.rs b/src/main.rs index 675cbd3..a5c6091 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,18 @@ mod cli; mod cmd; pub mod console; +#[path = "./models/tasks.rs"] +mod tasks; +use tasks::{Task, Tasks}; + +#[path = "./models/projects.rs"] +mod projects; +use projects::Projects; + +#[path = "./models/stamps.rs"] +mod stamps; +use stamps::Stamps; + fn main() { // Get cli matches let matches = cli::build_cli().get_matches(); @@ -23,7 +35,11 @@ fn main() { // gst projects ("projects", Some(_matches)) => { console::info("List of projects"); - cmd::get_projects(&config_file); + let projects = Projects { + data: Vec::new(), + error: "".to_string(), + }; + projects.get(&config_file); } // gst tasks [--project] @@ -32,14 +48,22 @@ fn main() { let project = _matches.value_of("project") .unwrap_or("0").trim().parse() .expect("Type a number!"); - cmd::get_tasks(&config_file, project); + let tasks = Tasks { + data: Vec::new(), + error: "".to_string(), + }; + tasks.get(&config_file, project); } // gst stamps [--last] ("stamps", Some(_matches)) => { console::info("List of stamps"); let last = _matches.occurrences_of("last"); - cmd::get_stamps(&config_file, last); + let stamps = Stamps { + data: Vec::new(), + error: "".to_string(), + }; + stamps.get(&config_file, last); } _ => console::error("Whut!!!"), diff --git a/src/config.rs b/src/models/config.rs similarity index 100% rename from src/config.rs rename to src/models/config.rs diff --git a/src/models/projects.rs b/src/models/projects.rs new file mode 100644 index 0000000..df8b1f1 --- /dev/null +++ b/src/models/projects.rs @@ -0,0 +1,50 @@ +use std::path::PathBuf; +use serde::{Serialize, Deserialize}; + +mod config; + +#[derive(Serialize, Deserialize, Debug)] +pub struct Project { + pub id: u32, + pub name: String, + pub slug: String, + pub description: Option, + pub status: String, + pub user_id: u32, + pub paused: u32, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct Projects { + pub data: Vec, + pub error: String, +} + +impl Projects { + pub fn get(&self, config_file: &PathBuf) { + // Config + let mut default_config = config::Config::default(); + let config = default_config.parse(&config_file); + + // Call api + let endpoint = format!("{}{}", &config.url, "projects"); + let client = reqwest::blocking::Client::new(); + let res = client.get(endpoint) + .header("Content-type", "application/json") + .header("Accept", "application/json") + .header("Authorization", &config.key) + .send(); + + match res { + Ok(parsed) => { + let projects = parsed.json::().unwrap(); + for project in projects.data { + println!("➡️ ({id}) {name}", + id=project.id, + name=project.name); + } + } + Err(e) => println!("Error happened: {}", e), + } + } +} diff --git a/src/models/stamps.rs b/src/models/stamps.rs new file mode 100644 index 0000000..6944a5e --- /dev/null +++ b/src/models/stamps.rs @@ -0,0 +1,51 @@ +use std::path::PathBuf; +use serde::{Serialize, Deserialize}; + +mod config; + +#[derive(Serialize, Deserialize, Debug)] +pub struct Stamp { + pub id: u32, + pub user_id: u32, + pub project_id: u32, + pub start: Option, + pub end: Option, + pub description: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct Stamps { + pub data: Vec, + pub error: String, +} + +impl Stamps { + pub fn get(&self, config_file: &PathBuf, last: u64) { + // Config + let mut default_config = config::Config::default(); + let config = default_config.parse(&config_file); + + // Call api + let endpoint = format!("{}{}", &config.url, "stamps"); + let client = reqwest::blocking::Client::new(); + let res = client.get(endpoint) + .header("Content-type", "application/json") + .header("Accept", "application/json") + .header("Authorization", &config.key) + .send(); + + match res { + Ok(parsed) => { + let stamps = parsed.json::().unwrap(); + // eprintln!("{:#?}", stamps); + for stamp in stamps.data { + println!("⏳ ({id}) {description}", + id=stamp.id, + description=stamp.description.unwrap()); + if last != 0 { break; } + } + } + Err(e) => println!("Error happened: {}", e), + } + } +} diff --git a/src/models/tasks.rs b/src/models/tasks.rs new file mode 100644 index 0000000..c753e57 --- /dev/null +++ b/src/models/tasks.rs @@ -0,0 +1,85 @@ +use std::path::PathBuf; +use serde::{Serialize, Deserialize}; + +mod config; + +#[derive(Serialize, Deserialize, Debug)] +pub struct Task { + pub id: u32, + pub name: String, + pub description: Option, + pub project_id: u32, + pub estimated: Option, +} + +impl Task { + #[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, "task/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)] +pub struct Tasks { + pub data: Vec, + pub error: String, +} + +impl Tasks { + pub fn get(&self, config_file: &PathBuf, project: u32) { + // Config + let mut default_config = config::Config::default(); + let config = default_config.parse(&config_file); + + // Call api + let endpoint = format!("{}{}", &config.url, "tasks"); + let client = reqwest::blocking::Client::new(); + let res = client.get(endpoint) + .header("Content-type", "application/json") + .header("Accept", "application/json") + .header("Authorization", &config.key) + .send(); + match res { + Ok(parsed) => { + // println!("{:?}", parsed); + let tasks = parsed.json::().unwrap(); + for task in tasks.data { + match project { + // 0 means no project, print them all + 0 => { + println!("✳️ (pr: {projectid}) ({id}) {name}", + projectid=task.project_id, + id=task.id, + name=task.name); + }, + // Otherwise but 0, print only task with that project.id + _ => { + if project == task.project_id { + println!("✳️ (pr: {projectid}) ({id}) {name}", + projectid=task.project_id, + id=task.id, + name=task.name); + } + } + } + } + } + Err(e) => println!("Error happened: {}", e), + } + } +} +