refactoring
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import '../../domain/entities/type_reference_entity.dart';
|
||||
import '../../domain/repositories/org_types_repository.dart';
|
||||
import '../datasources/org_types_remote_datasource.dart';
|
||||
|
||||
@LazySingleton(as: IOrgTypesRepository)
|
||||
class OrgTypesRepositoryImpl implements IOrgTypesRepository {
|
||||
final OrgTypesRemoteDataSource _dataSource;
|
||||
|
||||
const OrgTypesRepositoryImpl(this._dataSource);
|
||||
|
||||
@override
|
||||
Future<List<TypeReferenceEntity>> getOrgTypes() async {
|
||||
try {
|
||||
final models = await _dataSource.getOrgTypes();
|
||||
return models.map((m) => m.toEntity()).toList();
|
||||
} on DioException catch (e) {
|
||||
throw Exception('Erreur réseau: ${e.message}');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<TypeReferenceEntity> createOrgType({
|
||||
required String code,
|
||||
required String libelle,
|
||||
String? description,
|
||||
String? couleur,
|
||||
int ordreAffichage = 0,
|
||||
}) async {
|
||||
try {
|
||||
final model = await _dataSource.createOrgType({
|
||||
'domaine': 'TYPE_ORGANISATION',
|
||||
'code': code.toUpperCase().replaceAll(' ', '_'),
|
||||
'libelle': libelle,
|
||||
if (description != null) 'description': description,
|
||||
if (couleur != null) 'couleur': couleur,
|
||||
'ordreAffichage': ordreAffichage,
|
||||
'estDefaut': false,
|
||||
'estSysteme': false,
|
||||
'actif': true,
|
||||
});
|
||||
return model.toEntity();
|
||||
} on DioException catch (e) {
|
||||
throw Exception('Erreur réseau: ${e.message}');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<TypeReferenceEntity> updateOrgType({
|
||||
required String id,
|
||||
required String code,
|
||||
required String libelle,
|
||||
String? description,
|
||||
String? couleur,
|
||||
int ordreAffichage = 0,
|
||||
}) async {
|
||||
try {
|
||||
final model = await _dataSource.updateOrgType(id, {
|
||||
'domaine': 'TYPE_ORGANISATION',
|
||||
'code': code.toUpperCase().replaceAll(' ', '_'),
|
||||
'libelle': libelle,
|
||||
if (description != null) 'description': description,
|
||||
if (couleur != null) 'couleur': couleur,
|
||||
'ordreAffichage': ordreAffichage,
|
||||
});
|
||||
return model.toEntity();
|
||||
} on DioException catch (e) {
|
||||
throw Exception('Erreur réseau: ${e.message}');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteOrgType(String id) async {
|
||||
try {
|
||||
await _dataSource.deleteOrgType(id);
|
||||
} on DioException catch (e) {
|
||||
throw Exception('Erreur réseau: ${e.message}');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,8 +38,9 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
// Le backend retourne directement une liste [...]
|
||||
final List<dynamic> data = response.data as List<dynamic>;
|
||||
// Le backend retourne une réponse paginée {"data":[...],"total":0,...}
|
||||
final responseData = response.data as Map<String, dynamic>;
|
||||
final List<dynamic> data = (responseData['data'] as List<dynamic>?) ?? [];
|
||||
return data
|
||||
.map((json) => OrganizationModel.fromJson(json as Map<String, dynamic>))
|
||||
.toList();
|
||||
@@ -47,6 +48,7 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
throw Exception('Erreur lors de la récupération des organisations: ${response.statusCode}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.type == DioExceptionType.cancel) rethrow;
|
||||
throw Exception('Erreur réseau lors de la récupération des organisations: ${e.message}');
|
||||
} catch (e) {
|
||||
throw Exception('Erreur inattendue lors de la récupération des organisations: $e');
|
||||
@@ -59,13 +61,17 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
const String path = '$_baseUrl/mes';
|
||||
final response = await _apiClient.get(path);
|
||||
if (response.statusCode == 200) {
|
||||
final List<dynamic> data = response.data as List<dynamic>;
|
||||
final dynamic raw = response.data;
|
||||
final List<dynamic> data = raw is List<dynamic>
|
||||
? raw
|
||||
: ((raw as Map<String, dynamic>)['data'] as List<dynamic>? ?? []);
|
||||
return data
|
||||
.map((json) => OrganizationModel.fromJson(json as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
throw Exception('Erreur lors de la récupération de mes organisations: ${response.statusCode}');
|
||||
} on DioException catch (e) {
|
||||
if (e.type == DioExceptionType.cancel) rethrow;
|
||||
throw Exception('Erreur réseau lors de la récupération de mes organisations: ${e.message}');
|
||||
} catch (e) {
|
||||
throw Exception('Erreur inattendue lors de la récupération de mes organisations: $e');
|
||||
@@ -85,6 +91,7 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
throw Exception('Erreur lors de la récupération de l\'organisation: ${response.statusCode}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.type == DioExceptionType.cancel) rethrow;
|
||||
if (e.response?.statusCode == 404) {
|
||||
return null;
|
||||
}
|
||||
@@ -108,6 +115,7 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
throw Exception('Erreur lors de la création de l\'organisation: ${response.statusCode}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.type == DioExceptionType.cancel) rethrow;
|
||||
if (e.response?.statusCode == 400) {
|
||||
final errorData = e.response?.data;
|
||||
if (errorData is Map<String, dynamic> && errorData.containsKey('error')) {
|
||||
@@ -136,6 +144,7 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
throw Exception('Erreur lors de la mise à jour de l\'organisation: ${response.statusCode}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.type == DioExceptionType.cancel) rethrow;
|
||||
if (e.response?.statusCode == 404) {
|
||||
throw Exception('Organisation non trouvée');
|
||||
} else if (e.response?.statusCode == 400) {
|
||||
@@ -159,6 +168,7 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
throw Exception('Erreur lors de la suppression de l\'organisation: ${response.statusCode}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.type == DioExceptionType.cancel) rethrow;
|
||||
if (e.response?.statusCode == 404) {
|
||||
throw Exception('Organisation non trouvée');
|
||||
} else if (e.response?.statusCode == 400) {
|
||||
@@ -184,6 +194,7 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
throw Exception('Erreur lors de l\'activation de l\'organisation: ${response.statusCode}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.type == DioExceptionType.cancel) rethrow;
|
||||
if (e.response?.statusCode == 404) {
|
||||
throw Exception('Organisation non trouvée');
|
||||
}
|
||||
@@ -204,6 +215,7 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
throw Exception('Erreur lors de la suspension de l\'organisation: ${response.statusCode}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.type == DioExceptionType.cancel) rethrow;
|
||||
if (e.response?.statusCode == 404) {
|
||||
throw Exception('Organisation non trouvée');
|
||||
}
|
||||
@@ -216,7 +228,7 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
@override
|
||||
Future<List<OrganizationModel>> searchOrganizations({
|
||||
String? nom,
|
||||
TypeOrganization? type,
|
||||
String? type,
|
||||
StatutOrganization? statut,
|
||||
String? ville,
|
||||
String? region,
|
||||
@@ -231,7 +243,7 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
};
|
||||
|
||||
if (nom?.isNotEmpty == true) queryParams['nom'] = nom;
|
||||
if (type != null) queryParams['type'] = type.name.toUpperCase();
|
||||
if (type != null) queryParams['type'] = type.toUpperCase();
|
||||
if (statut != null) queryParams['statut'] = statut.name.toUpperCase();
|
||||
if (ville?.isNotEmpty == true) queryParams['ville'] = ville;
|
||||
if (region?.isNotEmpty == true) queryParams['region'] = region;
|
||||
@@ -243,8 +255,8 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
// Le backend retourne directement une liste [...]
|
||||
final List<dynamic> data = response.data as List<dynamic>;
|
||||
final responseData = response.data as Map<String, dynamic>;
|
||||
final List<dynamic> data = (responseData['data'] as List<dynamic>?) ?? [];
|
||||
return data
|
||||
.map((json) => OrganizationModel.fromJson(json as Map<String, dynamic>))
|
||||
.toList();
|
||||
@@ -252,6 +264,7 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
throw Exception('Erreur lors de la recherche d\'organisations: ${response.statusCode}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.type == DioExceptionType.cancel) rethrow;
|
||||
throw Exception('Erreur réseau lors de la recherche d\'organisations: ${e.message}');
|
||||
} catch (e) {
|
||||
throw Exception('Erreur inattendue lors de la recherche d\'organisations: $e');
|
||||
@@ -264,12 +277,16 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
final response = await _apiClient.get('$_baseUrl/$organizationId/membres');
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final List<dynamic> data = response.data as List<dynamic>;
|
||||
final dynamic raw = response.data;
|
||||
final List<dynamic> data = raw is List<dynamic>
|
||||
? raw
|
||||
: ((raw as Map<String, dynamic>)['data'] as List<dynamic>? ?? []);
|
||||
return data.map((e) => Map<String, dynamic>.from(e as Map)).toList();
|
||||
} else {
|
||||
throw Exception('Erreur lors de la récupération des membres: ${response.statusCode}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.type == DioExceptionType.cancel) rethrow;
|
||||
if (e.response?.statusCode == 404) {
|
||||
throw Exception('Organisation non trouvée');
|
||||
}
|
||||
@@ -296,6 +313,7 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
throw Exception('Erreur lors de la mise à jour de la configuration: ${response.statusCode}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.type == DioExceptionType.cancel) rethrow;
|
||||
if (e.response?.statusCode == 404) {
|
||||
throw Exception('Organisation non trouvée');
|
||||
} else if (e.response?.statusCode == 400) {
|
||||
@@ -321,6 +339,7 @@ class OrganizationRepositoryImpl implements IOrganizationRepository {
|
||||
throw Exception('Erreur lors de la récupération des statistiques: ${response.statusCode}');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (e.type == DioExceptionType.cancel) rethrow;
|
||||
throw Exception('Erreur réseau lors de la récupération des statistiques: ${e.message}');
|
||||
} catch (e) {
|
||||
throw Exception('Erreur inattendue lors de la récupération des statistiques: $e');
|
||||
|
||||
Reference in New Issue
Block a user