reactive_graph_type_system_rest/
relations.rs1use 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::RelationTypeId;
10use reactive_graph_type_system_api::RelationTypeManager;
11use reactive_graph_type_system_json_schema::relations::schema_relation_types;
12
13#[get("/types/relations")]
14pub async fn get_relation_types(relation_type_manager: web::Data<Arc<dyn RelationTypeManager + Send + Sync>>) -> HttpResponse {
15 HttpResponse::Ok()
16 .content_type(APPLICATION_JSON.to_string())
17 .json(relation_type_manager.get_all())
18}
19
20#[get("/types/relations/{namespace:.*}")]
21pub async fn get_relation_type(path: web::Path<String>, relation_type_manager: web::Data<Arc<dyn RelationTypeManager + Send + Sync>>) -> HttpResponse {
22 let namespace = path.into_inner();
23 let ty = match RelationTypeId::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 relation_type_manager.get(&ty) {
30 Some(relation_type) => HttpResponse::Ok().content_type(APPLICATION_JSON.to_string()).json(&relation_type),
31 None => HttpResponse::NotFound()
32 .content_type(APPLICATION_JSON.to_string())
33 .body(format!("Relation Type {ty} not found")),
34 }
35}
36
37#[get("/types/relations/schema")]
38pub async fn json_schema_relation_types() -> HttpResponse {
39 json_schema_response(schema_relation_types())
40}