Add API method to easiliy tick a track now

This commit is contained in:
D. Scott Boggs 2023-06-17 13:20:08 -04:00
parent e2efd6fe66
commit c85af82eb7
4 changed files with 72 additions and 10 deletions

39
server/Cargo.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -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<DatabaseConnection>, id: i32) -> Api
.map_err(Error::from)?;
Ok(Status::Ok)
}
#[patch("/<id>/ticked")]
pub(super) async fn ticked(
db: &State<DatabaseConnection>,
id: i32,
) -> ApiResult<Json<ticks::Model>> {
let tick = ticks::ActiveModel::now(id);
Ok(Json(
tick.insert(db as &DatabaseConnection)
.await
.map_err(Error::from)?
.to_owned(),
))
}

View file

@ -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<super::tracks::Entity> 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()
}
}
}