diff --git a/src/db/models/wireguard.rs b/src/db/models/wireguard.rs index 97d84b705..9dc8f76ea 100644 --- a/src/db/models/wireguard.rs +++ b/src/db/models/wireguard.rs @@ -942,6 +942,28 @@ impl WireguardNetwork { transfer_series, }) } + + pub async fn get_devices_by_type<'e, E>( + &self, + executor: E, + device_type: DeviceType, + ) -> Result>, SqlxError> + where + E: PgExecutor<'e>, + { + query_as!( + Device, + "SELECT \ + id, name, wireguard_pubkey, user_id, created, description, device_type \"device_type: DeviceType\", \ + configured \ + FROM device WHERE id in (SELECT device_id FROM wireguard_network_device WHERE wireguard_network_id = $1) \ + AND device_type = $2", + self.id, + device_type as DeviceType + ) + .fetch_all(executor) + .await + } } // [`IpNetwork`] does not implement [`Default`] diff --git a/src/handlers/network_devices.rs b/src/handlers/network_devices.rs index 67bc2a699..58fd04c29 100644 --- a/src/handlers/network_devices.rs +++ b/src/handlers/network_devices.rs @@ -642,7 +642,7 @@ pub async fn modify_network_device( ); } - let network_device_info = NetworkDeviceInfo::from_device(device, &mut *transaction).await?; + let network_device_info = NetworkDeviceInfo::from_device(device, &mut transaction).await?; transaction.commit().await?; Ok(ApiResponse { diff --git a/src/handlers/wireguard.rs b/src/handlers/wireguard.rs index 0f3cb5f3e..3618cae86 100644 --- a/src/handlers/wireguard.rs +++ b/src/handlers/wireguard.rs @@ -217,7 +217,15 @@ pub async fn delete_network( ); let network = find_network(network_id, &appstate.pool).await?; let network_name = network.name.clone(); - network.delete(&appstate.pool).await?; + let mut transaction = appstate.pool.begin().await?; + let network_devices = network + .get_devices_by_type(&mut *transaction, DeviceType::Network) + .await?; + for device in network_devices { + device.delete(&mut *transaction).await?; + } + network.delete(&mut *transaction).await?; + transaction.commit().await?; appstate.send_wireguard_event(GatewayEvent::NetworkDeleted(network_id, network_name)); info!( "User {} deleted WireGuard network {network_id}",