From c85af82eb7e2a22b95812202c5cb586fcfc5eb0b Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Sat, 17 Jun 2023 13:20:08 -0400 Subject: [PATCH] Add API method to easiliy tick a track now --- server/Cargo.lock | 39 +++++++++++++++++++++++++++--------- server/Cargo.toml | 1 + server/src/api/tracks.rs | 16 ++++++++++++++- server/src/entities/ticks.rs | 26 ++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 10 deletions(-) diff --git a/server/Cargo.lock b/server/Cargo.lock index 77eb380..459e05a 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -297,8 +297,11 @@ checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5" dependencies = [ "android-tzdata", "iana-time-zone", + "js-sys", "num-traits", "serde", + "time 0.1.45", + "wasm-bindgen", "winapi", ] @@ -345,7 +348,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7efb37c3e1ccb1ff97164ad95ac1606e8ccd35b3fa0a7d99a304c7f4a428cc24" dependencies = [ "percent-encoding", - "time", + "time 0.3.22", "version_check", ] @@ -750,7 +753,7 @@ checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", ] [[package]] @@ -1046,6 +1049,7 @@ dependencies = [ name = "kalkulog-server" version = "0.1.0" dependencies = [ + "chrono", "derive_builder", "either", "femme", @@ -1154,7 +1158,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" dependencies = [ "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys", ] @@ -1660,7 +1664,7 @@ dependencies = [ "serde_json", "state", "tempfile", - "time", + "time 0.3.22", "tokio", "tokio-stream", "tokio-util", @@ -1705,7 +1709,7 @@ dependencies = [ "smallvec", "stable-pattern", "state", - "time", + "time 0.3.22", "tokio", "uncased", ] @@ -1819,7 +1823,7 @@ dependencies = [ "serde_json", "sqlx", "thiserror", - "time", + "time 0.3.22", "tracing", "url", "uuid", @@ -1882,7 +1886,7 @@ dependencies = [ "rust_decimal", "sea-query-derive", "serde_json", - "time", + "time 0.3.22", "uuid", ] @@ -1898,7 +1902,7 @@ dependencies = [ "sea-query", "serde_json", "sqlx", - "time", + "time 0.3.22", "uuid", ] @@ -2169,7 +2173,7 @@ dependencies = [ "sqlx-rt", "stringprep", "thiserror", - "time", + "time 0.3.22", "tokio-stream", "url", "uuid", @@ -2393,6 +2397,17 @@ dependencies = [ "once_cell", ] +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "time" version = "0.3.22" @@ -2767,6 +2782,12 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/server/Cargo.toml b/server/Cargo.toml index 644e931..1102e3f 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -13,6 +13,7 @@ path = "src/main.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +chrono = "0.4.26" femme = "2.2.1" sea-orm-migration = "0.11.3" serde_json = "1.0.96" diff --git a/server/src/api/tracks.rs b/server/src/api/tracks.rs index 61d8121..f8bfc4f 100644 --- a/server/src/api/tracks.rs +++ b/server/src/api/tracks.rs @@ -4,7 +4,7 @@ use crate::error::Error; use either::Either::{self, Left, Right}; use rocket::http::Status; use rocket::{serde::json::Json, State}; -use sea_orm::{prelude::*, DatabaseConnection, EntityOrSelect}; +use sea_orm::{prelude::*, DatabaseConnection}; use std::default::default; #[get("/")] @@ -86,3 +86,17 @@ pub(super) async fn delete_track(db: &State, id: i32) -> Api .map_err(Error::from)?; Ok(Status::Ok) } + +#[patch("//ticked")] +pub(super) async fn ticked( + db: &State, + id: i32, +) -> ApiResult> { + let tick = ticks::ActiveModel::now(id); + Ok(Json( + tick.insert(db as &DatabaseConnection) + .await + .map_err(Error::from)? + .to_owned(), + )) +} diff --git a/server/src/entities/ticks.rs b/server/src/entities/ticks.rs index e2a4914..a593f78 100644 --- a/server/src/entities/ticks.rs +++ b/server/src/entities/ticks.rs @@ -1,5 +1,8 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3 +use std::default::default; + +use chrono::{Datelike, Timelike, Utc}; use sea_orm::entity::prelude::*; use serde::{Deserialize, Serialize}; @@ -37,3 +40,26 @@ impl Related for Entity { } impl ActiveModelBehavior for ActiveModel {} + +impl ActiveModel { + pub(crate) fn now(track_id: i32) -> Self { + use sea_orm::ActiveValue::Set; + let now = Utc::now(); + Self { + track_id: Set(Some(track_id)), + year: Set(Some(now.year())), + month: Set(now.month().try_into().ok()), + /* ^^^^^^^^^^^^^^^^^^^^^^^ + * I can't imagine a situation where this doesn't fit. This way, at + * least, if it fails, you just get a messed up database entry that + * doesn't do anything bad + */ + day: Set(now.day().try_into().ok()), + hour: Set(now.hour().try_into().ok()), + minute: Set(now.minute().try_into().ok()), + second: Set(now.second().try_into().ok()), + has_time_info: Set(Some(1)), + ..default() + } + } +}