Add handler for ListGridSchemas
Adds a message, ListGridSchemas, and a handler for this message
to be used to list all grid schemas from the reporting database.
// See the License for the specific language governing permissions and
// limitations under the License.
-use crate::database::models::{GridPropertyDefinition, GridSchema};
+use crate::database::{
+ helpers as db,
+ models::{GridPropertyDefinition, GridSchema},
+};
+use crate::rest_api::{error::RestApiResponseError, routes::DbExecutor};
+use actix::{Handler, Message, SyncContext};
use serde::{Deserialize, Serialize};
+use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize)]
pub struct GridSchemaSlice {
@@ -64,3 +70,35 @@ impl GridPropertyDefinitionSlice {
}
}
}
+
+struct ListGridSchemas;
+
+impl Message for ListGridSchemas {
+ type Result = Result<Vec<GridSchemaSlice>, RestApiResponseError>;
+}
+
+impl Handler<ListGridSchemas> for DbExecutor {
+ type Result = Result<Vec<GridSchemaSlice>, RestApiResponseError>;
+
+ fn handle(&mut self, _msg: ListGridSchemas, _: &mut SyncContext<Self>) -> Self::Result {
+ let mut properties = db::list_grid_property_definitions(&*self.connection_pool.get()?)?
+ .into_iter()
+ .fold(HashMap::new(), |mut acc, definition| {
+ acc.entry(definition.schema_name.to_string())
+ .or_insert_with(|| vec![])
+ .push(definition);
+ acc
+ });
+
+ let fetched_schemas = db::list_grid_schemas(&*self.connection_pool.get()?)?
+ .iter()
+ .map(|schema| {
+ GridSchemaSlice::from_schema(
+ schema,
+ properties.remove(&schema.name).unwrap_or_else(|| vec![]),
+ )
+ })
+ .collect();
+ Ok(fetched_schemas)
+ }
+}
