diff --git a/crates/http/src/main.rs b/crates/http/src/main.rs index 1e3302d..0c04491 100644 --- a/crates/http/src/main.rs +++ b/crates/http/src/main.rs @@ -33,7 +33,7 @@ fn index( title: String, } let total = db - .contents_total_by_provider_id(global.provider_id) + .contents_total_by_provider_id(global.provider_id, search) .map_err(|e| { error!("Could not get contents total: `{e}`"); Status::InternalServerError @@ -65,7 +65,7 @@ fn index( back: page.map(|p| uri!(index(search, if p > 2 { Some(p - 1) } else { None }))), next: if page.unwrap_or(1) * global.list_limit >= total { None } else { Some(uri!(index(search, Some(page.map_or(2, |p| p + 1))))) }, - rows: db.contents_by_provider_id(global.provider_id, Sort::Desc, Some(global.list_limit)).map_err(|e| { + rows: db.contents_by_provider_id(global.provider_id, search, Sort::Desc, Some(global.list_limit)).map_err(|e| { error!("Could not get contents: `{e}`"); Status::InternalServerError })? @@ -118,8 +118,9 @@ fn info( } } -#[get("/rss")] +#[get("/rss?")] fn rss( + search: Option<&str>, global: &State, meta: &State, db: &State, @@ -130,7 +131,7 @@ fn rss( 1024, // @TODO ); for c in db - .contents_by_provider_id(global.provider_id, Sort::Desc, Some(20)) // @TODO + .contents_by_provider_id(global.provider_id, search, Sort::Desc, Some(20)) // @TODO .map_err(|e| { error!("Could not load channel item contents: `{e}`"); Status::InternalServerError diff --git a/crates/mysql/src/lib.rs b/crates/mysql/src/lib.rs index f8c6c48..f3e168f 100644 --- a/crates/mysql/src/lib.rs +++ b/crates/mysql/src/lib.rs @@ -106,10 +106,14 @@ impl Mysql { ) } - pub fn contents_total_by_provider_id(&self, provider_id: Option) -> Result { + pub fn contents_total_by_provider_id( + &self, + provider_id: Option, + keyword: Option<&str>, + ) -> Result { let total: Option = self.pool.get_conn()?.exec_first( - "SELECT COUNT(*) FROM `content` WHERE `provider_id` <=> ?", - (provider_id,), + "SELECT COUNT(*) FROM `content` WHERE `provider_id` <=> ? AND `title` LIKE ?", + (provider_id, like(keyword)), )?; Ok(total.unwrap_or(0)) } @@ -117,6 +121,7 @@ impl Mysql { pub fn contents_by_provider_id( &self, provider_id: Option, + keyword: Option<&str>, sort: Sort, limit: Option, ) -> Result, Error> { @@ -125,10 +130,10 @@ impl Mysql { `channel_item_id`, `provider_id`, `title`, - `description` FROM `content` WHERE `provider_id` <=> ? ORDER BY `content_id` {sort} LIMIT {}", + `description` FROM `content` WHERE `provider_id` <=> ? AND `title` LIKE ? ORDER BY `content_id` {sort} LIMIT {}", limit.unwrap_or(DEFAULT_LIMIT) ), - (provider_id, )) + (provider_id, like(keyword), )) } /// Get subjects for `rssto-llm` queue @@ -252,4 +257,9 @@ impl std::fmt::Display for Sort { } } +/// Shared search logic +fn like(value: Option<&str>) -> String { + value.map_or("%".into(), |k| format!("{k}%")) +} + const DEFAULT_LIMIT: usize = 100;