implement search keyword handler

This commit is contained in:
yggverse 2026-01-08 17:10:56 +02:00
parent a0ba992746
commit b5dd30dafb
2 changed files with 20 additions and 9 deletions

View file

@ -33,7 +33,7 @@ fn index(
title: String, title: String,
} }
let total = db let total = db
.contents_total_by_provider_id(global.provider_id) .contents_total_by_provider_id(global.provider_id, search)
.map_err(|e| { .map_err(|e| {
error!("Could not get contents total: `{e}`"); error!("Could not get contents total: `{e}`");
Status::InternalServerError Status::InternalServerError
@ -65,7 +65,7 @@ fn index(
back: page.map(|p| uri!(index(search, if p > 2 { Some(p - 1) } else { None }))), 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 } next: if page.unwrap_or(1) * global.list_limit >= total { None }
else { Some(uri!(index(search, Some(page.map_or(2, |p| p + 1))))) }, 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}`"); error!("Could not get contents: `{e}`");
Status::InternalServerError Status::InternalServerError
})? })?
@ -118,8 +118,9 @@ fn info(
} }
} }
#[get("/rss")] #[get("/rss?<search>")]
fn rss( fn rss(
search: Option<&str>,
global: &State<Global>, global: &State<Global>,
meta: &State<Meta>, meta: &State<Meta>,
db: &State<Mysql>, db: &State<Mysql>,
@ -130,7 +131,7 @@ fn rss(
1024, // @TODO 1024, // @TODO
); );
for c in db 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| { .map_err(|e| {
error!("Could not load channel item contents: `{e}`"); error!("Could not load channel item contents: `{e}`");
Status::InternalServerError Status::InternalServerError

View file

@ -106,10 +106,14 @@ impl Mysql {
) )
} }
pub fn contents_total_by_provider_id(&self, provider_id: Option<u64>) -> Result<usize, Error> { pub fn contents_total_by_provider_id(
&self,
provider_id: Option<u64>,
keyword: Option<&str>,
) -> Result<usize, Error> {
let total: Option<usize> = self.pool.get_conn()?.exec_first( let total: Option<usize> = self.pool.get_conn()?.exec_first(
"SELECT COUNT(*) FROM `content` WHERE `provider_id` <=> ?", "SELECT COUNT(*) FROM `content` WHERE `provider_id` <=> ? AND `title` LIKE ?",
(provider_id,), (provider_id, like(keyword)),
)?; )?;
Ok(total.unwrap_or(0)) Ok(total.unwrap_or(0))
} }
@ -117,6 +121,7 @@ impl Mysql {
pub fn contents_by_provider_id( pub fn contents_by_provider_id(
&self, &self,
provider_id: Option<u64>, provider_id: Option<u64>,
keyword: Option<&str>,
sort: Sort, sort: Sort,
limit: Option<usize>, limit: Option<usize>,
) -> Result<Vec<Content>, Error> { ) -> Result<Vec<Content>, Error> {
@ -125,10 +130,10 @@ impl Mysql {
`channel_item_id`, `channel_item_id`,
`provider_id`, `provider_id`,
`title`, `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) limit.unwrap_or(DEFAULT_LIMIT)
), ),
(provider_id, )) (provider_id, like(keyword), ))
} }
/// Get subjects for `rssto-llm` queue /// 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; const DEFAULT_LIMIT: usize = 100;