adds Key generic parameters, use custom Key type from template.rs

This commit is contained in:
2019-02-10 14:43:18 +01:00
parent 29b2f076bf
commit 2f3993bb9e
11 changed files with 241 additions and 279 deletions

View File

@@ -8,6 +8,7 @@ edition = "2018"
rocket = "0.4.0"
rocket_cors = { git = "https://github.com/lawliet89/rocket_cors", branch = "master" }
cookbook = { path = "../cookbook/" }
planner = { path = "../planner/" }
serde = "1.0"
serde_derive = "1.0"

View File

@@ -1,112 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello Bulma!</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.2/css/bulma.min.css">
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.22/dist/vue.js"></script>
</head>
<body>
<div id="app">
<div class="hero hero-body">
<h1 class="title">Cook Assistant</h1>
<h2 class="subtitle">Recettes</h2>
</div>
<!-- Details View -->
<section v-if="active_view > -1" class="section has-background-grey-lighter">
<div class="box">
<button @click="closeActiveView" class="button is-pulled-right">X close</button>
<h4 class="title">{{ items[active_view].title }}</h4>
<h6 class="subtitle">{{ categories[items[active_view].category].name }}</h6>
<p><strong>Ingredients : </strong></p>
<ul>
<li v-for="ing in items[active_view].ingredients.split('\n')">{{ ing }}</li>
</ul>
<button @click="deleteRecipe(items[active_view].id)" class="button is-danger is-pulled-right">DELETE !</button>
</div>
</section>
<!-- Category List View -->
<section v-else class="section has-background-grey-lighter">
<div class="container">
<div v-if="active_category == -1" class="columns">
<div v-for="c in categories" :key="c.id" class="column">
<button @click="setActiveCategory(c.id)" class="button is-large is-primary has-text-dark">{{ c.name }}</button>
</div>
</div>
<div v-else class="box">
<button @click="setActiveCategory(-1)" class="button is-pulled-left"><< back</button>
<h2 class="subtitle">{{ categories[active_category].name }}</h2>
<ul>
<li v-for="item in displayed" :key="item.id">
<a href="" @click.prevent="setActiveView(items.findIndex((i) => i.id ==item.id))">{{ item.title }}</a>
</li>
</ul>
</div>
</div>
</section>
</div>
</body>
<script type="text/javascript">
// TODO: Must find a viable use of Recipe.id instead of active_view !
var app = new Vue({
el: '#app',
data () {
return {
categories: [
{id: 0, name: "Petit-déjeuner"},
{id: 1, name: "Entrée"},
{id: 2, name: "Plat principal"},
{id: 3, name: "Dessert"}
],
active_category: -1,
active_view: -1,
items: []
};
},
methods: {
setActiveCategory: function(id) {
this.active_category = id;
},
setActiveView: function(idx) {
this.active_view = idx;
},
closeActiveView: function() {
this.active_view = -1;
},
deleteRecipe: function(id) {
fetch("/api/delete/" + id)
.then((res) => res.json())
.then((data) => {
if (data === true) {
this.items.splice(this.active_view, 1);
this.closeActiveView();
console.log("Deleted :" + data);
} else {
console.log("Error deleting");
}})
.catch((err) => console.error(err));
},
fetchRecipesList: function() {
fetch("/api/list")
.then((res) => res.json())
.then((data) => this.items = data)
.catch(function(err) {
console.error(err);
});
}
},
computed: {
displayed: function() {
return this.items.filter(
rec => rec.category == this.active_category
);
}
},
mounted () {
this.fetchRecipesList()
}
});
</script>
</html>

View File

@@ -5,8 +5,9 @@
#[macro_use] extern crate serde_derive;
extern crate rocket_cors;
extern crate cookbook;
extern crate planner;
use std::path::Path;
use std::path::{Path, PathBuf};
use rocket::{
response::{NamedFile, status::NotFound},
http::Method,
@@ -15,8 +16,14 @@ use rocket_cors::{AllowedHeaders, AllowedOrigins, Error};
#[get("/")]
fn index() -> Result<NamedFile, NotFound<String>> {
NamedFile::open(&Path::new("./html/index.html"))
.map_err(|_| NotFound(format!("Server error : index not found")))
files(PathBuf::from("index.html"))
}
#[get("/<file..>", rank=6)]
fn files(file: PathBuf) -> Result<NamedFile, NotFound<String>> {
let path = Path::new("vue/dist/").join(file);
NamedFile::open(&path)
.map_err(|_| NotFound(format!("Bad path: {:?}", path)))
}
mod api {
@@ -68,6 +75,27 @@ mod api {
pub fn delete_recipe(conn: CookbookDbConn, id: i32) -> Json<bool> {
Json( recipes::delete(&conn, id) )
}
#[get("/solver/one")]
pub fn one_solution(conn: CookbookDbConn) -> Json<String> {
use planner::{
template,
solver::{Domain, 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) {
problem = problem.add_variable(var, dom, ini);
}
let mut problem = problem
.add_constraint(|_| true)
.finish();
let results = problem.solve_all();
Json(format!("{:?}", results.first().unwrap()))
}
}
fn main() -> Result<(), Error> {
@@ -86,8 +114,8 @@ fn main() -> Result<(), Error> {
rocket::ignite()
.attach(api::CookbookDbConn::fairing())
.mount("/", routes![index])
.mount("/api", routes![api::recipes_list, api::delete_recipe])
.mount("/", routes![index, files])
.mount("/api", routes![api::recipes_list, api::delete_recipe, api::one_solution])
.attach(cors)
.launch();
Ok(())

View File

@@ -1,10 +1,9 @@
<template>
<div id="app">
<section class="section has-background-primary" id="heading">
<Heading msg="Cook-Assistant"/>
</section>
<Heading msg="Cook-Assistant"/>
<section class="section" id="recipes-view">
<p v-if="is_loading">Loading...</p>
<p v-if="status.loading">Loading...</p>
<div v-if="status.error">Error</div>
<h2 v-else class="subtitle">Livre de recettes</h2>
<div class="container">
<keep-alive>
@@ -19,6 +18,7 @@
</section>
<section class="section has-background-grey-lighter" id="planner-view">
<h2 class="subtitle">Planning</h2>
<Planner />
</section>
</div>
</template>
@@ -28,6 +28,7 @@ import 'bulma/css/bulma.css'
import Heading from './components/Heading.vue'
import RecipeDetails from './components/RecipeDetails.vue'
import RecipeList from './components/RecipeList.vue'
import Planner from './components/Planner.vue'
@@ -37,10 +38,15 @@ export default {
Heading,
RecipeDetails,
RecipeList,
Planner,
},
data () {
return {
is_loading: true,
status: {
loading: true,
error: false,
error_msg: "",
},
items: [],
// Index of the item
// activated for details view
@@ -71,10 +77,14 @@ export default {
fetch("http://localhost:8000/api/list")
.then((res) => res.json())
.then((data) => {
console.log(data);
this.items = data;
this.is_loading = false;
this.status.loading = false;
})
.catch(function(err) {
this.status.loading = false;
this.status.error = true;
this.statue.error_msg = err;
console.error(err);
});
}

View File

@@ -0,0 +1,25 @@
<template>
<div class="box">
<h2 class="subtitle">Week</h2>
<button @click="fetchSolution" class="button is-primary">FetchSolution</button>
<p>{{ template }}</p>
</div>
</template>
<script>
export default {
name: 'Planner',
data () {
return {
template: "",
};},
methods: {
fetchSolution: function() {
fetch("http://localhost:8000/api/solver/one")
.then((res) => res.json())
.then((data) => this.template = data)
.catch((err) => console.log(err));
}
}
}
</script>

View File

@@ -1,6 +1,6 @@
<template>
<div class="container box">
<button @click="$emit('close')" class="button is-pulled-right">X close</button>
<button @click="$emit('close')" class="button is-pulled-left">Return to list</button>
<h4 class="title">{{ item.title }}</h4>
<h6 class="subtitle">{{ categories[item.category].name }}</h6>
<p><strong>Ingredients : </strong></p>
@@ -15,6 +15,8 @@
</template>
<script>
import _, {categories} from './RecipeList.vue'
export default {
name: 'RecipeDetails',
props: {
@@ -28,7 +30,7 @@ export default {
},
data () {
return {
categories: ["test", "test", "test"],
categories: categories,
}
}
}

View File

@@ -2,14 +2,16 @@
<div class="container">
<div v-if="active_category == -1" class="columns">
<div v-for="c in categories" :key="c.id" class="column">
<button @click="setActiveCategory(c.id)" class="button is-large is-primary has-text-dark">{{ c.name }}</button>
<button @click="setActiveCategory(c.id)"
class="button is-large is-primary has-text-dark button-block">
{{ c.name }}</button>
</div>
</div>
<div v-else class="box">
<button @click="setActiveCategory(-1)" class="button is-pulled-left"><< back</button>
<button @click="setActiveCategory(-1)" class="button is-pulled-left">Categories</button>
<h2 class="subtitle">{{ categories[active_category].name }}</h2>
<ul>
<li v-for="item in displayed" :key="item.id">
<li v-for="item in displayed" :key="item.id" class="has-text-left">
<a href=""
@click.prevent="$emit(
'open-details',
@@ -52,3 +54,14 @@ export default {
},
}
</script>
<style scoped>
.button-block {
width: 200px;
height: 200px;
}
.li {
list-style: circle;
}
</style>