weih/web/handlers/
context_types.rs

1use crate::mlmd::context::{ContextTypeDetail, ContextTypeSummary};
2use crate::web::link::Link;
3use crate::web::{response, Config};
4use actix_web::{get, web, HttpResponse};
5
6#[get("/context_types/")]
7async fn get_context_type_summaries(config: web::Data<Config>) -> actix_web::Result<HttpResponse> {
8    let mut store = config.connect_metadata_store().await?;
9    let types = store
10        .get_context_types()
11        .execute()
12        .await
13        .map_err(actix_web::error::ErrorInternalServerError)?;
14
15    let mut md = concat!(
16        "# Context Types\n",
17        "\n",
18        "| id | name | properties |\n",
19        "|----|------|------------|\n"
20    )
21    .to_string();
22
23    for ty in types {
24        let ty = ContextTypeSummary::from(ty);
25        md += &format!(
26            "| {} | {} | {:?} |\n",
27            Link::ContextType(ty.id),
28            ty.name,
29            ty.properties
30        );
31    }
32
33    Ok(response::markdown(&md))
34}
35
36#[get("/context_types/{id}")]
37async fn get_context_type_detail(
38    config: web::Data<Config>,
39    path: web::Path<(i32,)>,
40) -> actix_web::Result<HttpResponse> {
41    let id = path.0;
42    let mut store = config.connect_metadata_store().await?;
43
44    let types = store
45        .get_context_types()
46        .id(mlmd::metadata::TypeId::new(id))
47        .execute()
48        .await
49        .map_err(actix_web::error::ErrorInternalServerError)?;
50    if types.is_empty() {
51        return Err(actix_web::error::ErrorNotFound(format!(
52            "no such context type: {}",
53            id
54        )));
55    }
56    let ty = ContextTypeDetail::from(types[0].clone());
57
58    let mut md = "# Context Type\n".to_string();
59
60    md += &format!("- ID: {}\n", ty.id);
61    md += &format!("- Name: {}\n", ty.name);
62    md += &format!("- Properties:\n");
63
64    for (k, v) in &ty.properties {
65        md += &format!("  - {}: {}\n", k, v);
66    }
67    md += &format!("- [Contexts](/contexts/?type={})\n", ty.name); // TODO: escape
68
69    Ok(response::markdown(&md))
70}