Save favorite name and description in rust backend

This commit is contained in:
Reimar 2024-08-26 21:31:56 +02:00
parent eaf0a64d72
commit 6c294889c7
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
4 changed files with 25 additions and 4 deletions

View File

@ -1,3 +1,5 @@
DELETE FROM favorites;
ALTER TABLE favorites ADD COLUMN name TEXT NOT NULL; ALTER TABLE favorites ADD COLUMN name TEXT NOT NULL;
ALTER TABLE favorites ADD COLUMN description TEXT NOT NULL; ALTER TABLE favorites ADD COLUMN description TEXT NOT NULL;

View File

@ -49,7 +49,7 @@ fn get_authorized_user(req: &HttpRequest) -> Option<AuthorizedUser> {
return None; 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 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()?; let signature = URL_SAFE_NO_PAD.decode(jwt_parts.get(2).unwrap()).ok()?;

View File

@ -58,7 +58,7 @@ struct CreateFavoriteRequest {
lng: f64, lng: f64,
} }
#[derive(Deserialize)] #[derive(Deserialize, Debug)]
struct ReverseLookupResponse { struct ReverseLookupResponse {
name: String, name: String,
display_name: String, display_name: String,
@ -67,10 +67,25 @@ struct ReverseLookupResponse {
#[post("/favorites")] #[post("/favorites")]
async fn create_favorite(auth: AuthorizedUser, data: web::Data<AppData>, input: web::Json<CreateFavoriteRequest>) -> impl Responder { async fn create_favorite(auth: AuthorizedUser, data: web::Data<AppData>, input: web::Json<CreateFavoriteRequest>) -> impl Responder {
let db = data.database.lock().unwrap(); 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::<ReverseLookupResponse>();
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::<ReverseLookupResponse>().await
else { return HttpResponse::InternalServerError(); };
match db.execute( match db.execute(
"INSERT INTO favorites (user_id, lat, lng, name, description) VALUES (:user_id, :lat, :lng, :name, :description)", "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(), Ok(_) => HttpResponse::Created(),
Err(_) => HttpResponse::InternalServerError(), Err(_) => HttpResponse::InternalServerError(),

View File

@ -8,6 +8,8 @@ pub struct Favorite {
pub user_id: String, pub user_id: String,
pub lat: f64, pub lat: f64,
pub lng: f64, pub lng: f64,
pub name: String,
pub description: String,
} }
impl Favorite { impl Favorite {
@ -17,6 +19,8 @@ impl Favorite {
user_id: row.get("user_id")?, user_id: row.get("user_id")?,
lat: row.get("lat")?, lat: row.get("lat")?,
lng: row.get("lng")?, lng: row.get("lng")?,
name: row.get("name")?,
description: row.get("description")?,
}) })
} }
} }