Compare commits

..

2 Commits

2 changed files with 20 additions and 7 deletions

View File

@ -1,5 +1,7 @@
use super::search::Street; use super::search::Street;
use chrono::NaiveDate; use chrono::NaiveDate;
use rocket::http::RawStr;
use rocket::request::FromFormValue;
use std::error::Error; use std::error::Error;
const BASE_URL: &str = "https://www.ivago.be/nl/particulier/afval/ophaling"; const BASE_URL: &str = "https://www.ivago.be/nl/particulier/afval/ophaling";
@ -13,6 +15,17 @@ pub struct PickupTime {
url: String, url: String,
} }
impl<'v> FromFormValue<'v> for NaiveDate {
type Error = &'v RawStr;
fn from_form_value(form_value: &'v RawStr) -> Result<NaiveDate, &'v RawStr> {
match NaiveDate::parse_from_str(form_value, "%Y-%m-%d") {
Ok(date) => Ok(date),
Err(_) => Err(form_value),
}
}
}
pub fn get_pickup_times( pub fn get_pickup_times(
street: Street, street: Street,
number: u64, number: u64,

View File

@ -3,6 +3,7 @@ mod controller;
mod tests; mod tests;
use chrono::NaiveDate; use chrono::NaiveDate;
use rocket::http::Status;
use rocket_contrib::json::Json; use rocket_contrib::json::Json;
pub fn routes() -> Vec<rocket::Route> { pub fn routes() -> Vec<rocket::Route> {
@ -10,21 +11,20 @@ pub fn routes() -> Vec<rocket::Route> {
} }
// URL: https://www.ivago.be/nl/particulier/autocomplete/garbage/streets?q=Lange // URL: https://www.ivago.be/nl/particulier/autocomplete/garbage/streets?q=Lange
// TODO make this async
// TODO change this so it can return errors instead of empty json
#[get("/search?<street>")] #[get("/search?<street>")]
pub fn search_streets_json(street: String) -> Json<Vec<controller::Street>> { pub fn route_search_streets(street: String) -> Result<Json<Vec<controller::Street>>, Status> {
match controller::search_streets(&street) { match controller::search_streets(&street) {
Ok(streets) => Json(streets), Ok(streets) => Ok(Json(streets)),
Err(err) => Json(Vec::new()), Err(err) => Err(Status::InternalServerError),
} }
} }
#[get("/?<street>&<number>&<start_date>&<end_date>")] #[get("/?<street>&<number>&<start_date>&<end_date>")]
pub fn route_get_pickup_times( pub fn route_get_pickup_times(
street: controller::Street, street: controller::Street,
number: u64, number: u32,
start_date: NaiveDate, start_date: NaiveDate,
end_date: NaiveDate, end_date: NaiveDate,
) -> Json<Vec<controller::PickupTime>> { ) -> Result<Json<Vec<controller::PickupTime>>, Status> {
Err(Status::InternalServerError)
} }