shoutdotdev/src/watchdogs.rs

67 lines
1.7 KiB
Rust

use chrono::{DateTime, Utc};
use derive_builder::Builder;
use diesel::{
dsl::{auto_type, AsSelect, Lt},
pg::Pg,
prelude::*,
};
use uuid::Uuid;
use crate::schema::watchdogs;
pub use crate::schema::watchdogs::{dsl, table};
#[derive(Clone, Debug, Identifiable, Queryable, Selectable)]
#[diesel(table_name = watchdogs)]
pub struct Watchdog {
pub id: Uuid,
pub project_id: Uuid,
pub created_at: DateTime<Utc>,
pub last_set_at: DateTime<Utc>,
pub expiration: DateTime<Utc>,
pub notified: bool,
}
impl Watchdog {
#[auto_type(no_type_alias)]
pub fn all() -> _ {
let select: AsSelect<Watchdog, Pg> = Watchdog::as_select();
table.select(select)
}
#[auto_type(no_type_alias)]
pub fn with_id<'a>(id: &'a Uuid) -> _ {
dsl::id.eq(id)
}
#[auto_type(no_type_alias)]
pub fn with_project<'a>(project_id: &'a Uuid) -> _ {
dsl::project_id.eq(project_id)
}
pub fn with_expiration_before<T: Into<DateTime<Utc>>>(
time: T,
) -> Lt<dsl::expiration, DateTime<Utc>> {
dsl::expiration.lt(Into::<DateTime<Utc>>::into(time))
}
#[auto_type(no_type_alias)]
pub fn with_notified(notified: bool) -> _ {
dsl::notified.eq(notified)
}
pub fn insertable_builder() -> InsertableWatchdogBuilder {
InsertableWatchdogBuilder::default()
}
}
#[derive(Builder, Clone, Debug, Insertable)]
#[diesel(table_name = watchdogs)]
#[builder(pattern = "owned", setter(prefix = "with"))]
pub struct InsertableWatchdog {
#[builder(setter(skip), default = "uuid::Uuid::now_v7()")]
id: Uuid,
project_id: Uuid,
#[builder(setter(strip_option), default)]
expiration: Option<DateTime<Utc>>,
}