recipes/backend/src/data/model.rs

99 lines
1.9 KiB
Rust

use chrono::prelude::*;
use common::web_api::Difficulty;
use sqlx::{self, FromRow};
#[derive(Debug, Clone, FromRow)]
pub struct User {
pub id: i64,
pub name: String,
pub email: String,
pub default_servings: u32,
#[sqlx(try_from = "u8")]
pub first_day_of_the_week: Weekday,
pub lang: String,
pub is_admin: bool,
}
impl User {
pub fn can_edit_recipe(&self, recipe: &Recipe) -> bool {
self.is_admin || recipe.user_id == self.id
}
}
#[derive(Debug, FromRow)]
pub struct UserLoginInfo {
pub last_login_datetime: DateTime<Utc>,
pub ip: String,
pub user_agent: String,
}
#[derive(Debug, FromRow)]
pub struct Recipe {
pub id: i64,
pub user_id: i64,
pub title: String,
pub lang: String,
pub estimated_time: Option<u32>, // [s].
pub description: String,
#[sqlx(try_from = "u32")]
pub difficulty: Difficulty,
pub servings: Option<u32>,
pub is_public: bool,
#[sqlx(skip)]
pub tags: Vec<String>,
#[sqlx(skip)]
pub groups: Vec<Group>,
}
#[derive(Debug, FromRow)]
pub struct RecipeSearchResult {
pub id: i64,
pub title: String,
pub title_highlighted: String,
}
#[derive(Debug, FromRow)]
pub struct Group {
pub id: i64,
pub name: String,
pub comment: String,
#[sqlx(skip)]
pub steps: Vec<Step>,
}
#[derive(Debug, FromRow)]
pub struct Step {
pub id: i64,
pub action: String,
#[sqlx(skip)]
pub ingredients: Vec<Ingredient>,
}
#[derive(Debug, FromRow)]
pub struct Ingredient {
pub id: i64,
pub name: String,
pub comment: String,
pub quantity_value: Option<f64>,
pub quantity_unit: String,
}
#[derive(Debug, FromRow)]
pub struct ShoppingListItem {
pub id: i64,
pub name: String,
pub quantity_value: Option<f64>,
pub quantity_unit: String,
pub recipe_id: Option<i64>,
pub recipe_title: Option<String>,
pub date: Option<NaiveDate>,
pub is_checked: bool,
}