createDataRepository method
void
createDataRepository()
Implementation
void createDataRepository(
String pathPage,
String pageName,
String apiName,
bool bodyList,
bool responseList,
) {
final apiClassName = apiName.pascalCase;
final apiMethodName = apiName.camelCase;
final path = join(pathPage, 'data', 'repositories');
DirectoryHelper.createDir(path);
final bodyClass = getBodyClass(apiClassName, bodyList);
final entityClass = getEntityClass(apiClassName, responseList);
final entityImpl = responseList
? 'data.map((e) => e.toEntity()).toList()'
: 'data.toEntity()';
if (!exists(join(path, '${pageName}_repository_impl.dart'))) {
join(path, '${pageName}_repository_impl.dart')
.write('''import 'package:core/core.dart';
import '../../domain/entities/${apiName}_entity.dart';
import '../../domain/repositories/${pageName}_repository.dart';
import '../datasources/${pageName}_remote_data_source.dart';
import '../models/body/${apiName}_body.dart';
import '../../mapper.dart';
class ${pageName.pascalCase}RepositoryImpl implements ${pageName.pascalCase}Repository {
${pageName.pascalCase}RepositoryImpl({
required this.remoteDataSource,
});
final ${pageName.pascalCase}RemoteDataSource remoteDataSource;
@override
Future<Either<MorphemeFailure, $entityClass>> $apiMethodName($bodyClass body,{Map<String, String>? headers,}) async {
try {
final data = await remoteDataSource.$apiMethodName(body, headers: headers,);
return Right($entityImpl);
} on MorphemeException catch (e) {
return Left(e.toMorphemeFailure());
} catch (e) {
return Left(InternalFailure(e.toString()));
}
}
}''');
} else {
String data = File(join(path, '${pageName}_repository_impl.dart'))
.readAsStringSync();
final isDataDatasourceAlready =
RegExp(r'remote_data_source\.dart').hasMatch(data);
final isDomainRepositoryAlready =
RegExp(r'repository\.dart').hasMatch(data);
data = data.replaceAll(
RegExp(r"import\s?'package:core\/core.dart';\n?\n?", multiLine: true),
'''import 'package:core/core.dart';
${isDataDatasourceAlready ? '' : "import '../datasources/${pageName}_remote_data_source.dart';"}
${isDomainRepositoryAlready ? '' : "import '../../domain/repositories/${pageName}_repository.dart';"}
import '../../domain/entities/${apiName}_entity.dart';
import '../models/body/${apiName}_body.dart';''');
final isEmpty = RegExp(r'remoteDataSource;(\s+)?}(\s+)?}').hasMatch(data);
data =
data.replaceAll(RegExp(r'}(\s+)?}(\s+)?}'), '''${isEmpty ? '' : '}}'}
@override
Future<Either<MorphemeFailure, $entityClass>> $apiMethodName($bodyClass body,{Map<String, String>? headers,}) async {
try {
final data = await remoteDataSource.$apiMethodName(body, headers: headers);
return Right($entityImpl);
} on MorphemeException catch (e) {
return Left(e.toMorphemeFailure());
} catch (e) {
return Left(InternalFailure(e.toString()));
}
}
}''');
join(path, '${pageName}_repository_impl.dart').write(data);
}
StatusHelper.generated(join(path, '${pageName}_repository_impl.dart'));
}