createDomainRepository method

void createDomainRepository(
  1. String pathPage,
  2. String pageName,
  3. String apiName,
  4. bool bodyList,
  5. bool responseList,
)

Implementation

void createDomainRepository(
  String pathPage,
  String pageName,
  String apiName,
  bool bodyList,
  bool responseList,
) {
  final apiClassName = apiName.pascalCase;
  final apiMethodName = apiName.camelCase;

  final path = join(pathPage, 'domain', 'repositories');
  DirectoryHelper.createDir(path);

  final bodyClass = getBodyClass(apiClassName, bodyList);
  final entityClass = getEntityClass(apiClassName, responseList);

  if (!exists(join(path, '${pageName}_repository.dart'))) {
    join(path, '${pageName}_repository.dart')
        .write('''import 'package:core/core.dart';

import '../../data/models/body/${apiName}_body.dart';
import '../entities/${apiName}_entity.dart';

abstract class ${pageName.pascalCase}Repository {
Future<Either<MorphemeFailure, $entityClass>> $apiMethodName($bodyClass body,{Map<String, String>? headers,});
}''');
  } else {
    String data =
        File(join(path, '${pageName}_repository.dart')).readAsStringSync();

    data = data.replaceAll(
        RegExp(r"import\s?'package:core\/core.dart';\n?\n?", multiLine: true),
        '''import 'package:core/core.dart';

import '../../data/models/body/${apiName}_body.dart';
import '../entities/${apiName}_entity.dart';''');

    data = data.replaceAll(RegExp(r'}$', multiLine: true),
        '''  Future<Either<MorphemeFailure, $entityClass>> $apiMethodName($bodyClass body,{Map<String, String>? headers,});
}''');

    join(path, '${pageName}_repository.dart').write(data);
  }

  StatusHelper.generated(join(path, '${pageName}_repository.dart'));
}