[#28] Switched search_streets to database
							parent
							
								
									95564e8111
								
							
						
					
					
						commit
						91985a1710
					
				|  | @ -2,4 +2,4 @@ | ||||||
| # see diesel.rs/guides/configuring-diesel-cli | # see diesel.rs/guides/configuring-diesel-cli | ||||||
| 
 | 
 | ||||||
| [print_schema] | [print_schema] | ||||||
| file = "src/schema.rs" | file = "src/fej/schema.rs" | ||||||
|  |  | ||||||
|  | @ -1,2 +1,2 @@ | ||||||
| -- This file should undo anything in `up.sql` | -- This file should undo anything in `up.sql` | ||||||
| DROP SCHEMA ivago CASCADE; | DROP table ivago_streets; | ||||||
|  |  | ||||||
|  | @ -1,7 +1,4 @@ | ||||||
| -- Your SQL goes here | CREATE TABLE ivago_streets ( | ||||||
| CREATE SCHEMA ivago; |  | ||||||
| 
 |  | ||||||
| CREATE TABLE ivago.streets ( |  | ||||||
|     name TEXT NOT NULL, |     name TEXT NOT NULL, | ||||||
|     city TEXT NOT NULL, |     city TEXT NOT NULL, | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -8,6 +8,7 @@ use rocket::http::Status; | ||||||
| pub enum FejError { | pub enum FejError { | ||||||
|     InvalidArgument, |     InvalidArgument, | ||||||
|     FailedRequest, |     FailedRequest, | ||||||
|  |     DatabaseError, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // I'd love to move this over to the server binary, but right now, error E0117 is making that
 | // 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<FejError> for Status { | ||||||
|         match err { |         match err { | ||||||
|             FejError::InvalidArgument => Status::BadRequest, |             FejError::InvalidArgument => Status::BadRequest, | ||||||
|             FejError::FailedRequest => Status::InternalServerError, |             FejError::FailedRequest => Status::InternalServerError, | ||||||
|  |             FejError::DatabaseError => Status::InternalServerError, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | @ -33,3 +35,9 @@ impl From<chrono::ParseError> for FejError { | ||||||
|         FejError::InvalidArgument |         FejError::InvalidArgument | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | impl From<diesel::result::Error> for FejError { | ||||||
|  |     fn from(_: diesel::result::Error) -> FejError { | ||||||
|  |         FejError::DatabaseError | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -1,6 +1,9 @@ | ||||||
| use crate::errors::FejError; | use crate::errors::FejError; | ||||||
|  | use crate::schema::ivago_streets::dsl::*; | ||||||
| use chrono::DateTime; | use chrono::DateTime; | ||||||
| use chrono_tz::Tz; | use chrono_tz::Tz; | ||||||
|  | use diesel::prelude::*; | ||||||
|  | use diesel::PgConnection; | ||||||
| use reqwest::blocking as reqwest; | use reqwest::blocking as reqwest; | ||||||
| use std::collections::HashMap; | use std::collections::HashMap; | ||||||
| use std::convert::{From, TryFrom}; | use std::convert::{From, TryFrom}; | ||||||
|  | @ -25,19 +28,25 @@ const CAL_URL: &str = "https://www.ivago.be/nl/particulier/garbage/pick-up/picku | ||||||
| /// # Arguments
 | /// # Arguments
 | ||||||
| ///
 | ///
 | ||||||
| /// * `search_term` - Search term to use to look for streets
 | /// * `search_term` - Search term to use to look for streets
 | ||||||
| pub fn search_streets(search_term: &str) -> Result<Vec<Street>, FejError> { | // pub fn search_streets(search_term: &str) -> Result<Vec<Street>, FejError> {
 | ||||||
|     let client = reqwest::Client::new(); | //     let client = reqwest::Client::new();
 | ||||||
|     let response = client.get(SEARCH_URL).query(&[("q", search_term)]).send()?; | //     let response = client.get(SEARCH_URL).query(&[("q", search_term)]).send()?;
 | ||||||
|     let data: Vec<HashMap<String, String>> = response.json()?; | //     let data: Vec<HashMap<String, String>> = response.json()?;
 | ||||||
| 
 | 
 | ||||||
|     // This is pretty cool, filter_map first does get() on all the maps, and
 | //     // This is pretty cool, filter_map first does get() on all the maps, and
 | ||||||
|     // then filters out any None values
 | //     // then filters out any None values
 | ||||||
|     // Then, we do the same thing for streets
 | //     // Then, we do the same thing for streets
 | ||||||
|     Ok(data | //     Ok(data
 | ||||||
|         .iter() | //         .iter()
 | ||||||
|         .filter_map(|m| m.get("value")) | //         .filter_map(|m| m.get("value"))
 | ||||||
|         .filter_map(|v| Street::try_from(v.as_str()).ok()) | //         .filter_map(|v| Street::try_from(v.as_str()).ok())
 | ||||||
|         .collect()) | //         .collect())
 | ||||||
|  | // }
 | ||||||
|  | pub fn search_streets(db_con: &PgConnection, search_term: &str) -> Result<Vec<Street>, 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.
 | /// Returns the pickup times for the various trash types.
 | ||||||
|  |  | ||||||
|  | @ -1,3 +1,4 @@ | ||||||
|  | use diesel::Queryable; | ||||||
| use regex::Regex; | use regex::Regex; | ||||||
| use rocket::http::RawStr; | use rocket::http::RawStr; | ||||||
| use rocket::request::FromFormValue; | use rocket::request::FromFormValue; | ||||||
|  | @ -5,6 +6,7 @@ use serde::ser::{Serialize, SerializeStruct, Serializer}; | ||||||
| use std::convert::TryFrom; | use std::convert::TryFrom; | ||||||
| 
 | 
 | ||||||
| /// Represents a street in a given city
 | /// Represents a street in a given city
 | ||||||
|  | #[derive(Queryable)] | ||||||
| pub struct Street { | pub struct Street { | ||||||
|     name: String, |     name: String, | ||||||
|     city: String, |     city: String, | ||||||
|  |  | ||||||
|  | @ -1,7 +1,10 @@ | ||||||
| #![feature(proc_macro_hygiene, decl_macro)] | #[macro_use] | ||||||
|  | extern crate diesel; | ||||||
| 
 | 
 | ||||||
| // Route modules
 | // Route modules
 | ||||||
| pub mod ivago; | pub mod ivago; | ||||||
| 
 | 
 | ||||||
| // Helper modules
 | // Helper modules
 | ||||||
| pub mod errors; | pub mod errors; | ||||||
|  | 
 | ||||||
|  | pub mod schema; | ||||||
|  |  | ||||||
|  | @ -0,0 +1,6 @@ | ||||||
|  | table! { | ||||||
|  |     ivago_streets (name, city) { | ||||||
|  |         name -> Text, | ||||||
|  |         city -> Text, | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -47,7 +47,7 @@ embed_migrations!(); | ||||||
| 
 | 
 | ||||||
| // This defines a connection to the database
 | // This defines a connection to the database
 | ||||||
| #[database("postgres_fej")] | #[database("postgres_fej")] | ||||||
| struct FejDbConn(diesel::PgConnection); | pub struct FejDbConn(diesel::PgConnection); | ||||||
| 
 | 
 | ||||||
| // I'd like to thank Stackoverflow for helping me with this
 | // 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
 | // https://stackoverflow.com/questions/61047355/how-to-run-diesel-migration-with-rocket-in-production
 | ||||||
|  |  | ||||||
|  | @ -1,3 +1,4 @@ | ||||||
|  | use crate::FejDbConn; | ||||||
| use fej::ivago::{get_pickup_times, search_streets, BasicDate, PickupTime, Street}; | use fej::ivago::{get_pickup_times, search_streets, BasicDate, PickupTime, Street}; | ||||||
| use rocket::http::Status; | use rocket::http::Status; | ||||||
| use rocket_contrib::json::Json; | use rocket_contrib::json::Json; | ||||||
|  | @ -9,8 +10,8 @@ use rocket_contrib::json::Json; | ||||||
| ///
 | ///
 | ||||||
| /// * `search_term` - Search term to use to look for streets
 | /// * `search_term` - Search term to use to look for streets
 | ||||||
| #[get("/search?<q>")] | #[get("/search?<q>")] | ||||||
| pub fn route_search_streets(q: String) -> Result<Json<Vec<Street>>, Status> { | pub fn route_search_streets(db_conn: FejDbConn, q: String) -> Result<Json<Vec<Street>>, Status> { | ||||||
|     Ok(Json(search_streets(q.as_str())?)) |     Ok(Json(search_streets(&db_conn.0, q.as_str())?)) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /// Handles returning of pickup times for a specific address. It returns a list
 | /// Handles returning of pickup times for a specific address. It returns a list
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue