it0/it0_app/lib/features/servers/data/models/server_model.dart

86 lines
2.2 KiB
Dart

import '../../domain/entities/server.dart';
class ServerModel {
final String id;
final String hostname;
final String? ip;
final String? environment;
final String? role;
final String status;
final String? lastCheckedAt;
final List<String>? tags;
const ServerModel({
required this.id,
required this.hostname,
this.ip,
this.environment,
this.role,
required this.status,
this.lastCheckedAt,
this.tags,
});
factory ServerModel.fromJson(Map<String, dynamic> json) {
return ServerModel(
id: json['id']?.toString() ?? '',
hostname: json['hostname'] as String? ?? 'unknown',
ip: json['ip'] as String? ?? json['ipAddress'] as String? ??
json['ip_address'] as String?,
environment: json['environment'] as String?,
role: json['role'] as String?,
status: json['status'] as String? ?? 'unknown',
lastCheckedAt: json['lastCheckedAt'] as String? ??
json['last_checked_at'] as String?,
tags: (json['tags'] as List<dynamic>?)
?.map((t) => t.toString())
.toList(),
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'hostname': hostname,
if (ip != null) 'ip': ip,
if (environment != null) 'environment': environment,
if (role != null) 'role': role,
'status': status,
if (tags != null) 'tags': tags,
};
}
/// Converts this model to a domain [Server] entity.
Server toEntity() {
return Server(
id: id,
hostname: hostname,
ip: ip,
environment: environment,
role: role,
status: status,
lastCheckedAt: _parseDateTime(lastCheckedAt),
tags: tags,
);
}
/// Creates a model from a domain [Server] entity.
factory ServerModel.fromEntity(Server entity) {
return ServerModel(
id: entity.id,
hostname: entity.hostname,
ip: entity.ip,
environment: entity.environment,
role: entity.role,
status: entity.status,
lastCheckedAt: entity.lastCheckedAt?.toIso8601String(),
tags: entity.tags,
);
}
static DateTime? _parseDateTime(String? value) {
if (value == null) return null;
return DateTime.tryParse(value);
}
}