Skip to content

Commit

Permalink
Converted vec<i32> to String
Browse files Browse the repository at this point in the history
Co-authored-by: Pmarquez <pxp9@users.noreply.github.com>
  • Loading branch information
0xCAB0 and pxp9 committed Jul 8, 2023
1 parent 68c1316 commit ae87f2f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
27 changes: 14 additions & 13 deletions src/model/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ pub struct Filter {
#[builder(default, setter(strip_option))]
pub search_text: Option<String>,
#[builder(default, setter(strip_option))]
pub catalog_ids: Option<Vec<i32>>,
pub catalog_ids: Option<String>,
#[builder(default, setter(strip_option))]
pub color_ids: Option<Vec<i32>>,
pub color_ids: Option<String>,
#[builder(default, setter(strip_option))]
pub brand_ids: Option<Vec<i32>>,
pub brand_ids: Option<String>,
#[builder(default, setter(strip_option))]
pub countries_ids: Option<Vec<i32>>,
pub countries_ids: Option<String>,
#[builder(default, setter(strip_option))]
pub size_ids: Option<Vec<i32>>,
pub size_ids: Option<String>,
#[builder(default, setter(strip_option))]
pub article_status: Option<Vec<ArticleStatus>>,
#[builder(default, setter(strip_option))]
Expand All @@ -43,14 +43,15 @@ pub enum ArticleStatus {
}

/// Para pasar de `ArticleStatus` a `i32` que es el id de vinted
impl From<ArticleStatus> for i32 {
fn from(status: ArticleStatus) -> Self {
match status {
ArticleStatus::NewTags => 6,
ArticleStatus::NewNoTags => 1,
ArticleStatus::VeryGood => 2,
ArticleStatus::Good => 3,
ArticleStatus::Satisfactory => 4,
///
impl From<&ArticleStatus> for &str {
fn from(status: &ArticleStatus) -> Self {
match *status {
ArticleStatus::NewTags => "6",
ArticleStatus::NewNoTags => "1",
ArticleStatus::VeryGood => "2",
ArticleStatus::Good => "3",
ArticleStatus::Satisfactory => "4",
}
}
}
Expand Down
34 changes: 16 additions & 18 deletions src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum VintedWrapperError {
ItemNumberError,
}

impl From<reqwest::Error> for VintedWrapperError{
impl From<reqwest::Error> for VintedWrapperError {
fn from(value: reqwest::Error) -> Self {
VintedWrapperError::CookiesError(CookieError::ReqWestError(value))
}
Expand Down Expand Up @@ -211,11 +211,7 @@ impl<'a> VintedWrapper<'a> {
/// }
/// }
/// ```
pub async fn get_items(
&self,
filters: &Filter,
num: u32,
) -> Result<Items, VintedWrapperError> {
pub async fn get_items(&self, filters: &Filter, num: u32) -> Result<Items, VintedWrapperError> {
if num == 0 {
return Err(VintedWrapperError::ItemNumberError);
}
Expand Down Expand Up @@ -246,37 +242,39 @@ impl<'a> VintedWrapper<'a> {
}

// Filtro catalogo
if let Some(vec) = &filters.catalog_ids {
let querify_vec: Vec<String> = vec.iter().map(|id| id.to_string()).collect();

let mut catalog_args: String = format!("&catalog_ids={}", querify_vec.join(","));
if let Some(catalog_ids) = &filters.catalog_ids {
let mut catalog_args: String = format!("&catalog_ids={}", catalog_ids);

VintedWrapper::substitute_if_first(&mut first, &mut catalog_args);

url = format!("{url}{catalog_args}");
}

// Filtro colores
if let Some(vec) = &filters.color_ids {
let querify_vec: Vec<String> = vec.iter().map(|id| id.to_string()).collect();

let mut color_args: String = format!("&color_ids={}", querify_vec.join(","));
if let Some(color_ids) = &filters.color_ids {
let mut color_args: String = format!("&color_ids={}", color_ids);

VintedWrapper::substitute_if_first(&mut first, &mut color_args);

url = format!("{url}{color_args}");
}

if let Some(vec) = &filters.brand_ids {
let querify_vec: Vec<String> = vec.iter().map(|id| id.to_string()).collect();

let mut brand_args: String = format!("&brand_ids={}", querify_vec.join(","));
if let Some(brand_ids) = &filters.brand_ids {
let mut brand_args: String = format!("&brand_ids={}", brand_ids);

VintedWrapper::substitute_if_first(&mut first, &mut brand_args);

url = format!("{url}{brand_args}");
}

if let Some(vec) = &filters.article_status {
let querify_vec: Vec<&str> = vec.into_iter().map(|status| status.into()).collect();

Check failure on line 271 in src/queries.rs

View workflow job for this annotation

GitHub Actions / Test

this `.into_iter()` call is equivalent to `.iter()` and will not consume the `Vec`

Check failure on line 271 in src/queries.rs

View workflow job for this annotation

GitHub Actions / Test

this `.into_iter()` call is equivalent to `.iter()` and will not consume the `Vec`

let mut article_status_args: String = format!("&status_ids={}", querify_vec.join(","));

VintedWrapper::substitute_if_first(&mut first, &mut article_status_args);
}

// TODO terminar de procesar los filtros

// Limitar el articulo a 1
Expand Down
8 changes: 4 additions & 4 deletions src/tests/queries.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::db::DbController;
use crate::model::filter::Filter;
use crate::queries::VintedWrapperError;
use crate::{VintedWrapper};
use crate::VintedWrapper;
use bb8_postgres::tokio_postgres::NoTls;

const DB_URL: &str = "postgres://postgres:postgres@localhost/vinted-rs";
Expand Down Expand Up @@ -31,7 +31,7 @@ async fn test_get_item_brands() {
let db: DbController<NoTls> = DbController::new(DB_URL, POOL_SIZE, NoTls).await.unwrap();
let brand = db.get_brand_by_name(&String::from("Adidas")).await.unwrap();

let filter: Filter = Filter::builder().brand_ids(vec![brand.id]).build();
let filter: Filter = Filter::builder().brand_ids(brand.id.to_string()).build();

match vinted.get_items(&filter, 1).await {
// Limitado el numero de elementos a 1
Expand All @@ -51,7 +51,7 @@ async fn test_get_items_brands() {
let db: DbController<NoTls> = DbController::new(DB_URL, POOL_SIZE, NoTls).await.unwrap();
let brand = db.get_brand_by_name(&String::from("Adidas")).await.unwrap();

let filter: Filter = Filter::builder().brand_ids(vec![brand.id]).build();
let filter: Filter = Filter::builder().brand_ids(brand.id.to_string()).build();

match vinted.get_items(&filter, 10).await {
Ok(items) => {
Expand All @@ -70,7 +70,7 @@ async fn test_get_items_brands() {
async fn test_get_items_catalogs_no_db() {
let vinted = VintedWrapper::new();
//Woman elements
let filter: Filter = Filter::builder().catalog_ids(vec![1904]).build();
let filter: Filter = Filter::builder().catalog_ids(String::from("1904")).build();
let substrings = vec![
"women", "mujer", "femme", "kobiety", "donna", "moterims", "noi",
];
Expand Down

0 comments on commit ae87f2f

Please sign in to comment.