[#28] Moved db stuff to own module

pull/35/head
Jef Roosens 2021-04-17 22:44:15 +02:00
parent 91985a1710
commit 0877cc590c
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
4 changed files with 28 additions and 23 deletions

View File

@ -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<Vec<Street>, FejError> {
Ok(ivago_streets
.filter(name.like(search_term))
.or_filter(city.like(search_term))
.load(db_con)?)
}

View File

@ -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<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())
// }
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)?)
// 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.

View File

@ -55,7 +55,7 @@ fn run_db_migrations(rocket: Rocket) -> Result<Rocket, Rocket> {
let conn = FejDbConn::get_one(&rocket).expect("database connection");
match embedded_migrations::run(&*conn) {
Ok(()) => Ok(rocket),
Err(e) => Err(rocket),
Err(_) => Err(rocket),
}
}

View File

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