Adds basic vuejs functionnality, implements RecipeCategory custom field

This commit is contained in:
2019-02-01 15:58:58 +01:00
parent 66514eb192
commit 616f8095e2
5 changed files with 112 additions and 14 deletions

View File

@@ -2,11 +2,66 @@ use super::schema::recipes;
use super::schema::ingredients;
use super::diesel::prelude::*;
pub mod fields {
use diesel::{
backend::Backend,
sqlite::Sqlite,
sql_types::SmallInt,
deserialize::{self, FromSql},
serialize::{self, Output, ToSql},
};
use std::io::Write;
/// All recipes have a single associated category
/// representing the main use of the resulting preparation.
///
/// It is stored as Integer
#[derive(Debug, Clone, FromSqlRow, AsExpression)]
#[repr(i16)]
pub enum RecipeCategory {
Breakfast = 0,
Starter = 1,
MainCourse = 2,
Dessert = 3
}
impl RecipeCategory {
fn from_id(id: i16) -> Option<RecipeCategory> {
match id {
0 => Some(RecipeCategory::Breakfast),
1 => Some(RecipeCategory::Starter),
2 => Some(RecipeCategory::MainCourse),
3 => Some(RecipeCategory::Dessert),
_ => None,
}
}
}
impl FromSql<SmallInt, Sqlite> for RecipeCategory {
fn from_sql(bytes: Option<&<Sqlite as Backend>::RawValue>) -> deserialize::Result<Self> {
if let Some(result) = RecipeCategory::from_id(
<i16 as FromSql<SmallInt,Sqlite>>::from_sql(bytes)?)
{ Ok(result) }
else { Err("Invalid RecipeCategory id".into()) }
}
}
impl ToSql<SmallInt, Sqlite> for RecipeCategory {
fn to_sql<W: Write>(&self, out: &mut Output<W, Sqlite>) -> serialize::Result {
let as_int = self.clone() as i16;
<i16 as ToSql<SmallInt, Sqlite>>::to_sql(&as_int, out)
}
}
}
/// Data for a recipe stored in DB
#[derive(Debug, Clone, Queryable)]
pub struct Recipe {
pub id: i32,
pub title: String,
pub category: i32,
pub category: fields::RecipeCategory,
pub ingredients: String,
pub preparation: String,
}
@@ -15,13 +70,13 @@ pub struct Recipe {
#[table_name="recipes"]
pub struct NewRecipe<'a> {
pub title: &'a str,
pub category: i32,
pub category: i16,
pub ingredients: &'a str,
pub preparation: &'a str,
}
impl<'a> NewRecipe<'a> {
pub fn new(title: &'a str, category: i32, ingredients: &'a str, preparation: &'a str) -> Self {
pub fn new(title: &'a str, category: i16, ingredients: &'a str, preparation: &'a str) -> Self {
NewRecipe{
title,
category,