use crate::backend::types::{City, Road, ShortestPath}; use crate::graph::graph::{all_cities, create_city, create_road, shortest_path}; use neo4rs::{Graph}; use rocket::response::status::{BadRequest}; use rocket::serde::json::Json; use rocket::{get, post, Route, State}; use rocket_okapi::{openapi, openapi_get_routes}; /// configure available api nodes pub fn build_api() -> Vec { openapi_get_routes![addcity, addroad, getshortestpath, getallcities] } /// endpoint to add new city to graph #[openapi] #[post("/addcity", data = "")] pub async fn addcity(graph: &State, msg: Json) -> Result> { match create_city(graph, msg.into_inner()).await { Ok(_) => Ok("ok".to_string()), Err(e) => Err(BadRequest(e.to_string())), } } /// endpoint to add new road between two cities #[openapi] #[post("/road", data = "")] pub async fn addroad(graph: &State, msg: Json) -> Result> { match create_road(graph, msg.into_inner()).await { Ok(_) => Ok("ok".to_string()), Err(e) => Err(BadRequest(e.to_string())), } } /// endpoint to get all available cities with optional limit #[openapi] #[get("/cities?")] pub async fn getallcities(graph: &State, limit: Option) -> Result>, BadRequest> { match all_cities(graph, limit).await { Ok(v) => Ok(Json::from(v)), Err(e) => Err(BadRequest(e.to_string())), } } /// endpoint to get shortest path between two cities with the resulting length #[openapi] #[get("/shortestpath?&")] pub async fn getshortestpath( graph: &State, city1: String, city2: String, ) -> Result, BadRequest> { match shortest_path(graph, city1, city2).await { Ok(v) => Ok(Json::from(v)), Err(e) => Err(BadRequest(e.to_string())), } }