diff --git a/diesel.toml b/diesel.toml index 92267c8..779714e 100644 --- a/diesel.toml +++ b/diesel.toml @@ -2,4 +2,4 @@ # see diesel.rs/guides/configuring-diesel-cli [print_schema] -file = "src/schema.rs" +file = "src/fej/schema.rs" diff --git a/migrations/2021-04-15-155511_ivago_search/down.sql b/migrations/2021-04-15-155511_ivago_search/down.sql index 9d9d004..a5beedb 100644 --- a/migrations/2021-04-15-155511_ivago_search/down.sql +++ b/migrations/2021-04-15-155511_ivago_search/down.sql @@ -1,2 +1,2 @@ -- This file should undo anything in `up.sql` -DROP SCHEMA ivago CASCADE; +DROP table ivago_streets; diff --git a/migrations/2021-04-15-155511_ivago_search/up.sql b/migrations/2021-04-15-155511_ivago_search/up.sql index f33eff1..d4928af 100644 --- a/migrations/2021-04-15-155511_ivago_search/up.sql +++ b/migrations/2021-04-15-155511_ivago_search/up.sql @@ -1,7 +1,4 @@ --- Your SQL goes here -CREATE SCHEMA ivago; - -CREATE TABLE ivago.streets ( +CREATE TABLE ivago_streets ( name TEXT NOT NULL, city TEXT NOT NULL, diff --git a/src/fej/errors.rs b/src/fej/errors.rs index 730714c..3174b92 100644 --- a/src/fej/errors.rs +++ b/src/fej/errors.rs @@ -8,6 +8,7 @@ use rocket::http::Status; pub enum FejError { InvalidArgument, FailedRequest, + DatabaseError, } // I'd love to move this over to the server binary, but right now, error E0117 is making that @@ -17,6 +18,7 @@ impl From for Status { match err { FejError::InvalidArgument => Status::BadRequest, FejError::FailedRequest => Status::InternalServerError, + FejError::DatabaseError => Status::InternalServerError, } } } @@ -33,3 +35,9 @@ impl From for FejError { FejError::InvalidArgument } } + +impl From for FejError { + fn from(_: diesel::result::Error) -> FejError { + FejError::DatabaseError + } +} diff --git a/src/fej/ivago/mod.rs b/src/fej/ivago/mod.rs index 7d3a9f9..5879cef 100644 --- a/src/fej/ivago/mod.rs +++ b/src/fej/ivago/mod.rs @@ -1,6 +1,9 @@ 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}; @@ -25,19 +28,25 @@ 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()) +// // 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)?) } /// Returns the pickup times for the various trash types. diff --git a/src/fej/ivago/street.rs b/src/fej/ivago/street.rs index 1a7cac9..a4da4b5 100644 --- a/src/fej/ivago/street.rs +++ b/src/fej/ivago/street.rs @@ -1,3 +1,4 @@ +use diesel::Queryable; use regex::Regex; use rocket::http::RawStr; use rocket::request::FromFormValue; @@ -5,6 +6,7 @@ use serde::ser::{Serialize, SerializeStruct, Serializer}; use std::convert::TryFrom; /// Represents a street in a given city +#[derive(Queryable)] pub struct Street { name: String, city: String, diff --git a/src/fej/lib.rs b/src/fej/lib.rs index 761c91b..f84d5d6 100644 --- a/src/fej/lib.rs +++ b/src/fej/lib.rs @@ -1,7 +1,10 @@ -#![feature(proc_macro_hygiene, decl_macro)] +#[macro_use] +extern crate diesel; // Route modules pub mod ivago; // Helper modules pub mod errors; + +pub mod schema; diff --git a/src/fej/schema.rs b/src/fej/schema.rs new file mode 100644 index 0000000..3fe23f2 --- /dev/null +++ b/src/fej/schema.rs @@ -0,0 +1,6 @@ +table! { + ivago_streets (name, city) { + name -> Text, + city -> Text, + } +} diff --git a/src/schema.rs b/src/schema.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/server/main.rs b/src/server/main.rs index 4267776..aa9de11 100644 --- a/src/server/main.rs +++ b/src/server/main.rs @@ -47,7 +47,7 @@ embed_migrations!(); // This defines a connection to the database #[database("postgres_fej")] -struct FejDbConn(diesel::PgConnection); +pub struct FejDbConn(diesel::PgConnection); // I'd like to thank Stackoverflow for helping me with this // https://stackoverflow.com/questions/61047355/how-to-run-diesel-migration-with-rocket-in-production diff --git a/src/server/routes/ivago.rs b/src/server/routes/ivago.rs index d8318bd..a9b9336 100644 --- a/src/server/routes/ivago.rs +++ b/src/server/routes/ivago.rs @@ -1,3 +1,4 @@ +use crate::FejDbConn; use fej::ivago::{get_pickup_times, search_streets, BasicDate, PickupTime, Street}; use rocket::http::Status; use rocket_contrib::json::Json; @@ -9,8 +10,8 @@ use rocket_contrib::json::Json; /// /// * `search_term` - Search term to use to look for streets #[get("/search?")] -pub fn route_search_streets(q: String) -> Result>, Status> { - Ok(Json(search_streets(q.as_str())?)) +pub fn route_search_streets(db_conn: FejDbConn, q: String) -> Result>, Status> { + Ok(Json(search_streets(&db_conn.0, q.as_str())?)) } /// Handles returning of pickup times for a specific address. It returns a list