113 lines
2.8 KiB
Dart
113 lines
2.8 KiB
Dart
import 'package:brick_offline_first_with_supabase/brick_offline_first_with_supabase.dart';
|
|
import 'package:brick_supabase/brick_supabase.dart';
|
|
|
|
enum NetworkLinkKind {
|
|
copper,
|
|
fiber,
|
|
wireless,
|
|
virtual,
|
|
unknown;
|
|
|
|
String get wire {
|
|
switch (this) {
|
|
case NetworkLinkKind.copper:
|
|
return 'copper';
|
|
case NetworkLinkKind.fiber:
|
|
return 'fiber';
|
|
case NetworkLinkKind.wireless:
|
|
return 'wireless';
|
|
case NetworkLinkKind.virtual:
|
|
return 'virtual';
|
|
case NetworkLinkKind.unknown:
|
|
return 'unknown';
|
|
}
|
|
}
|
|
|
|
String get label {
|
|
switch (this) {
|
|
case NetworkLinkKind.copper:
|
|
return 'Copper';
|
|
case NetworkLinkKind.fiber:
|
|
return 'Fiber';
|
|
case NetworkLinkKind.wireless:
|
|
return 'Wireless';
|
|
case NetworkLinkKind.virtual:
|
|
return 'Virtual';
|
|
case NetworkLinkKind.unknown:
|
|
return 'Unknown';
|
|
}
|
|
}
|
|
|
|
static NetworkLinkKind? fromWire(String? value) {
|
|
switch (value) {
|
|
case 'copper':
|
|
return NetworkLinkKind.copper;
|
|
case 'fiber':
|
|
return NetworkLinkKind.fiber;
|
|
case 'wireless':
|
|
return NetworkLinkKind.wireless;
|
|
case 'virtual':
|
|
return NetworkLinkKind.virtual;
|
|
case 'unknown':
|
|
return NetworkLinkKind.unknown;
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
@ConnectOfflineFirstWithSupabase(
|
|
supabaseConfig: SupabaseSerializable(tableName: 'network_links'),
|
|
)
|
|
class NetworkLink extends OfflineFirstWithSupabaseModel {
|
|
final String id;
|
|
final String portA;
|
|
final String portB;
|
|
final NetworkLinkKind? linkKind;
|
|
final String? cableLabel;
|
|
final String? notes;
|
|
final DateTime createdAt;
|
|
|
|
NetworkLink({
|
|
required this.id,
|
|
required this.portA,
|
|
required this.portB,
|
|
this.linkKind,
|
|
this.cableLabel,
|
|
this.notes,
|
|
required this.createdAt,
|
|
});
|
|
|
|
factory NetworkLink.fromMap(Map<String, dynamic> map) {
|
|
return NetworkLink(
|
|
id: map['id'] as String,
|
|
portA: map['port_a'] as String,
|
|
portB: map['port_b'] as String,
|
|
linkKind: NetworkLinkKind.fromWire(map['link_kind'] as String?),
|
|
cableLabel: map['cable_label'] as String?,
|
|
notes: map['notes'] as String?,
|
|
createdAt:
|
|
DateTime.tryParse(map['created_at']?.toString() ?? '') ?? DateTime.now(),
|
|
);
|
|
}
|
|
|
|
/// Insert helper that enforces the canonical port_a < port_b ordering
|
|
/// required by the database CHECK constraint.
|
|
static Map<String, dynamic> insertMapFor({
|
|
required String portA,
|
|
required String portB,
|
|
NetworkLinkKind? linkKind,
|
|
String? cableLabel,
|
|
String? notes,
|
|
}) {
|
|
final ordered = portA.compareTo(portB) < 0 ? [portA, portB] : [portB, portA];
|
|
return {
|
|
'port_a': ordered[0],
|
|
'port_b': ordered[1],
|
|
'link_kind': ?linkKind?.wire,
|
|
'cable_label': ?cableLabel,
|
|
'notes': ?notes,
|
|
};
|
|
}
|
|
}
|