createController method

Future<Map<String, dynamic>> createController(
  1. Map<String, dynamic> params
)

Create a new controller

Implementation

Future<Map<String, dynamic>> createController(
  Map<String, dynamic> params,
) async {
  try {
    final className = params['className'] as String;
    final route = params['route'] as String;
    final description = params['description'] as String?;
    final includeCRUD = params['includeCRUD'] as bool? ?? false;
    final customPath = params['filePath'] as String?;

    // Generate controller code
    final code = ControllerTemplate.generateController(
      className: className,
      route: route,
      description: description,
      includeRudMethods: includeCRUD,
    );

    // Determine file path
    final fileName = '${_toSnakeCase(className)}.dart';
    final filePath = customPath ??
        p.join(projectRoot, 'lib', 'src', 'controllers', fileName);

    // Ensure directory exists
    await FileUtils.ensureDirectory(p.dirname(filePath));

    // Check if file exists
    if (await FileUtils.fileExists(filePath)) {
      return {
        'success': false,
        'error': 'File already exists: $filePath',
      };
    }

    // Write file
    await FileUtils.writeFile(filePath, code);

    return {
      'success': true,
      'message': 'Controller created successfully',
      'filePath': FileUtils.getRelativePath(projectRoot, filePath),
      'absolutePath': filePath,
    };
  } catch (e) {
    return {
      'success': false,
      'error': e.toString(),
    };
  }
}