Implement adding new items through edit
This commit is contained in:
parent
dd37800f0a
commit
88138dc783
|
@ -15,7 +15,7 @@ fn cli() -> Command {
|
|||
.arg(
|
||||
arg!(--server <URL> "The base URL of the tracking server")
|
||||
.short('s')
|
||||
.default_value("http://localhost:3000")
|
||||
.default_value("https://timer.thomasave.be")
|
||||
)
|
||||
.arg(
|
||||
arg!(--json "Use JSON output")
|
||||
|
@ -88,6 +88,7 @@ async fn main() {
|
|||
Some(("status", _)) => {
|
||||
status(settings).await.unwrap();
|
||||
}
|
||||
_ => unreachable!(), // If all subcommands are defined above, anything else is unreachable!()
|
||||
_ => cli().print_help().unwrap(),
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,9 +17,9 @@ pub struct Settings {
|
|||
pub json: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct WorkPeriod {
|
||||
pub id: i32,
|
||||
pub id: Option<i32>,
|
||||
pub project: String,
|
||||
pub start_time: NaiveDateTime,
|
||||
pub end_time: Option<NaiveDateTime>,
|
||||
|
@ -116,7 +116,21 @@ fn edit_periods(periods: Vec<WorkPeriod>) -> Result<Vec<WorkPeriod>, std::io::Er
|
|||
|
||||
pub async fn update_period(settings: &Settings, period: WorkPeriod) -> Result<(), reqwest::Error> {
|
||||
let client = Client::new();
|
||||
let response = client.put(settings.url.to_string() + "/api/history/" + &period.id.to_string())
|
||||
let response = client.put(settings.url.to_string() + "/api/history/" + &period.id.unwrap().to_string())
|
||||
.json(&period)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
println!("{:?}", response.text().await);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn add_period(settings: &Settings, period: &mut WorkPeriod) -> Result<(), reqwest::Error> {
|
||||
let client = Client::new();
|
||||
period.id = None;
|
||||
let response = client.post(settings.url.to_string() + "/api/history")
|
||||
.json(&period)
|
||||
.send()
|
||||
.await?;
|
||||
|
@ -164,10 +178,15 @@ pub async fn edit(settings: Settings, since: Option<&String>, until: Option<&Str
|
|||
} else {
|
||||
let body = response.text().await.unwrap();
|
||||
let periods = parse_periods(body).unwrap();
|
||||
let mut ids = periods.iter().map(|p| p.id).collect::<Vec<i32>>();
|
||||
let mut ids = periods.iter().map(|p| p.id.unwrap()).collect::<Vec<i32>>();
|
||||
let res = edit_periods(periods).unwrap();
|
||||
for period in res {
|
||||
ids.remove(ids.iter().position(|&x| x == period.id).unwrap());
|
||||
let pos = ids.iter().position(|&x| x == period.id.unwrap());
|
||||
if let Some(pos) = pos {
|
||||
ids.remove(pos);
|
||||
} else {
|
||||
add_period(&settings, &mut period.clone()).await.unwrap();
|
||||
}
|
||||
update_period(&settings, period).await.unwrap();
|
||||
}
|
||||
for id in ids {
|
||||
|
@ -177,6 +196,23 @@ pub async fn edit(settings: Settings, since: Option<&String>, until: Option<&Str
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Status {
|
||||
pub today: String,
|
||||
pub week: String,
|
||||
pub month: String,
|
||||
pub year: String,
|
||||
pub active: String,
|
||||
}
|
||||
|
||||
pub async fn status(settings: Settings) -> Result<(), reqwest::Error> {
|
||||
let client = Client::new();
|
||||
let response = client.get(settings.url.to_string() + "/")
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let response_text = response.text().await.unwrap();
|
||||
let status: Status = serde_json::from_str(&response_text).unwrap();
|
||||
println!("{}", serde_json::to_string_pretty(&status).unwrap());
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue