WIP: improve search #1

Closed
rob wants to merge 26 commits from improve-search into master
2 changed files with 28 additions and 35 deletions
Showing only changes of commit ad738296a0 - Show all commits

View File

@ -35,7 +35,7 @@ enum SortKey {
impl Default for SortKey { impl Default for SortKey {
fn default() -> Self { fn default() -> Self {
Self::Name Self::Scraped
} }
} }
@ -119,14 +119,10 @@ async fn search(
Ok(res) Ok(res)
} }
// TODO what is NewQuery used for?
// it is not a file search, that is done through SearchQuery
#[derive(Deserialize)] #[derive(Deserialize)]
struct NewQuery { struct NewQuery {
page: Option<usize>, page: Option<usize>,
size: Option<usize>, size: Option<usize>,
sort_key: Option<SortKey>,
sort_dir: Option<SortDirection>,
type_: Option<String>, type_: Option<String>,
} }
@ -134,7 +130,6 @@ async fn new_torrents(
db: web::Data<r2d2::Pool<SqliteConnectionManager>>, db: web::Data<r2d2::Pool<SqliteConnectionManager>>,
query: web::Query<NewQuery>, query: web::Query<NewQuery>,
) -> Result<HttpResponse, actix_web::Error> { ) -> Result<HttpResponse, actix_web::Error> {
let res = web::block(move || { let res = web::block(move || {
let conn = db.get().unwrap(); let conn = db.get().unwrap();
new_query(query, conn) new_query(query, conn)
@ -148,29 +143,6 @@ async fn new_torrents(
Ok(res) Ok(res)
} }
fn build_order_clause(type_: &str, sort_key: SortKey, sort_dir: SortDirection) -> String {
let dir = match sort_dir {
SortDirection::Asc => "asc",
SortDirection::Desc => "desc",
};
let column = match sort_key {
SortKey::Name => {
if type_ == "file" {
"path"
} else {
"name"
}
},
SortKey::Size => "size_bytes",
SortKey::Seeds => "seeders",
SortKey::Leeches => "leechers",
SortKey::Scraped => "scraped_date",
};
format!("{} {}", column, dir)
}
fn search_query( fn search_query(
query: web::Query<SearchQuery>, query: web::Query<SearchQuery>,
conn: r2d2::PooledConnection<SqliteConnectionManager>, conn: r2d2::PooledConnection<SqliteConnectionManager>,
@ -211,14 +183,12 @@ fn new_query(
let page = query.page.unwrap_or(1); let page = query.page.unwrap_or(1);
let size = cmp::min(100, query.size.unwrap_or(DEFAULT_SIZE)); let size = cmp::min(100, query.size.unwrap_or(DEFAULT_SIZE));
let sort_key = query.sort_key.unwrap_or_default();
let sort_dir = query.sort_dir.unwrap_or_default();
let type_ = query.type_.as_ref().map_or("torrent", String::deref); let type_ = query.type_.as_ref().map_or("torrent", String::deref);
let offset = size * (page - 1); let offset = size * (page - 1);
println!( println!(
"new, type = {}, page = {}, size = {}, sort_key = {:?}, sort_dir = {:?}", "new, type = {}, page = {}, size = {}",
type_, page, size, sort_key, sort_dir type_, page, size
); );
let res = if type_ == "file" { let res = if type_ == "file" {
@ -405,6 +375,29 @@ fn torrent_file_new(
Ok(files) Ok(files)
} }
fn build_order_clause(type_: &str, sort_key: SortKey, sort_dir: SortDirection) -> String {
let dir = match sort_dir {
SortDirection::Asc => "asc",
SortDirection::Desc => "desc",
};
let column = match sort_key {
SortKey::Name => {
if type_ == "file" {
"path"
} else {
"name"
}
},
SortKey::Size => "size_bytes",
SortKey::Seeds => "seeders",
SortKey::Leeches => "leechers",
SortKey::Scraped => "scraped_date",
};
format!("{} {}", column, dir)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use r2d2_sqlite::SqliteConnectionManager; use r2d2_sqlite::SqliteConnectionManager;

View File

@ -53,7 +53,7 @@ export class Navbar extends Component<any, State> {
value={this.state.searchParams.q} value={this.state.searchParams.q}
onInput={linkEvent(this, this.searchChange)}></input> onInput={linkEvent(this, this.searchChange)}></input>
<div class="input-group-append"> <div class="input-group-append">
<select value={this.state.searchParams.type_} <select value={this.state.searchParams.type_}
onInput={linkEvent(this, this.searchTypeChange)} onInput={linkEvent(this, this.searchTypeChange)}
class="custom-select border-top-0 border-bottom-0 rounded-0"> class="custom-select border-top-0 border-bottom-0 rounded-0">
<option disabled>Type</option> <option disabled>Type</option>
@ -85,7 +85,6 @@ export class Navbar extends Component<any, State> {
i.setState({ searchParams: searchParams }); i.setState({ searchParams: searchParams });
} }
// TODO why does switching from torrent to file search clear the sort_key?
searchTypeChange(i: Navbar, event) { searchTypeChange(i: Navbar, event) {
let searchParams: SearchParams = { let searchParams: SearchParams = {
q: i.state.searchParams.q, q: i.state.searchParams.q,
@ -96,6 +95,7 @@ export class Navbar extends Component<any, State> {
} }
i.setState({ searchParams: searchParams }); i.setState({ searchParams: searchParams });
} }
fillSearchField() { fillSearchField() {
let splitPath: Array<string> = this.context.router.route.location.pathname.split("/"); let splitPath: Array<string> = this.context.router.route.location.pathname.split("/");
if (splitPath.length == 7 && splitPath[1] == 'search') if (splitPath.length == 7 && splitPath[1] == 'search')