import '../../domain/entities/comment.dart'; /// Modèle de données pour les commentaires (Data Transfer Object). /// /// Cette classe est responsable de la sérialisation/désérialisation /// avec l'API backend et convertit vers/depuis l'entité de domaine Comment. class CommentModel { CommentModel({ required this.id, required this.postId, required this.userId, required this.userFirstName, required this.userLastName, required this.userProfileImageUrl, required this.content, required this.timestamp, }); /// Factory pour créer un [CommentModel] à partir d'un JSON. factory CommentModel.fromJson(Map json) { return CommentModel( id: _parseId(json, 'id', ''), postId: _parseId(json, 'postId', ''), userId: _parseId(json, 'userId', ''), userFirstName: _parseString(json, 'userFirstName', ''), userLastName: _parseString(json, 'userLastName', ''), userProfileImageUrl: _parseString(json, 'userProfileImageUrl', ''), content: _parseString(json, 'content', ''), timestamp: _parseTimestamp(json['timestamp']), ); } /// Crée un [CommentModel] depuis une entité de domaine [Comment]. factory CommentModel.fromEntity(Comment comment) { return CommentModel( id: comment.id, postId: comment.postId, userId: comment.userId, userFirstName: comment.userFirstName, userLastName: comment.userLastName, userProfileImageUrl: comment.userProfileImageUrl, content: comment.content, timestamp: comment.timestamp, ); } final String id; final String postId; final String userId; final String userFirstName; final String userLastName; final String userProfileImageUrl; final String content; final DateTime timestamp; /// Convertit ce modèle en JSON pour l'envoi vers l'API. Map toJson() { return { 'id': id, 'postId': postId, 'userId': userId, 'userFirstName': userFirstName, 'userLastName': userLastName, 'userProfileImageUrl': userProfileImageUrl, 'content': content, 'timestamp': timestamp.toIso8601String(), }; } /// Convertit ce modèle vers une entité de domaine [Comment]. Comment toEntity() { return Comment( id: id, postId: postId, userId: userId, userFirstName: userFirstName, userLastName: userLastName, userProfileImageUrl: userProfileImageUrl, content: content, timestamp: timestamp, ); } /// Parse une valeur string depuis le JSON avec valeur par défaut. static String _parseString( Map json, String key, String defaultValue, ) { return json[key] as String? ?? defaultValue; } /// Parse un timestamp depuis le JSON. static DateTime _parseTimestamp(dynamic timestamp) { if (timestamp == null) return DateTime.now(); if (timestamp is String) { try { return DateTime.parse(timestamp); } catch (e) { return DateTime.now(); } } if (timestamp is int) { return DateTime.fromMillisecondsSinceEpoch(timestamp); } return DateTime.now(); } /// Parse un ID (UUID) depuis le JSON. /// /// [json] Le JSON à parser /// [key] La clé de l'ID /// [defaultValue] La valeur par défaut si l'ID est null /// /// Returns l'ID parsé ou la valeur par défaut static String _parseId( Map json, String key, String defaultValue, ) { final value = json[key]; if (value == null) return defaultValue; return value.toString(); } }