adds initial user defined panels

This commit is contained in:
2025-12-23 16:41:21 -05:00
parent a110aa6376
commit ebbf864af9
33 changed files with 2188 additions and 370 deletions

130
server/src/panels/mod.rs Normal file
View File

@@ -0,0 +1,130 @@
pub mod panel;
use crate::core::Uuid;
use crate::panels::panel::{PanelRequired, PanelUpdate};
use panel::Panel;
use sqlx::SqlitePool;
pub struct PanelService {
pool: SqlitePool,
}
impl PanelService {
pub fn new(pool: SqlitePool) -> Self {
Self { pool }
}
pub async fn create(&self, name: &str, data: &str) -> anyhow::Result<Uuid> {
let id = Uuid::random();
let mut transaction = self.pool.begin().await?;
let _ = sqlx::query!(
r#"
INSERT INTO PANELS (id, name, data, deleted)
VALUES ($1, $2, $3, FALSE);
"#,
id.value,
name,
data
)
.execute(&mut *transaction)
.await?;
transaction.commit().await?;
Ok(id)
}
pub async fn read_all(&self) -> anyhow::Result<Vec<PanelRequired>> {
let mut transaction = self.pool.begin().await?;
let panels = sqlx::query_as!(
PanelRequired,
r#"
SELECT id, name
FROM PANELS
WHERE deleted = FALSE
"#,
)
.fetch_all(&mut *transaction)
.await?;
transaction.commit().await?;
Ok(panels)
}
pub async fn read(&self, id: Uuid) -> anyhow::Result<Option<Panel>> {
let mut transaction = self.pool.begin().await?;
let panel = sqlx::query_as(
r#"
SELECT id, name, data
FROM PANELS
WHERE id = $1 AND deleted = FALSE
"#,
)
.bind(id.value)
.fetch_optional(&mut *transaction)
.await?;
transaction.commit().await?;
Ok(panel)
}
pub async fn update(&self, id: Uuid, data: PanelUpdate) -> anyhow::Result<()> {
let mut transaction = self.pool.begin().await?;
if let Some(name) = data.name {
let _ = sqlx::query!(
r#"
UPDATE PANELS
SET name = $2
WHERE id = $1;
"#,
id.value,
name
)
.execute(&mut *transaction)
.await?;
}
if let Some(data) = data.data {
let _ = sqlx::query!(
r#"
UPDATE PANELS
SET data = $2
WHERE id = $1;
"#,
id.value,
data
)
.execute(&mut *transaction)
.await?;
}
transaction.commit().await?;
Ok(())
}
pub async fn delete(&self, id: Uuid) -> anyhow::Result<()> {
let mut transaction = self.pool.begin().await?;
let _ = sqlx::query!(
r#"
UPDATE PANELS
SET deleted = TRUE
WHERE id = $1;
"#,
id.value,
)
.execute(&mut *transaction)
.await?;
transaction.commit().await?;
Ok(())
}
}

View File

@@ -0,0 +1,22 @@
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
#[derive(Clone, FromRow, Serialize, Deserialize)]
pub struct PanelRequired {
pub id: String,
pub name: String,
}
#[derive(Clone, FromRow, Serialize, Deserialize)]
pub struct Panel {
#[sqlx(flatten)]
#[serde(flatten)]
pub header: PanelRequired,
pub data: String,
}
#[derive(Default, Clone, Serialize, Deserialize)]
pub struct PanelUpdate {
pub name: Option<String>,
pub data: Option<String>,
}