diff --git a/rust-backend/migrations/V2__add_name_and_description_to_favorites_table.sql b/rust-backend/migrations/V2__add_name_and_description_to_favorites_table.sql index f1d8a7f..b33201d 100644 --- a/rust-backend/migrations/V2__add_name_and_description_to_favorites_table.sql +++ b/rust-backend/migrations/V2__add_name_and_description_to_favorites_table.sql @@ -1,3 +1,5 @@ +DELETE FROM favorites; + ALTER TABLE favorites ADD COLUMN name TEXT NOT NULL; ALTER TABLE favorites ADD COLUMN description TEXT NOT NULL; diff --git a/rust-backend/src/auth.rs b/rust-backend/src/auth.rs index 3f09a72..eb2e6fe 100644 --- a/rust-backend/src/auth.rs +++ b/rust-backend/src/auth.rs @@ -49,7 +49,7 @@ fn get_authorized_user(req: &HttpRequest) -> Option { return None; } - let header: Value = serde_json::from_slice(&URL_SAFE_NO_PAD.decode(jwt_parts.get(0).unwrap()).ok()?).ok()?; + let _header: Value = serde_json::from_slice(&URL_SAFE_NO_PAD.decode(jwt_parts.get(0).unwrap()).ok()?).ok()?; let payload: Value = serde_json::from_slice(&URL_SAFE_NO_PAD.decode(jwt_parts.get(1).unwrap()).ok()?).ok()?; let signature = URL_SAFE_NO_PAD.decode(jwt_parts.get(2).unwrap()).ok()?; diff --git a/rust-backend/src/main.rs b/rust-backend/src/main.rs index 55ad114..8367888 100644 --- a/rust-backend/src/main.rs +++ b/rust-backend/src/main.rs @@ -58,7 +58,7 @@ struct CreateFavoriteRequest { lng: f64, } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] struct ReverseLookupResponse { name: String, display_name: String, @@ -67,10 +67,25 @@ struct ReverseLookupResponse { #[post("/favorites")] async fn create_favorite(auth: AuthorizedUser, data: web::Data, input: web::Json) -> impl Responder { let db = data.database.lock().unwrap(); - let body = reqwest::blocking::get("https://nominatim.openstreetmap.org/reverse.php?lat=54.951489&lon=9.703438&zoom=18&format=jsonv2").json::(); + + let Ok(response) = reqwest::Client::new() + .get("https://nominatim.openstreetmap.org/reverse.php?lat=54.951489&lon=9.703438&zoom=18&format=jsonv2") + .header("User-Agent", "SkanTravels/1.0") + .send().await + else { return HttpResponse::InternalServerError(); }; + + let Ok(response) = response.json::().await + else { return HttpResponse::InternalServerError(); }; + match db.execute( "INSERT INTO favorites (user_id, lat, lng, name, description) VALUES (:user_id, :lat, :lng, :name, :description)", - &[(":user_id", &auth.user_id), (":lat", &input.lat.to_string()), (":lng", &input.lng.to_string())] + &[ + (":user_id", &auth.user_id), + (":lat", &input.lat.to_string()), + (":lng", &input.lng.to_string()), + (":name", &response.display_name), + (":description", &response.name), + ], ) { Ok(_) => HttpResponse::Created(), Err(_) => HttpResponse::InternalServerError(), diff --git a/rust-backend/src/models.rs b/rust-backend/src/models.rs index f266397..a9fa37d 100644 --- a/rust-backend/src/models.rs +++ b/rust-backend/src/models.rs @@ -8,6 +8,8 @@ pub struct Favorite { pub user_id: String, pub lat: f64, pub lng: f64, + pub name: String, + pub description: String, } impl Favorite { @@ -17,6 +19,8 @@ impl Favorite { user_id: row.get("user_id")?, lat: row.get("lat")?, lng: row.get("lng")?, + name: row.get("name")?, + description: row.get("description")?, }) } }