Skip to content

Commit

Permalink
add whole words only setting to search endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
berinaniesh committed Jul 11, 2024
1 parent 06b1328 commit d6866e2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub struct TranslationSelector {
pub struct SearchParameters {
pub search_text: String,
pub match_case: Option<bool>,
pub whole_words: Option<bool>,
pub translation: String,
pub book: Option<String>,
pub abbreviation: Option<String>,
Expand Down
21 changes: 16 additions & 5 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,24 @@ pub async fn search(search_parameters: web::Json<SearchParameters>, app_data: we
let mut qb: QueryBuilder<Postgres> = QueryBuilder::new(r#"
SELECT translation, book, abbreviation, book_name, chapter, verse_number, verse from fulltable where verse "#);
let match_case = search_parameters.match_case.unwrap_or(false);
if match_case {
qb.push("like ");
let whole_words = search_parameters.whole_words.unwrap_or(false);
if whole_words {
if match_case {
qb.push("~ ");
} else {
qb.push("~* ");
}
let actual_search_string = format!(r#"\m{}\M"#, &search_parameters.search_text);
qb.push_bind(actual_search_string);
} else {
qb.push("ilike ");
if match_case {
qb.push("like ");
} else {
qb.push("ilike ");
}
let actual_search_string = format!("%{}%", &search_parameters.search_text);
qb.push_bind(actual_search_string);
}
let actual_search_string = format!("%{}%", &search_parameters.search_text);
qb.push_bind(actual_search_string);
qb.push(" and translation=");
qb.push_bind(search_parameters.translation.to_uppercase());
if let Some(book) = &search_parameters.book {
Expand Down

0 comments on commit d6866e2

Please sign in to comment.