fedimovies/src/models/tags/queries.rs

25 lines
615 B
Rust
Raw Normal View History

2022-09-19 00:45:56 +00:00
use tokio_postgres::GenericClient;
2022-12-03 22:09:42 +00:00
use crate::database::DatabaseError;
2022-09-19 00:45:56 +00:00
pub async fn search_tags(
db_client: &impl GenericClient,
search_query: &str,
2022-09-29 21:36:55 +00:00
limit: u16,
2022-09-19 00:45:56 +00:00
) -> Result<Vec<String>, DatabaseError> {
let db_search_query = format!("%{}%", search_query);
let rows = db_client.query(
"
SELECT tag_name
FROM tag
WHERE tag_name ILIKE $1
LIMIT $2
",
2022-09-29 21:36:55 +00:00
&[&db_search_query, &i64::from(limit)],
2022-09-19 00:45:56 +00:00
).await?;
let tags: Vec<String> = rows.iter()
.map(|row| row.try_get("tag_name"))
.collect::<Result<_, _>>()?;
Ok(tags)
}