Minor refactoring

This commit is contained in:
2019-01-29 21:44:05 +01:00
parent 4576ffb03b
commit 874a9f86f8
4 changed files with 31 additions and 23 deletions

View File

@@ -1,5 +1,6 @@
use super::schema::recipes;
use super::schema::ingredients;
use super::diesel::prelude::*;
#[derive(Queryable)]
pub struct Recipe {
@@ -15,10 +16,29 @@ pub struct Recipe {
pub struct NewRecipe<'a> {
pub title: &'a str,
pub category: i32,
pub ingredients: String,
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 {
NewRecipe{
title,
category,
ingredients,
preparation,
}
}
pub fn insert(self, conn: &SqliteConnection) -> Result<Self, String> {
diesel::insert_into(recipes::table)
.values(&self)
.execute(conn)
.expect("Error inserting recipe");
Ok(self)
}
}
#[derive(Queryable)]
pub struct Ingredient {
pub id: i32,