Adds Planner component

This commit is contained in:
2019-02-10 21:29:48 +01:00
parent 2f3993bb9e
commit ccb178ae5a
2 changed files with 71 additions and 4 deletions

View File

@@ -76,8 +76,19 @@ mod api {
Json( recipes::delete(&conn, id) )
}
#[derive(Serialize)]
pub struct TemplateItems {
key: (String, String),
value: Option<RecipeObject>,
}
#[derive(Serialize)]
pub struct TemplateObject {
items: Vec<TemplateItems>
}
#[get("/solver/one")]
pub fn one_solution(conn: CookbookDbConn) -> Json<String> {
pub fn one_solution(conn: CookbookDbConn) -> Json<TemplateObject> {
use planner::{
template,
solver::{Domain, Problem}
@@ -93,8 +104,18 @@ mod api {
.add_constraint(|_| true)
.finish();
let results = problem.solve_all();
let one_result = results.first().unwrap();
Json(format!("{:?}", results.first().unwrap()))
Json(TemplateObject {
items: one_result
.into_iter()
.map(|(k,v)| {
TemplateItems {
key: (format!("{}", k.0), format!("{:?}", k.1)),
value: v.map(|r| RecipeObject::from(&conn, r.clone())),
}})
.collect(),
})
}
}