working on rust side

This commit is contained in:
2019-02-13 16:01:24 +01:00
parent 8c89b9c059
commit 3ee9533faf
4 changed files with 108 additions and 9 deletions

View File

@@ -37,7 +37,7 @@ mod api {
pub struct CookbookDbConn(diesel::SqliteConnection);
/// A serializable wrapper for [`cookbook::recipes::Recipe`]
#[derive(Serialize)]
#[derive(Serialize, Deserialize, Debug)]
pub struct RecipeObject {
id: i32,
title: String,
@@ -76,13 +76,13 @@ mod api {
Json( recipes::delete(&conn, id) )
}
#[derive(Serialize)]
#[derive(Serialize, Deserialize, Debug)]
pub struct TemplateItems {
key: (String, String),
value: Option<RecipeObject>,
}
#[derive(Serialize)]
#[derive(Serialize, Deserialize)]
pub struct TemplateObject {
items: Vec<TemplateItems>
}
@@ -118,6 +118,65 @@ mod api {
panic!("No solution at all !");
}
}
#[post("/solver/complete", data="<partial>")]
pub fn complete_problem(conn: CookbookDbConn, partial: Json<Vec<TemplateItems>>) -> Json<TemplateObject> {
use planner::{
template,
solver::{Domain, Problem}
};
println!("{:?}", partial);
println!("Building problem");
let possible_values = recipes::load_all(&conn);
let domain = Domain::new(possible_values);
let mut problem = Problem::build();
for (var, dom, ini) in template::Template::generate_variables(&domain) {
// Let's hack for now
// BUGGY because template does not generate every variables, needs refactoring
// Find variable in partial
let initial_id = partial.iter().find_map(|slot| {
if slot.value.is_none() { return None; };
//println!("{:?} vs {:?}", slot, var);
if slot.key.0 == var.0
&& slot.key.1 == format!("{:?}",var.1)
{
let id = slot.value.as_ref().unwrap().id;
println!("found initial : recipe with id {}", id);
return Some(id);
} else {
return None;
}
});
let ini = if let Some(id) = initial_id {
let new_ini = domain.values.iter().find(|r| r.id == id);
println!("Overrided {:?}", new_ini);
new_ini
} else {
ini
};
// If found, override initial value
problem = problem.add_variable(var, dom, ini);
}
let mut problem = problem
.add_constraint(|_| true)
.finish();
if let Some(one_result) = problem.solve_one() {
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(),
})
} else {
panic!("No solution at all !");
}
}
}
fn main() -> Result<(), Error> {
@@ -137,7 +196,12 @@ fn main() -> Result<(), Error> {
rocket::ignite()
.attach(api::CookbookDbConn::fairing())
.mount("/", routes![index, files])
.mount("/api", routes![api::recipes_list, api::delete_recipe, api::one_solution])
.mount("/api", routes![
api::recipes_list,
api::delete_recipe,
api::one_solution,
api::complete_problem,
])
.attach(cors)
.launch();
Ok(())

View File

@@ -1,6 +1,6 @@
<template>
<div class="box">
<button @click="fetchSolution"
<button @click="fetchCompletion"
class="button is-primary is-fullwidth"
v-bind:class="{'is-loading': is_loading }">
FetchSolution</button>
@@ -109,7 +109,23 @@ export default {
return res.json();}
)
.then((data) => this.template.updateJson(data))
.catch((err) => console.log(err));
.catch((err) => console.error(err));
},
fetchCompletion: function() {
this.is_loading = true;
// TODO: Keep only value's id
let body = JSON.stringify(this.template.items);
fetch("http://localhost:8000/api/solver/complete", {
method: 'POST',
body,
})
.then((res) => {
this.is_loading = false;
return res.json();}
)
.then((data) => this.template.updateJson(data))
.catch((err) => console.error(err));
},
unsetMeal: function(mealKey) {
let idx = this.template.findIndexByKey(mealKey);