reactive_graph_type_system_rest/
entities.rs

1use std::str::FromStr;
2use std::sync::Arc;
3
4use crate::json_schema_response;
5use actix_web::HttpResponse;
6use actix_web::get;
7use actix_web::web;
8use mime::APPLICATION_JSON;
9use reactive_graph_graph::EntityTypeId;
10use reactive_graph_type_system_api::EntityTypeManager;
11use reactive_graph_type_system_json_schema::entities::schema_entity_types;
12
13#[get("/types/entities")]
14pub async fn get_entity_types(entity_type_manager: web::Data<Arc<dyn EntityTypeManager + Send + Sync>>) -> HttpResponse {
15    HttpResponse::Ok()
16        .content_type(APPLICATION_JSON.to_string())
17        .json(entity_type_manager.get_all())
18}
19
20#[get("/types/entities/{namespace:.*}")]
21pub async fn get_entity_type(path: web::Path<String>, entity_type_manager: web::Data<Arc<dyn EntityTypeManager + Send + Sync>>) -> HttpResponse {
22    let namespace = path.into_inner();
23    let ty = match EntityTypeId::from_str(&namespace) {
24        Ok(ty) => ty,
25        Err(e) => {
26            return HttpResponse::NotFound().content_type(APPLICATION_JSON.to_string()).body(e.to_string());
27        }
28    };
29    match entity_type_manager.get(&ty) {
30        Some(entity_type) => HttpResponse::Ok().content_type(APPLICATION_JSON.to_string()).json(&entity_type),
31        None => HttpResponse::NotFound()
32            .content_type(APPLICATION_JSON.to_string())
33            .body(format!("Entity Type {ty} not found")),
34    }
35}
36
37#[get("/types/entities/schema")]
38pub async fn json_schema_entity_types() -> HttpResponse {
39    json_schema_response(schema_entity_types())
40}