Skip to content

Commit

Permalink
add search ability for sites
Browse files Browse the repository at this point in the history
  • Loading branch information
huangcheng committed Dec 2, 2023
1 parent 4e784b2 commit f71835b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
23 changes: 17 additions & 6 deletions src/handlers/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,26 @@ pub async fn delete_category(id: &str, db: &mut Connection<Db>) -> Result<(), Se

pub async fn get_sites(
category_id: &str,
search: Option<&str>,
upload_url: &str,
db: &mut Connection<Db>,
) -> Result<Vec<response::site::Site>, ServiceError> {
let sites = query_as::<_, response::site::Site>(
r#"SELECT id, name, url, description, icon FROM site WHERE id IN (SELECT site_id FROM category_site WHERE category_id = ?)"#,
)
.bind(category_id)
.fetch_all(&mut ***db)
.await?;
let sites = match search {
Some(search) => query_as::<_, response::site::Site>(
r#"SELECT site.id, site.name, site.url, site.description, site.icon FROM site INNER JOIN category_site ON site.id = category_site.site_id WHERE category_site.category_id = ? AND (site.name LIKE ? OR site.description LIKE ?)"#,
)
.bind(category_id)
.bind(format!("%{}%", search))
.bind(format!("%{}%", search))
.fetch_all(&mut ***db)
.await?,
None => query_as::<_, response::site::Site>(
r#"SELECT site.id, site.name, site.url, site.description, site.icon FROM site INNER JOIN category_site ON site.id = category_site.site_id WHERE category_site.category_id = ?"#,
)
.bind(category_id)
.fetch_all(&mut ***db)
.await?,
};

Ok(sites
.iter()
Expand Down
5 changes: 3 additions & 2 deletions src/routes/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ pub async fn delete(id: &str, mut db: Connection<Db>, _jwt: JwtMiddleware) -> Re
Ok(())
}

#[get("/<id>/sites")]
#[get("/<id>/sites?<search>")]
pub async fn get_sites(
id: &str,
search: Option<&str>,
config: &State<Config>,
mut db: Connection<Db>,
) -> Result<Json<Vec<Site>>, Status> {
let sites = category::get_sites(id, &config.upload_url, &mut db)
let sites = category::get_sites(id, search, &config.upload_url, &mut db)
.await
.map_err(|e| {
error!("{}", e);
Expand Down

0 comments on commit f71835b

Please sign in to comment.