[#28] Switched search_streets to database

pull/35/head
Jef Roosens 2021-04-17 22:39:22 +02:00
parent 95564e8111
commit 91985a1710
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
11 changed files with 48 additions and 22 deletions

View File

@ -2,4 +2,4 @@
# see diesel.rs/guides/configuring-diesel-cli
[print_schema]
file = "src/schema.rs"
file = "src/fej/schema.rs"

View File

@ -1,2 +1,2 @@
-- This file should undo anything in `up.sql`
DROP SCHEMA ivago CASCADE;
DROP table ivago_streets;

View File

@ -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,

View File

@ -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<FejError> for Status {
match err {
FejError::InvalidArgument => Status::BadRequest,
FejError::FailedRequest => Status::InternalServerError,
FejError::DatabaseError => Status::InternalServerError,
}
}
}
@ -33,3 +35,9 @@ impl From<chrono::ParseError> for FejError {
FejError::InvalidArgument
}
}
impl From<diesel::result::Error> for FejError {
fn from(_: diesel::result::Error) -> FejError {
FejError::DatabaseError
}
}

View File

@ -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<Vec<Street>, FejError> {
let client = reqwest::Client::new();
let response = client.get(SEARCH_URL).query(&[("q", search_term)]).send()?;
let data: Vec<HashMap<String, String>> = response.json()?;
// pub fn search_streets(search_term: &str) -> Result<Vec<Street>, FejError> {
// let client = reqwest::Client::new();
// let response = client.get(SEARCH_URL).query(&[("q", search_term)]).send()?;
// let data: Vec<HashMap<String, String>> = 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<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.

View File

@ -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,

View File

@ -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;

View File

@ -0,0 +1,6 @@
table! {
ivago_streets (name, city) {
name -> Text,
city -> Text,
}
}

View File

View File

@ -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

View File

@ -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?<q>")]
pub fn route_search_streets(q: String) -> Result<Json<Vec<Street>>, Status> {
Ok(Json(search_streets(q.as_str())?))
pub fn route_search_streets(db_conn: FejDbConn, q: String) -> Result<Json<Vec<Street>>, Status> {
Ok(Json(search_streets(&db_conn.0, q.as_str())?))
}
/// Handles returning of pickup times for a specific address. It returns a list