Skip to content

Commit

Permalink
🐛 #133 Fixxing sort issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jgunzelman88 committed Jun 4, 2023
1 parent c8af48a commit 383067d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 33 deletions.
7 changes: 5 additions & 2 deletions src-tauri/src/routes/collections.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::models::pokemon::{Card, SealedProduct};
use crate::routes::poke_card::CardSearch;
use crate::routes::poke_product::{ProductSearch, ProductSearchResults};
use crate::utils::shared::sort_sql;
use crate::utils::sql_collection_data::{
add_tag, delete_card, delete_tag, get_tags, search_card_collection,
search_card_collection_count, upsert_card, search_product_collection,
Expand Down Expand Up @@ -115,6 +116,7 @@ pub async fn search_cards(
let count;
let tag_str = search_params.0.tags.clone().unwrap_or_default();
let _tags: String = urlencoding::decode(&tag_str).unwrap().into();
let sort: String = sort_sql(&search_params.0.sort.unwrap_or_default());

match search_card_collection_count(
search_params.0.name.clone(),
Expand All @@ -131,7 +133,7 @@ pub async fn search_cards(
search_params.0.expansions,
search_params.0.rarities,
Some(_tags),
search_params.0.sort,
Some(sort),
None,
) {
Ok(val) => _cards = val,
Expand All @@ -149,12 +151,13 @@ pub async fn search_products(
search_params: web::Query<ProductSearch>
) -> Result<impl Responder> {
let products: Vec<SealedProduct>;
let sort: String = sort_sql(&search_params.0.sort.unwrap_or_default());
let count: i64;
match product_collection_count(search_params.0.name.clone(), search_params.0.types.clone(), search_params.0.tags.clone()){
Ok(val) => count = val,
Err(e) => return Err(error::ErrorBadRequest(e))
}
match search_product_collection(*page, search_params.0.name, search_params.0.types, search_params.0.tags, search_params.0.sort){
match search_product_collection(*page, search_params.0.name, search_params.0.types, search_params.0.tags, Some(sort)){
Ok(val) => products = val,
Err(e) => return Err(error::ErrorBadRequest(e))
}
Expand Down
20 changes: 2 additions & 18 deletions src-tauri/src/routes/poke_card.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::models::pokemon::{Card, Expantion, Series};
use crate::utils::shared::sort_sql;
use crate::utils::sql_pokemon_data::{
card_count, card_search_sql, get_expansion, get_expansions, get_rarities, get_series,
get_series_list,
Expand Down Expand Up @@ -184,24 +185,7 @@ pub async fn card_search_helper(
let order = urlencoding::decode(search_params.sort.as_deref().unwrap_or_default())
.expect("UTF-8")
.to_string();
let sort: String;
if order.eq("name") {
sort = String::from("ORDER BY name ASC");
} else if order.eq("setNumber") {
sort = String::from("ORDER BY expCardNumber ASC");
} else if order.eq("pokedex") {
sort = String::from("ORDER BY pokedex ASC");
} else if order.eq("priceASC") {
sort = String::from("ORDER BY price ASC");
} else if order.eq("priceDSC") {
sort = String::from("ORDER BY price DESC");
} else if order.eq("dateASC") {
sort = String::from("ORDER BY datetime(releaseDate) ASC");
} else if order.eq("dateDSC") {
sort = String::from("ORDER BY datetime(releaseDate) DESC");
} else {
sort = String::from("ORDER BY datetime(releaseDate) DESC");
}
let sort: String = sort_sql(&order);
let count: i64;

match card_count(
Expand Down
14 changes: 2 additions & 12 deletions src-tauri/src/routes/poke_product.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::models::pokemon::SealedProduct;
use crate::{models::pokemon::SealedProduct, utils::shared::sort_sql};
use crate::utils::sql_pokemon_data;
use actix_web::{error, get, web, Responder, Result};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -32,17 +32,7 @@ pub async fn product_search(
let order = urlencoding::decode(search_params.sort.as_deref().unwrap_or_default())
.expect("UTF-8")
.to_string();
let sort: String;
if order.eq("name") {
sort = String::from("ORDER BY name ASC");
} else if order.eq("priceASC") {
sort = String::from("ORDER BY price ASC");
} else if order.eq("priceDSC") {
sort = String::from("ORDER BY price DESC");
} else {
sort = String::from("");
}

let sort: String = sort_sql(&order);
let count: i64;

match sql_pokemon_data::product_count(Some(name_filter.clone()), search_params.0.types.clone(), None) {
Expand Down
15 changes: 14 additions & 1 deletion src-tauri/src/utils/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ pub async fn download_file(url: &str, path: &str) -> Result<(), String> {
if res.status() == reqwest::StatusCode::NOT_FOUND {
return Err(String::from("404 error pulling data"));
} else {
let mut file = fs::File::create(path).or(Err(format!("Failed to create file '{}'", path)))?;
let mut file =
fs::File::create(path).or(Err(format!("Failed to create file '{}'", path)))?;
let mut stream = res.bytes_stream();
while let Some(item) = stream.next().await {
let chunk = item.or(Err(format!("Error while downloading file")))?;
Expand All @@ -79,6 +80,18 @@ pub async fn get_static_resources() {
}
}

pub fn sort_sql(query_param: &str) -> String {
match query_param {
"name" => return String::from("ORDER BY name ASC"),
"setNumber" => return String::from("ORDER BY expCardNumber ASC"),
"pokedex" => return String::from("ORDER BY pokedex ASC"),
"priceASC" => return String::from("ORDER BY price ASC"),
"priceDSC" => return String::from("ORDER BY price DESC"),
"dateASC" => return String::from("ORDER BY datetime(releaseDate) ASC"),
"dateDSC" => return String::from("ORDER BY datetime(releaseDate) DESC"),
_ => return String::from("ORDER BY datetime(releaseDate) DESC"),
}
}
/// SQL util to search for a value in a JSON array value uses json functions Many to Many
/// # Arguments
/// * col_name - name of the column that is a json list
Expand Down

0 comments on commit 383067d

Please sign in to comment.