createMapper method

void createMapper(
  1. String pathPage,
  2. String pageName,
  3. String apiName
)

Implementation

void createMapper(
  String pathPage,
  String pageName,
  String apiName,
) {
  final apiClassName = apiName.pascalCase;

  final path = pathPage;
  DirectoryHelper.createDir(path);

  if (!exists(join(path, 'mapper.dart'))) {
    join(path, 'mapper.dart')
        .write('''import 'data/models/response/${apiName}_response.dart';
import 'domain/entities/${apiName}_entity.dart';

extension ${apiClassName}ResponseMapper on ${apiClassName}Response {
${apiClassName}Entity toEntity() => ${apiClassName}Entity(token: token);
}

extension ${apiClassName}EntityMapper on ${apiClassName}Entity {
${apiClassName}Response toResponse() => ${apiClassName}Response(token: token);
}''');
  } else {
    String data = File(join(path, 'mapper.dart')).readAsStringSync();

    data = '''import 'data/models/response/${apiName}_response.dart';
import 'domain/entities/${apiName}_entity.dart';
$data''';

    data = '''$data

extension ${apiClassName}ResponseMapper on ${apiClassName}Response {
${apiClassName}Entity toEntity() => ${apiClassName}Entity(token: token);
}

extension ${apiClassName}EntityMapper on ${apiClassName}Entity {
${apiClassName}Response toResponse() => ${apiClassName}Response(token: token);
}''';

    join(path, 'mapper.dart').write(data);
  }

  StatusHelper.generated(join(path, 'mapper.dart'));
}