feat(networks): add name field to networks
- Migration 0007: ALTER TABLE networks ADD COLUMN name TEXT NOT NULL DEFAULT '' - Network model, repository, and API updated to include name - Networks page: name input in the add form, Name column as first column in table - Delete modal now shows "Name (CIDR)" for clarity - Hosts page: network dropdowns now show network name instead of CIDR - Seeds updated with names (LAN, DMZ, Corporate, VPN) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -11,7 +11,7 @@ use crate::server::db::DbError;
|
||||
pub async fn list_networks(pool: &AnyPool) -> Result<Vec<Network>, DbError> {
|
||||
// `fetch_all` runs the query and collects every row into a Vec.
|
||||
// It returns an error if the query fails; an empty table returns Ok(vec![]).
|
||||
let rows = sqlx::query("SELECT id, cidr FROM networks ORDER BY id")
|
||||
let rows = sqlx::query("SELECT id, name, cidr FROM networks ORDER BY id")
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
|
||||
@@ -42,11 +42,14 @@ pub async fn find_network(pool: &AnyPool, id: i64) -> Result<Option<Network>, Db
|
||||
/// `RETURNING id, cidr` reads back the inserted row in a single round-trip,
|
||||
/// avoiding a separate SELECT after the INSERT.
|
||||
/// Requires SQLite ≥ 3.35 (2021) and any PostgreSQL version.
|
||||
pub async fn create_network(pool: &AnyPool, cidr: &str) -> Result<Network, DbError> {
|
||||
let row = sqlx::query("INSERT INTO networks (cidr) VALUES ($1) RETURNING id, cidr")
|
||||
.bind(cidr)
|
||||
.fetch_one(pool) // exactly one row is returned by RETURNING
|
||||
.await?;
|
||||
pub async fn create_network(pool: &AnyPool, name: &str, cidr: &str) -> Result<Network, DbError> {
|
||||
let row = sqlx::query(
|
||||
"INSERT INTO networks (name, cidr) VALUES ($1, $2) RETURNING id, name, cidr",
|
||||
)
|
||||
.bind(name)
|
||||
.bind(cidr)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
Ok(row_to_network(&row))
|
||||
}
|
||||
@@ -73,7 +76,8 @@ pub async fn delete_network(pool: &AnyPool, id: i64) -> Result<bool, DbError> {
|
||||
/// The type must implement `sqlx::Decode` for the `Any` backend.
|
||||
fn row_to_network(row: &sqlx::any::AnyRow) -> Network {
|
||||
Network {
|
||||
id: row.get("id"),
|
||||
id: row.get("id"),
|
||||
name: row.get("name"),
|
||||
cidr: row.get("cidr"),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user