Compare commits
2 Commits
e3f134a9bf
...
27a61f8a9a
Author | SHA1 | Date |
---|---|---|
Jef Roosens | 27a61f8a9a | |
Jef Roosens | e3174f21af |
|
@ -1,5 +1,7 @@
|
|||
use super::search::Street;
|
||||
use chrono::NaiveDate;
|
||||
use rocket::http::RawStr;
|
||||
use rocket::request::FromFormValue;
|
||||
use std::error::Error;
|
||||
|
||||
const BASE_URL: &str = "https://www.ivago.be/nl/particulier/afval/ophaling";
|
||||
|
@ -13,6 +15,17 @@ pub struct PickupTime {
|
|||
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(
|
||||
street: Street,
|
||||
number: u64,
|
||||
|
|
|
@ -3,6 +3,7 @@ mod controller;
|
|||
mod tests;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use rocket::http::Status;
|
||||
use rocket_contrib::json::Json;
|
||||
|
||||
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
|
||||
// TODO make this async
|
||||
// TODO change this so it can return errors instead of empty json
|
||||
#[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) {
|
||||
Ok(streets) => Json(streets),
|
||||
Err(err) => Json(Vec::new()),
|
||||
Ok(streets) => Ok(Json(streets)),
|
||||
Err(err) => Err(Status::InternalServerError),
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/?<street>&<number>&<start_date>&<end_date>")]
|
||||
pub fn route_get_pickup_times(
|
||||
street: controller::Street,
|
||||
number: u64,
|
||||
number: u32,
|
||||
start_date: NaiveDate,
|
||||
end_date: NaiveDate,
|
||||
) -> Json<Vec<controller::PickupTime>> {
|
||||
) -> Result<Json<Vec<controller::PickupTime>>, Status> {
|
||||
Err(Status::InternalServerError)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue