Skip to content

Commit

Permalink
Added search by country
Browse files Browse the repository at this point in the history
  • Loading branch information
0xCAB0 committed Jul 9, 2023
1 parent 4a227e2 commit ae2a8a4
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 24 deletions.
17 changes: 16 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ use bb8_postgres::{
use postgres_types::ToSql;
use thiserror::Error;

use crate::model::filter::{brand::Brand, category::Category};
use crate::model::filter::{brand::Brand, category::Category, country::Country};

const GET_BRAND_BY_NAME: &str = include_str!("sql_queries/GET_BRAND_BY_NAME.sql");
const GET_BRANDS_BY_NAME: &str = include_str!("sql_queries/GET_BRANDS_BY_NAME.sql");
const GET_CATEGORY_BY_NAME: &str = include_str!("sql_queries/GET_CATEGORY_BY_NAME.sql");
const GET_COUNTRY_BY_ISO_CODE: &str = include_str!("sql_queries/GET_COUNTRY_BY_ISO_CODE.sql");

/**
Represents an error that can occur during database operations.
Expand Down Expand Up @@ -122,4 +123,18 @@ where

Ok(cat)
}

/// Retrieves a category by its title from the database
pub async fn get_country_by_iso<S: AsRef<str> + Sync + ToSql + Display>(
&self,
code: &S,
) -> Result<Country, DbError> {
let conn = self.pool.get().await?;
let row: Row = conn.query_one(GET_COUNTRY_BY_ISO_CODE, &[&code]).await?;

// Works because From<Row> for Category is implemented
let country: Country = row.into();

Ok(country)
}
}
2 changes: 2 additions & 0 deletions src/model/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub struct Filter {
///
///Try it in [Regex 101](https://regex101.com/r/u8ZEpv/1)
///
///
/// **Note:** Color names are only avalible in French in our database at the moment
///### Example
///```rust
/// use vinted_rs::Filter;
Expand Down
12 changes: 6 additions & 6 deletions src/model/filter/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use typed_builder::TypedBuilder;

#[derive(Debug, Clone, TypedBuilder, PartialEq, Eq)]
pub struct Category {
id: i32,
pub id: i32,
// TODO creo que estos titulos solo estan en ingles
title: String,
code: String,
parent_id: i32,
url: String,
url_en: String,
pub title: String,
pub code: String,
pub parent_id: i32,
pub url: String,
pub url_en: String,
}

#[cfg(feature = "advanced_filters")]
Expand Down
6 changes: 3 additions & 3 deletions src/model/filter/category_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use typed_builder::TypedBuilder;

#[derive(Debug, Clone, TypedBuilder, PartialEq, Eq)]
pub struct CategoryTree {
id: i32,
parent_id: i32,
child_id: i32,
pub id: i32,
pub parent_id: i32,
pub child_id: i32,
}

#[cfg(feature = "advanced_filters")]
Expand Down
7 changes: 3 additions & 4 deletions src/model/filter/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use typed_builder::TypedBuilder;

#[derive(Debug, Clone, TypedBuilder, PartialEq, Eq)]
pub struct Color {
id: i32,
// TODO el titulo solo está en francés valorar que podemos hacer
title: String,
hex: String,
pub id: i32,
pub title: String,
pub hex: String,
}

#[cfg(feature = "advanced_filters")]
Expand Down
12 changes: 7 additions & 5 deletions src/model/filter/country.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ use bb8_postgres::tokio_postgres::Row;
use typed_builder::TypedBuilder;

#[derive(Debug, Clone, TypedBuilder, PartialEq, Eq)]
struct Country {
id: i32,
local_name: String,
iso_code: String,
flag: String,
pub struct Country {
pub id: i32,
pub name: String,
pub local_name: String,
pub iso_code: String,
pub flag: String,
}

#[cfg(feature = "advanced_filters")]
impl From<Row> for Country {
fn from(row: Row) -> Self {
Country::builder()
.id(row.get("id"))
.name(row.get("name"))
.local_name(row.get("local_name"))
.iso_code(row.get("iso_code"))
.flag(row.get("flag"))
Expand Down
8 changes: 4 additions & 4 deletions src/model/filter/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use typed_builder::TypedBuilder;

#[derive(Debug, Clone, TypedBuilder, PartialEq, Eq)]
pub struct Material {
id: i32,
material_es: String,
material_fr: String,
material_en: String,
pub id: i32,
pub material_es: String,
pub material_fr: String,
pub material_en: String,
}

#[cfg(feature = "advanced_filters")]
Expand Down
19 changes: 18 additions & 1 deletion src/tests/db.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
db::DbController,
model::filter::{brand::Brand, category::Category},
model::filter::{brand::Brand, category::Category, country::Country},
};
use bb8_postgres::tokio_postgres::NoTls;

Expand Down Expand Up @@ -56,3 +56,20 @@ async fn test_get_category_by_name() {
.build()
);
}

#[tokio::test]
async fn test_get_country_by_iso() {
let db: DbController<NoTls> = DbController::new(DB_URL, POOL_SIZE, NoTls).await.unwrap();
let c = db.get_country_by_iso(&String::from("ES")).await.unwrap();

assert_eq!(
c,
Country::builder()
.id(7)
.name(String::from("Espagne"))
.local_name(String::from("España"))
.iso_code(String::from("ES"))
.flag(String::from("🇪🇸"))
.build()
);
}
47 changes: 47 additions & 0 deletions src/tests/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,53 @@ async fn test_get_items_catalogs_no_db() {
match vinted.get_items(&filter, 10).await {
Ok(items) => {
assert_eq!(items.items.len(), 10);
//TODO: Try to test all are from the same catalog somehow
}
Err(err) => match err {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
},
};
}

#[tokio::test]
async fn test_get_items_by_price() {
let vinted = VintedWrapper::new();
let min = 50;
let max = 100;

let filter: Filter = Filter::builder().price_from(min).price_to(max).build();

match vinted.get_items(&filter, 10).await {
Ok(items) => {
assert_eq!(items.items.len(), 10);
let ok: bool = items.items.iter().all(|item| {
let price: f32 = item.price.parse().unwrap();
println!("{price}");
price <= max as f32 && price >= min as f32
});

assert!(ok);
}
Err(err) => match err {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
},
};
}

#[tokio::test]
async fn test_get_items_by_country() {
let vinted = VintedWrapper::new();
let db: DbController<NoTls> = DbController::new(DB_URL, POOL_SIZE, NoTls).await.unwrap();
let country = db.get_country_by_iso(&String::from("ES")).await.unwrap();

let filter: Filter = Filter::builder().countries_ids(country.id.to_string()).build();

match vinted.get_items(&filter, 20).await {
Ok(items) => {
assert_eq!(items.items.len(), 20);
//TODO: Try to test all are from the same country somehow
}
Err(err) => match err {
VintedWrapperError::ItemNumberError => unreachable!(),
Expand Down

0 comments on commit ae2a8a4

Please sign in to comment.