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, pub last_set_at: DateTime, pub expiration: DateTime, pub notified: bool, } impl Watchdog { #[auto_type(no_type_alias)] pub fn all() -> _ { let select: AsSelect = 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>>( time: T, ) -> Lt> { dsl::expiration.lt(Into::>::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>, }