work
This commit is contained in:
parent
f7fac9aec8
commit
d70dabc271
3 changed files with 47 additions and 4 deletions
|
@ -35,7 +35,6 @@ impl Validate for Location {
|
||||||
impl ToAPI for Location {
|
impl ToAPI for Location {
|
||||||
async fn api(&self) -> serde_json::Value {
|
async fn api(&self) -> serde_json::Value {
|
||||||
json!({
|
json!({
|
||||||
"id": self._id,
|
|
||||||
"name": self.name,
|
"name": self.name,
|
||||||
"parent": self.parent,
|
"parent": self.parent,
|
||||||
"conditions": self.conditions
|
"conditions": self.conditions
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::ops::Deref;
|
||||||
|
|
||||||
use json_store::JSONStore;
|
use json_store::JSONStore;
|
||||||
use location::Location;
|
use location::Location;
|
||||||
use mongod::Model;
|
|
||||||
use rocket::routes as route;
|
use rocket::routes as route;
|
||||||
use rocket::{http::Method, launch};
|
use rocket::{http::Method, launch};
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::{collections::HashMap, ops::Deref};
|
||||||
|
|
||||||
use mongod::ToAPI;
|
use mongod::ToAPI;
|
||||||
use rocket::{get, State};
|
use rocket::{get, State};
|
||||||
|
|
||||||
|
@ -18,9 +20,51 @@ pub async fn location_info(
|
||||||
Ok(loc.api().await)
|
Ok(loc.api().await)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Resolve locations children recursively
|
||||||
|
async fn location_api_resolve(
|
||||||
|
id: &str,
|
||||||
|
location: &Location,
|
||||||
|
locations: &HashMap<String, Location>,
|
||||||
|
) -> serde_json::Value {
|
||||||
|
let mut loc_api = location.api().await;
|
||||||
|
let mut sub_l = HashMap::new();
|
||||||
|
|
||||||
|
for sub_loc in locations {
|
||||||
|
if let Some(parent) = &sub_loc.1.parent {
|
||||||
|
if parent == id {
|
||||||
|
sub_l.insert(
|
||||||
|
sub_loc.0.clone(),
|
||||||
|
Box::pin(location_api_resolve(&sub_loc.0, &sub_loc.1, locations)).await,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loc_api
|
||||||
|
.as_object_mut()
|
||||||
|
.unwrap()
|
||||||
|
.insert("sub".to_string(), serde_json::to_value(sub_l).unwrap());
|
||||||
|
return loc_api;
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/locations")]
|
#[get("/locations")]
|
||||||
pub async fn locations_info(locations: &State<JSONStore<Location>>) -> FallibleApiResponse {
|
pub async fn locations_info(locations: &State<JSONStore<Location>>) -> FallibleApiResponse {
|
||||||
// todo : recursive location map
|
let mut root_locations = HashMap::new();
|
||||||
|
|
||||||
unimplemented!()
|
for loc in locations.inner().deref() {
|
||||||
|
if loc.1.parent.is_none() {
|
||||||
|
root_locations.insert(loc.0, loc.1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut location_api = HashMap::new();
|
||||||
|
|
||||||
|
for loc in &mut root_locations {
|
||||||
|
location_api.insert(
|
||||||
|
loc.0,
|
||||||
|
location_api_resolve(loc.0, loc.1, locations.inner()).await,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(serde_json::to_value(location_api).unwrap())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue