1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use diesel::prelude::*;
use recipe::schema::*;

#[table_name="recipes"]
#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[has_many(recipe_ingredients, foreign_key="recipe_id")]
pub struct Recipe {
    pub id: i32,
    pub name: String,
    pub description: String,
}

#[derive(Insertable)]
#[table_name="recipes"]
pub struct NewRecipe<'a> {
    pub name: &'a str,
    pub description: &'a str,
}

#[table_name="recipe_ingredients"]
#[belongs_to(Recipe, foreign_key="recipe_id")]
#[belongs_to(Ingredient, foreign_key="ingredient_id")]
#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
pub struct RecipeIngredient {
    pub id: i32,
    pub recipe_id: i32,
    pub ingredient_id: i32,
    pub amount: f32,
}

#[derive(Insertable)]
#[table_name="recipe_ingredients"]
pub struct NewRecipeIngredient {
    pub recipe_id: i32,
    pub ingredient_id: i32,
    pub amount: f32,
}


#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[has_many(recipe_ingredients, foreign_key="ingredient_id")]
#[table_name="ingredients"]
pub struct Ingredient {
    pub id: i32,
    pub name: String,
    pub description: String,
    pub available: bool,
}


#[derive(Insertable)]
#[table_name="ingredients"]
pub struct NewIngredient<'a> {
    pub name: &'a str,
    pub description: &'a str,
    pub available: bool,
}