From 0877cc590cd671916192495f0eb92cf1b83af811 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 17 Apr 2021 22:44:15 +0200 Subject: [PATCH] [#28] Moved db stuff to own module --- src/fej/ivago/db.rs | 12 ++++++++++++ src/fej/ivago/mod.rs | 34 +++++++++++++--------------------- src/server/main.rs | 2 +- src/server/routes/ivago.rs | 3 ++- 4 files changed, 28 insertions(+), 23 deletions(-) create mode 100644 src/fej/ivago/db.rs diff --git a/src/fej/ivago/db.rs b/src/fej/ivago/db.rs new file mode 100644 index 0000000..43f94e9 --- /dev/null +++ b/src/fej/ivago/db.rs @@ -0,0 +1,12 @@ +use super::Street; +use crate::errors::FejError; +use crate::schema::ivago_streets::dsl::*; +use diesel::prelude::*; +use diesel::PgConnection; + +pub fn search_streets(db_con: &PgConnection, search_term: &str) -> Result, FejError> { + Ok(ivago_streets + .filter(name.like(search_term)) + .or_filter(city.like(search_term)) + .load(db_con)?) +} diff --git a/src/fej/ivago/mod.rs b/src/fej/ivago/mod.rs index 5879cef..a6d8295 100644 --- a/src/fej/ivago/mod.rs +++ b/src/fej/ivago/mod.rs @@ -1,14 +1,12 @@ use crate::errors::FejError; -use crate::schema::ivago_streets::dsl::*; use chrono::DateTime; use chrono_tz::Tz; -use diesel::prelude::*; -use diesel::PgConnection; use reqwest::blocking as reqwest; use std::collections::HashMap; use std::convert::{From, TryFrom}; mod basic_date; +pub mod db; mod pickup_time; mod street; @@ -28,25 +26,19 @@ const CAL_URL: &str = "https://www.ivago.be/nl/particulier/garbage/pick-up/picku /// # Arguments /// /// * `search_term` - Search term to use to look for streets -// pub fn search_streets(search_term: &str) -> Result, FejError> { -// let client = reqwest::Client::new(); -// let response = client.get(SEARCH_URL).query(&[("q", search_term)]).send()?; -// let data: Vec> = response.json()?; +pub fn search_streets(search_term: &str) -> Result, FejError> { + let client = reqwest::Client::new(); + let response = client.get(SEARCH_URL).query(&[("q", search_term)]).send()?; + let data: Vec> = response.json()?; -// // This is pretty cool, filter_map first does get() on all the maps, and -// // then filters out any None values -// // Then, we do the same thing for streets -// Ok(data -// .iter() -// .filter_map(|m| m.get("value")) -// .filter_map(|v| Street::try_from(v.as_str()).ok()) -// .collect()) -// } -pub fn search_streets(db_con: &PgConnection, search_term: &str) -> Result, FejError> { - Ok(ivago_streets - .filter(name.like(search_term)) - .or_filter(city.like(search_term)) - .load(db_con)?) + // This is pretty cool, filter_map first does get() on all the maps, and + // then filters out any None values + // Then, we do the same thing for streets + Ok(data + .iter() + .filter_map(|m| m.get("value")) + .filter_map(|v| Street::try_from(v.as_str()).ok()) + .collect()) } /// Returns the pickup times for the various trash types. diff --git a/src/server/main.rs b/src/server/main.rs index aa9de11..0e84ff8 100644 --- a/src/server/main.rs +++ b/src/server/main.rs @@ -55,7 +55,7 @@ fn run_db_migrations(rocket: Rocket) -> Result { let conn = FejDbConn::get_one(&rocket).expect("database connection"); match embedded_migrations::run(&*conn) { Ok(()) => Ok(rocket), - Err(e) => Err(rocket), + Err(_) => Err(rocket), } } diff --git a/src/server/routes/ivago.rs b/src/server/routes/ivago.rs index a9b9336..e8c0a9e 100644 --- a/src/server/routes/ivago.rs +++ b/src/server/routes/ivago.rs @@ -1,5 +1,6 @@ use crate::FejDbConn; -use fej::ivago::{get_pickup_times, search_streets, BasicDate, PickupTime, Street}; +use fej::ivago::db::search_streets; +use fej::ivago::{get_pickup_times, BasicDate, PickupTime, Street}; use rocket::http::Status; use rocket_contrib::json::Json;