This commit is contained in:
JMARyA 2024-09-03 11:16:45 +02:00
parent f7fac9aec8
commit d70dabc271
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 47 additions and 4 deletions

View file

@ -35,7 +35,6 @@ impl Validate for Location {
impl ToAPI for Location {
async fn api(&self) -> serde_json::Value {
json!({
"id": self._id,
"name": self.name,
"parent": self.parent,
"conditions": self.conditions

View file

@ -2,7 +2,7 @@ use std::ops::Deref;
use json_store::JSONStore;
use location::Location;
use mongod::Model;
use rocket::routes as route;
use rocket::{http::Method, launch};

View file

@ -1,3 +1,5 @@
use std::{collections::HashMap, ops::Deref};
use mongod::ToAPI;
use rocket::{get, State};
@ -18,9 +20,51 @@ pub async fn location_info(
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")]
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())
}