trees: don't crash on botanic_pfaf IS NULL
This commit is contained in:
parent
d29088ea69
commit
2a10109d7f
|
@ -70,39 +70,41 @@ pub fn get_tree(state: State) -> (State, Response<Body>) {
|
||||||
let app_state = AppState::borrow_from(&state);
|
let app_state = AppState::borrow_from(&state);
|
||||||
let result = {
|
let result = {
|
||||||
let mut db = app_state.db.lock().unwrap();
|
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()
|
.into_iter()
|
||||||
.next();
|
.next() {
|
||||||
if let Some(row) = row {
|
None => None,
|
||||||
let botanic_pfaf: &str = row.get(3);
|
Some(row) => {
|
||||||
let mut query = format!("SELECT ");
|
let botanic_pfaf: Option<String> = row.get(3);
|
||||||
for (i, col) in PFAF_COLS.iter().enumerate() {
|
botanic_pfaf.and_then(|botanic_pfaf| {
|
||||||
query = format!("{}{}\"{}\"::text", query, if i == 0 { "" } else { ", " }, col);
|
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 {
|
let res = match result {
|
||||||
|
|
Loading…
Reference in New Issue