trees: don't crash on botanic_pfaf IS NULL

This commit is contained in:
Astro 2021-08-26 16:45:06 +02:00
parent d29088ea69
commit 2a10109d7f
1 changed files with 33 additions and 31 deletions

View File

@ -70,39 +70,41 @@ pub fn get_tree(state: State) -> (State, Response<Body>) {
let app_state = AppState::borrow_from(&state);
let result = {
let mut db = app_state.db.lock().unwrap();
let row = db.query("SELECT id, coord, botanic, botanic_pfaf, german, planted FROM trees WHERE id=$1 LIMIT 1", &[&ie.id]).unwrap()
match db.query("SELECT id, coord, botanic, botanic_pfaf, german, planted FROM trees WHERE id=$1 LIMIT 1", &[&ie.id]).unwrap()
.into_iter()
.next();
if let Some(row) = row {
let botanic_pfaf: &str = row.get(3);
let mut query = format!("SELECT ");
for (i, col) in PFAF_COLS.iter().enumerate() {
query = format!("{}{}\"{}\"::text", query, if i == 0 { "" } else { ", " }, col);
.next() {
None => None,
Some(row) => {
let botanic_pfaf: Option<String> = row.get(3);
botanic_pfaf.and_then(|botanic_pfaf| {
let mut query = format!("SELECT ");
for (i, col) in PFAF_COLS.iter().enumerate() {
query = format!("{}{}\"{}\"::text", query, if i == 0 { "" } else { ", " }, col);
}
query = format!("{} FROM \"PlantsForAFuture\" WHERE \"Latin name\"=$1 LIMIT 1", query);
db.query(query.as_str(), &[&botanic_pfaf]).unwrap()
.into_iter()
.next()
}).map(|pfaf_row| {
let mut details: HashMap<String, String> = HashMap::with_capacity(PFAF_COLS.len());
for (i, col) in PFAF_COLS.iter().enumerate() {
if let Some(value) = pfaf_row.get(i) {
details.insert(col.to_string(), value);
}
}
let point: Point<f64> = row.get(1);
Tree {
id: row.get(0),
coords: [point.x(), point.y()],
botanic: row.get(2),
german: row.get(4),
details: Some(details),
planted: row.get(5),
}
})
}
}
query = format!("{} FROM \"PlantsForAFuture\" WHERE \"Latin name\"=$1 LIMIT 1", query);
let pfaf_row = db.query(query.as_str(), &[&botanic_pfaf]).unwrap()
.into_iter()
.next();
pfaf_row.map(|pfaf_row| {
let point: Point<f64> = row.get(1);
let mut details: HashMap<String, String> = HashMap::with_capacity(PFAF_COLS.len());
for (i, col) in PFAF_COLS.iter().enumerate() {
if let Some(value) = pfaf_row.get(i) {
details.insert(col.to_string(), value);
}
}
Tree {
id: row.get(0),
coords: [point.x(), point.y()],
botanic: row.get(2),
german: row.get(4),
details: Some(details),
planted: row.get(5),
}
})
} else {
None
}
};
let res = match result {