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

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()
}
}
}