feat: add diet entity and user lifestyle habits with data seeding support

This commit is contained in:
2025-12-19 13:52:48 +05:30
parent baccbee706
commit b9fbbbbbd6
6 changed files with 94 additions and 3 deletions

View File

@@ -9,6 +9,16 @@ roles:
- name: reader - name: reader
description: Read-only access to analytics description: Read-only access to analytics
diets:
- name: normal
description: Standard balanced diet without specific restrictions
- name: vegetarian
description: No meat or fish; includes dairy and eggs
- name: vegan
description: Purely plant-based; no animal products
- name: jain
description: Strictly vegetarian; no root vegetables (onions, potatoes, garlic, etc.)
biomarker_categories: biomarker_categories:
- name: blood - name: blood
description: Blood cell counts and hemogram markers description: Blood cell counts and hemogram markers

View File

@@ -5,7 +5,7 @@ use sea_orm::sea_query::SqliteQueryBuilder;
use crate::config::Config; use crate::config::Config;
use crate::models::bio::{biomarker, biomarker_category, biomarker_entry, biomarker_reference_rule}; use crate::models::bio::{biomarker, biomarker_category, biomarker_entry, biomarker_reference_rule};
use crate::models::user::{role, session, user}; use crate::models::user::{diet, role, session, user};
/// Connect to the SQLite database. /// Connect to the SQLite database.
pub async fn connect(config: &Config) -> Result<DatabaseConnection, DbErr> { pub async fn connect(config: &Config) -> Result<DatabaseConnection, DbErr> {
@@ -27,6 +27,7 @@ pub async fn run_migrations(db: &DatabaseConnection) -> Result<(), DbErr> {
// Create table statements (order matters for foreign keys) // Create table statements (order matters for foreign keys)
let statements = vec![ let statements = vec![
schema.create_table_from_entity(role::Entity), schema.create_table_from_entity(role::Entity),
schema.create_table_from_entity(diet::Entity),
schema.create_table_from_entity(user::Entity), schema.create_table_from_entity(user::Entity),
schema.create_table_from_entity(session::Entity), schema.create_table_from_entity(session::Entity),
schema.create_table_from_entity(biomarker_category::Entity), schema.create_table_from_entity(biomarker_category::Entity),

View File

@@ -0,0 +1,31 @@
//! Diet entity - predefined diet types for user selection.
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "diets")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(unique)]
pub name: String,
#[sea_orm(column_type = "Text", nullable)]
pub description: Option<String>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::user::Entity")]
Users,
}
impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::Users.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -1,9 +1,11 @@
//! User and session entities for authentication. //! User and session entities for authentication.
pub mod diet;
pub mod role; pub mod role;
pub mod session; pub mod session;
pub mod user; pub mod user;
pub use diet::Entity as Diet;
pub use role::Entity as Role; pub use role::Entity as Role;
pub use session::Entity as Session; pub use session::Entity as Session;
pub use user::Entity as User; pub use user::Entity as User;

View File

@@ -31,6 +31,16 @@ pub struct Model {
/// Date of birth /// Date of birth
pub birthdate: Option<Date>, pub birthdate: Option<Date>,
// Lifestyle / Habits
/// Whether the user smokes
pub smoking: Option<bool>,
/// Whether the user consumes alcohol
pub alcohol: Option<bool>,
/// Foreign key to diet types
pub diet_id: Option<i32>,
pub created_at: DateTime, pub created_at: DateTime,
pub updated_at: DateTime, pub updated_at: DateTime,
} }
@@ -46,6 +56,13 @@ pub enum Relation {
to = "super::role::Column::Id" to = "super::role::Column::Id"
)] )]
Role, Role,
#[sea_orm(
belongs_to = "super::diet::Entity",
from = "Column::DietId",
to = "super::diet::Column::Id"
)]
Diet,
} }
impl Related<super::session::Entity> for Entity { impl Related<super::session::Entity> for Entity {
@@ -60,5 +77,10 @@ impl Related<super::role::Entity> for Entity {
} }
} }
impl ActiveModelBehavior for ActiveModel {} impl Related<super::diet::Entity> for Entity {
fn to() -> RelationDef {
Relation::Diet.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -7,7 +7,7 @@ use std::fs;
use std::path::Path; use std::path::Path;
use crate::models::bio::{biomarker, biomarker_category, biomarker_reference_rule}; use crate::models::bio::{biomarker, biomarker_category, biomarker_reference_rule};
use crate::models::user::role; use crate::models::user::{diet, role};
// ============================================================================ // ============================================================================
// Seed Data Structures // Seed Data Structures
@@ -16,6 +16,7 @@ use crate::models::user::role;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct SeedData { pub struct SeedData {
pub roles: Vec<RoleSeed>, pub roles: Vec<RoleSeed>,
pub diets: Vec<DietSeed>,
pub biomarker_categories: Vec<BiomarkerCategorySeed>, pub biomarker_categories: Vec<BiomarkerCategorySeed>,
} }
@@ -36,6 +37,12 @@ pub struct BiomarkerCategorySeed {
pub description: Option<String>, pub description: Option<String>,
} }
#[derive(Debug, Deserialize)]
pub struct DietSeed {
pub name: String,
pub description: Option<String>,
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct BiomarkerSeed { pub struct BiomarkerSeed {
pub name: String, pub name: String,
@@ -350,6 +357,24 @@ pub async fn sync_seed_data(db: &DatabaseConnection, seed: &SeedData) -> anyhow:
} }
} }
// Sync diet types
for diet_seed in &seed.diets {
let existing = diet::Entity::find()
.filter(diet::Column::Name.eq(&diet_seed.name))
.one(db)
.await?;
if existing.is_none() {
let new_diet = diet::ActiveModel {
name: Set(diet_seed.name.clone()),
description: Set(diet_seed.description.clone()),
..Default::default()
};
new_diet.insert(db).await?;
tracing::info!("Inserted diet: {}", diet_seed.name);
}
}
// Sync biomarker categories // Sync biomarker categories
for cat_seed in &seed.biomarker_categories { for cat_seed in &seed.biomarker_categories {
let existing = biomarker_category::Entity::find() let existing = biomarker_category::Entity::find()