From 8a1435d9e94135617bd55c8bbb74d21a1622cc88 Mon Sep 17 00:00:00 2001 From: Connor Johnstone Date: Fri, 20 Mar 2026 16:28:15 -0400 Subject: [PATCH] Added the watch and scheduler systems --- src/entities/artist.rs | 3 + .../m20260320_000013_add_artist_monitoring.rs | 59 +++++++++++++++++++ src/migration/mod.rs | 2 + src/queries/artists.rs | 24 ++++++++ 4 files changed, 88 insertions(+) create mode 100644 src/migration/m20260320_000013_add_artist_monitoring.rs diff --git a/src/entities/artist.rs b/src/entities/artist.rs index edfb1b6..4635d63 100644 --- a/src/entities/artist.rs +++ b/src/entities/artist.rs @@ -14,6 +14,9 @@ pub struct Model { pub top_songs: String, /// JSON-serialized Vec of similar artist info pub similar_artists: String, + pub monitored: bool, + #[sea_orm(nullable)] + pub last_checked_at: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/src/migration/m20260320_000013_add_artist_monitoring.rs b/src/migration/m20260320_000013_add_artist_monitoring.rs new file mode 100644 index 0000000..5889e37 --- /dev/null +++ b/src/migration/m20260320_000013_add_artist_monitoring.rs @@ -0,0 +1,59 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(Artists::Table) + .add_column( + ColumnDef::new(Artists::Monitored) + .boolean() + .not_null() + .default(false), + ) + .to_owned(), + ) + .await?; + + manager + .alter_table( + Table::alter() + .table(Artists::Table) + .add_column(ColumnDef::new(Artists::LastCheckedAt).timestamp()) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(Artists::Table) + .drop_column(Artists::LastCheckedAt) + .to_owned(), + ) + .await?; + + manager + .alter_table( + Table::alter() + .table(Artists::Table) + .drop_column(Artists::Monitored) + .to_owned(), + ) + .await + } +} + +#[derive(DeriveIden)] +enum Artists { + Table, + Monitored, + LastCheckedAt, +} diff --git a/src/migration/mod.rs b/src/migration/mod.rs index eda814a..67d3f33 100644 --- a/src/migration/mod.rs +++ b/src/migration/mod.rs @@ -11,6 +11,7 @@ mod m20260317_000009_add_wanted_name; mod m20260317_000010_add_wanted_mbid; mod m20260319_000011_create_users; mod m20260319_000012_add_user_id_to_wanted_items; +mod m20260320_000013_add_artist_monitoring; pub struct Migrator; @@ -29,6 +30,7 @@ impl MigratorTrait for Migrator { Box::new(m20260317_000010_add_wanted_mbid::Migration), Box::new(m20260319_000011_create_users::Migration), Box::new(m20260319_000012_add_user_id_to_wanted_items::Migration), + Box::new(m20260320_000013_add_artist_monitoring::Migration), ] } } diff --git a/src/queries/artists.rs b/src/queries/artists.rs index 3c187b2..e04ed56 100644 --- a/src/queries/artists.rs +++ b/src/queries/artists.rs @@ -38,6 +38,8 @@ pub async fn upsert( added_at: Set(now), top_songs: Set("[]".to_string()), similar_artists: Set("[]".to_string()), + monitored: Set(false), + last_checked_at: Set(None), ..Default::default() }; match active.insert(db).await { @@ -109,3 +111,25 @@ pub async fn delete(db: &DatabaseConnection, id: i32) -> DbResult<()> { Artists::delete_by_id(id).exec(db).await?; Ok(()) } + +pub async fn set_monitored(db: &DatabaseConnection, id: i32, monitored: bool) -> DbResult { + let existing = get_by_id(db, id).await?; + let mut active: ActiveModel = existing.into(); + active.monitored = Set(monitored); + Ok(active.update(db).await?) +} + +pub async fn list_monitored(db: &DatabaseConnection) -> DbResult> { + Ok(Artists::find() + .filter(artist::Column::Monitored.eq(true)) + .order_by_asc(artist::Column::Name) + .all(db) + .await?) +} + +pub async fn update_last_checked(db: &DatabaseConnection, id: i32) -> DbResult { + let existing = get_by_id(db, id).await?; + let mut active: ActiveModel = existing.into(); + active.last_checked_at = Set(Some(Utc::now().naive_utc())); + Ok(active.update(db).await?) +}