WIP: improve search #1
|
@ -28,7 +28,7 @@ enum SortKey {
|
|||
Size,
|
||||
Seeders,
|
||||
Leechers,
|
||||
Date,
|
||||
Scraped,
|
||||
}
|
||||
|
||||
impl Default for SortKey {
|
||||
|
@ -44,7 +44,7 @@ impl fmt::Display for SortKey {
|
|||
Self::Size => "size_bytes",
|
||||
Self::Seeders => "seeders",
|
||||
Self::Leechers => "leechers",
|
||||
Self::Date => "scraped_date",
|
||||
Self::Scraped => "scraped_date",
|
||||
};
|
||||
write!(f, "{}", s)
|
||||
}
|
||||
|
@ -190,7 +190,7 @@ fn search_query(
|
|||
);
|
||||
|
||||
let res = if type_ == "file" {
|
||||
let results = torrent_file_search(conn, q, size, offset)?;
|
||||
let results = torrent_file_search(conn, q, sort_key, sort_dir, size, offset)?;
|
||||
serde_json::to_value(&results).unwrap()
|
||||
} else {
|
||||
let results = torrent_search(conn, q, sort_key, sort_dir, size, offset)?;
|
||||
|
@ -329,10 +329,13 @@ struct File {
|
|||
fn torrent_file_search(
|
||||
conn: r2d2::PooledConnection<SqliteConnectionManager>,
|
||||
query: &str,
|
||||
sort_key: SortKey,
|
||||
sort_dir: SortDirection,
|
||||
size: usize,
|
||||
offset: usize,
|
||||
) -> Result<Vec<File>, Error> {
|
||||
let stmt_str = "select * from files where path like '%' || ?1 || '%' limit ?2, ?3";
|
||||
// `sort_key` and `sort_dir` are already sanitized and should not be escaped:
|
||||
let stmt_str = format!("select * from files where path like '%' || ?1 || '%' order by {} {} limit ?2, ?3", sort_key, sort_dir);
|
||||
let mut stmt = conn.prepare(&stmt_str).unwrap();
|
||||
let file_iter = stmt.query_map(
|
||||
params![
|
||||
|
@ -405,7 +408,7 @@ mod tests {
|
|||
let manager = SqliteConnectionManager::file(super::torrents_db_file());
|
||||
let pool = r2d2::Pool::builder().max_size(15).build(manager).unwrap();
|
||||
let conn = pool.get().unwrap();
|
||||
let results = super::torrent_search(conn, "sherlock", 10, 0, SortKey::Name, SortDirection::Desc);
|
||||
let results = super::torrent_search(conn, "sherlock", super::SortKey::Name, super::SortDirection::Desc, 10, 0);
|
||||
assert!(results.unwrap().len() > 2);
|
||||
// println!("Query took {:?} seconds.", end - start);
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ class SortableLink extends Component {
|
|||
if (searchParams.sort_dir === "Asc") {
|
||||
return <svg class="icon icon-arrow-up d-none d-sm-inline mr-1"><use xlinkHref="#icon-arrow-up"></use></svg>;
|
||||
}
|
||||
return <svg class="icon icon-arrow-down mr-1"><use xlinkHref="#icon-arrow-down"></use></svg>l
|
||||
return <svg class="icon icon-arrow-down mr-1"><use xlinkHref="#icon-arrow-down"></use></svg>;
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -163,7 +163,7 @@ export class Search extends Component<any, State> {
|
|||
<SortableLink sortKey="Leechers" state={this.state}/>
|
||||
</th>
|
||||
<th class="text-right d-none d-md-table-cell">
|
||||
<SortableLink sortKey="Date" state={this.state}/>
|
||||
<SortableLink sortKey="Scraped" state={this.state}/>
|
||||
</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
|
|
Loading…
Reference in New Issue