agregar tablas epics y user_stories con relacion + Rust commands + frontend bridge

This commit is contained in:
2026-05-27 17:32:19 -05:00
parent 4b52033e0a
commit b141be345a
3 changed files with 278 additions and 0 deletions
+217
View File
@@ -111,6 +111,41 @@ pub struct PerformanceSnapshot {
pub calculated_at: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Epic {
pub id: i64,
pub initiative_id: i64,
pub code: Option<String>,
pub name: String,
pub description: Option<String>,
pub status: Option<String>,
pub client_taker: Option<i64>,
pub stimated_start_date: Option<String>,
pub stimated_end_date: Option<String>,
pub start_date: Option<String>,
pub end_date: Option<String>,
pub created_at: Option<String>,
pub updated_at: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct UserStory {
pub id: i64,
pub initiative_id: i64,
pub epic_id: Option<i64>,
pub code: Option<String>,
pub title: String,
pub description: Option<String>,
pub acceptance_criteria: Option<String>,
pub status: Option<String>,
pub priority: Option<String>,
pub story_points: Option<f64>,
pub estimated_hours: Option<f64>,
pub actual_hours: Option<f64>,
pub assigned_to: Option<i64>,
pub created_at: Option<String>,
}
async fn get_conn(db_path: &str) -> Result<libsql::Connection, String> {
let db = libsql::Builder::new_local(db_path)
.build()
@@ -225,6 +260,42 @@ async fn get_conn(db_path: &str) -> Result<libsql::Connection, String> {
impediment_count INTEGER,
calculated_at TEXT DEFAULT (datetime('now')),
FOREIGN KEY (user_id) REFERENCES alpha_users(id)
);
CREATE TABLE IF NOT EXISTS epics (
id INTEGER PRIMARY KEY,
initiative_id INTEGER NOT NULL,
code TEXT,
name TEXT NOT NULL,
description TEXT,
status TEXT DEFAULT 'active',
client_taker INTEGER,
stimated_start_date TEXT,
stimated_end_date TEXT,
start_date TEXT,
end_date TEXT,
created_at TEXT DEFAULT (datetime('now')),
updated_at TEXT DEFAULT (datetime('now')),
FOREIGN KEY (initiative_id) REFERENCES projects(id)
);
CREATE TABLE IF NOT EXISTS user_stories (
id INTEGER PRIMARY KEY,
initiative_id INTEGER NOT NULL,
epic_id INTEGER,
code TEXT,
title TEXT NOT NULL,
description TEXT,
acceptance_criteria TEXT,
status TEXT DEFAULT 'backlog',
priority TEXT DEFAULT 'medium',
story_points REAL,
estimated_hours REAL,
actual_hours REAL,
assigned_to INTEGER,
created_at TEXT DEFAULT (datetime('now')),
FOREIGN KEY (initiative_id) REFERENCES projects(id),
FOREIGN KEY (epic_id) REFERENCES epics(id)
);"
)
.await
@@ -744,4 +815,150 @@ pub mod commands {
Ok(conn.last_insert_rowid())
}
// ────────────────────────────────────────────
// Epics
// ────────────────────────────────────────────
#[tauri::command]
pub async fn get_epics(state: State<'_, Mutex<String>>, initiative_id: i64) -> Result<Vec<Epic>, String> {
let db_path = state.lock().map_err(|e| e.to_string())?.clone();
let conn = get_conn(&db_path).await?;
let mut rows = conn
.query("SELECT id, initiative_id, code, name, description, status, client_taker, stimated_start_date, stimated_end_date, start_date, end_date, created_at, updated_at FROM epics WHERE initiative_id = ?1 ORDER BY created_at DESC", libsql::params![initiative_id])
.await
.map_err(|e| format!("Query: {e}"))?;
let mut epics = Vec::new();
while let Some(row) = rows.next().await.map_err(|e| format!("Row: {e}"))? {
epics.push(Epic {
id: row.get::<i64>(0).unwrap_or(0),
initiative_id: row.get::<i64>(1).unwrap_or(0),
code: row.get::<String>(2).ok(),
name: row.get::<String>(3).unwrap_or_default(),
description: row.get::<String>(4).ok(),
status: row.get::<String>(5).ok(),
client_taker: row.get::<i64>(6).ok(),
stimated_start_date: row.get::<String>(7).ok(),
stimated_end_date: row.get::<String>(8).ok(),
start_date: row.get::<String>(9).ok(),
end_date: row.get::<String>(10).ok(),
created_at: row.get::<String>(11).ok(),
updated_at: row.get::<String>(12).ok(),
});
}
Ok(epics)
}
#[tauri::command]
pub async fn save_epic(state: State<'_, Mutex<String>>, epic: Epic) -> Result<i64, String> {
let db_path = state.lock().map_err(|e| e.to_string())?.clone();
let conn = get_conn(&db_path).await?;
conn.execute(
"INSERT OR REPLACE INTO epics (id, initiative_id, code, name, description, status, client_taker, stimated_start_date, stimated_end_date, start_date, end_date, updated_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, datetime('now'))",
libsql::params![
epic.id, epic.initiative_id, epic.code, epic.name, epic.description,
epic.status, epic.client_taker, epic.stimated_start_date,
epic.stimated_end_date, epic.start_date, epic.end_date,
],
)
.await
.map_err(|e| format!("Insert: {e}"))?;
Ok(conn.last_insert_rowid())
}
#[tauri::command]
pub async fn delete_epic(state: State<'_, Mutex<String>>, id: i64) -> Result<(), String> {
let db_path = state.lock().map_err(|e| e.to_string())?.clone();
let conn = get_conn(&db_path).await?;
conn.execute("DELETE FROM user_stories WHERE epic_id = ?1", libsql::params![id])
.await.map_err(|e| format!("Delete HUs: {e}"))?;
conn.execute("DELETE FROM epics WHERE id = ?1", libsql::params![id])
.await.map_err(|e| format!("Delete: {e}"))?;
Ok(())
}
// ────────────────────────────────────────────
// User Stories
// ────────────────────────────────────────────
#[tauri::command]
pub async fn get_user_stories(state: State<'_, Mutex<String>>, initiative_id: i64, epic_id: Option<i64>) -> Result<Vec<UserStory>, String> {
let db_path = state.lock().map_err(|e| e.to_string())?.clone();
let conn = get_conn(&db_path).await?;
let query = if epic_id.is_some() {
"SELECT id, initiative_id, epic_id, code, title, description, acceptance_criteria, status, priority, story_points, estimated_hours, actual_hours, assigned_to, created_at FROM user_stories WHERE initiative_id = ?1 AND epic_id = ?2 ORDER BY created_at DESC"
} else {
"SELECT id, initiative_id, epic_id, code, title, description, acceptance_criteria, status, priority, story_points, estimated_hours, actual_hours, assigned_to, created_at FROM user_stories WHERE initiative_id = ?1 ORDER BY created_at DESC"
};
let mut rows = if let Some(eid) = epic_id {
conn.query(query, libsql::params![initiative_id, eid]).await.map_err(|e| format!("Query: {e}"))?
} else {
conn.query(query, libsql::params![initiative_id]).await.map_err(|e| format!("Query: {e}"))?
};
let mut stories = Vec::new();
while let Some(row) = rows.next().await.map_err(|e| format!("Row: {e}"))? {
stories.push(UserStory {
id: row.get::<i64>(0).unwrap_or(0),
initiative_id: row.get::<i64>(1).unwrap_or(0),
epic_id: row.get::<i64>(2).ok(),
code: row.get::<String>(3).ok(),
title: row.get::<String>(4).unwrap_or_default(),
description: row.get::<String>(5).ok(),
acceptance_criteria: row.get::<String>(6).ok(),
status: row.get::<String>(7).ok(),
priority: row.get::<String>(8).ok(),
story_points: row.get::<f64>(9).ok(),
estimated_hours: row.get::<f64>(10).ok(),
actual_hours: row.get::<f64>(11).ok(),
assigned_to: row.get::<i64>(12).ok(),
created_at: row.get::<String>(13).ok(),
});
}
Ok(stories)
}
#[tauri::command]
pub async fn save_user_story(state: State<'_, Mutex<String>>, story: UserStory) -> Result<i64, String> {
let db_path = state.lock().map_err(|e| e.to_string())?.clone();
let conn = get_conn(&db_path).await?;
conn.execute(
"INSERT OR REPLACE INTO user_stories (id, initiative_id, epic_id, code, title, description, acceptance_criteria, status, priority, story_points, estimated_hours, actual_hours, assigned_to)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)",
libsql::params![
story.id, story.initiative_id, story.epic_id,
story.code, story.title, story.description,
story.acceptance_criteria, story.status, story.priority,
story.story_points, story.estimated_hours,
story.actual_hours, story.assigned_to,
],
)
.await
.map_err(|e| format!("Insert: {e}"))?;
Ok(conn.last_insert_rowid())
}
#[tauri::command]
pub async fn delete_user_story(state: State<'_, Mutex<String>>, id: i64) -> Result<(), String> {
let db_path = state.lock().map_err(|e| e.to_string())?.clone();
let conn = get_conn(&db_path).await?;
conn.execute("DELETE FROM user_stories WHERE id = ?1", libsql::params![id])
.await
.map_err(|e| format!("Delete: {e}"))?;
Ok(())
}
}