createModelFile method

void createModelFile(
  1. String featureName,
  2. String basePath
)

Implementation

void createModelFile(String featureName, String basePath) {
  final snakeCaseName = toSnakeCase(featureName);
  final classNamePrefix =
      featureName[0].toUpperCase() + featureName.substring(1);
  final packageName = getPackageName(basePath);

  final modelFile = File('$basePath/data/models/${snakeCaseName}_model.dart');
  if (!modelFile.existsSync()) {
    modelFile.writeAsStringSync('''
import 'package:$packageName/features/$featureName/domain/entities/$snakeCaseName.dart';

class ${classNamePrefix}Model {
final String id;
final String name;

${classNamePrefix}Model({
  required this.id,
  required this.name,
});

factory ${classNamePrefix}Model.fromJson(Map<String, dynamic> json) {
  return ${classNamePrefix}Model(
    id: json['id'],
    name: json['name'],
  );
}

Map<String, dynamic> toJson() {
  return {
    'id': id,
    'name': name,
  };
}

$classNamePrefix toEntity() {
  return $classNamePrefix(
    id: id,
    name: name,
  );
}
}
''');
    print('Created model file: ${snakeCaseName}_model.dart');
  }
}