114 lines
3.7 KiB
Dart
114 lines
3.7 KiB
Dart
library notification_repository;
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:injectable/injectable.dart';
|
|
import '../../../../core/network/api_client.dart';
|
|
import '../models/notification_model.dart';
|
|
|
|
/// Interface du repository des notifications
|
|
abstract class NotificationRepository {
|
|
/// Notifications du membre connecté (GET /api/notifications/me)
|
|
Future<List<NotificationModel>> getMesNotifications();
|
|
Future<List<NotificationModel>> getMesNonLues();
|
|
Future<List<NotificationModel>> getNotificationsByMembre(String membreId);
|
|
Future<List<NotificationModel>> getNonLuesByMembre(String membreId);
|
|
Future<NotificationModel?> getNotificationById(String id);
|
|
Future<void> marquerCommeLue(String id);
|
|
}
|
|
|
|
/// Implémentation via /api/notifications
|
|
@LazySingleton(as: NotificationRepository)
|
|
class NotificationRepositoryImpl implements NotificationRepository {
|
|
final ApiClient _apiClient;
|
|
static const String _baseUrl = '/api/notifications';
|
|
|
|
NotificationRepositoryImpl(this._apiClient);
|
|
|
|
@override
|
|
Future<List<NotificationModel>> getMesNotifications() async {
|
|
try {
|
|
final response = await _apiClient.get('$_baseUrl/me');
|
|
if (response.statusCode == 200) {
|
|
final data = response.data;
|
|
final list = data is List ? data : (data['content'] as List? ?? []);
|
|
return list.map((e) => NotificationModel.fromJson(e as Map<String, dynamic>)).toList();
|
|
}
|
|
return [];
|
|
} on DioException catch (e) {
|
|
if (e.response?.statusCode == 404) return [];
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<List<NotificationModel>> getMesNonLues() async {
|
|
try {
|
|
final response = await _apiClient.get('$_baseUrl/me/non-lues');
|
|
if (response.statusCode == 200) {
|
|
final data = response.data;
|
|
final list = data is List ? data : (data['content'] as List? ?? []);
|
|
return list.map((e) => NotificationModel.fromJson(e as Map<String, dynamic>)).toList();
|
|
}
|
|
return [];
|
|
} on DioException catch (e) {
|
|
if (e.response?.statusCode == 404) return [];
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<List<NotificationModel>> getNotificationsByMembre(String membreId) async {
|
|
try {
|
|
final response = await _apiClient.get('$_baseUrl/membre/$membreId');
|
|
if (response.statusCode == 200) {
|
|
final data = response.data;
|
|
final list = data is List ? data : (data['content'] as List? ?? []);
|
|
return list
|
|
.map((e) => NotificationModel.fromJson(e as Map<String, dynamic>))
|
|
.toList();
|
|
}
|
|
return [];
|
|
} on DioException catch (e) {
|
|
if (e.response?.statusCode == 404) return [];
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<List<NotificationModel>> getNonLuesByMembre(String membreId) async {
|
|
try {
|
|
final response = await _apiClient.get('$_baseUrl/membre/$membreId/non-lues');
|
|
if (response.statusCode == 200) {
|
|
final data = response.data;
|
|
final list = data is List ? data : (data['content'] as List? ?? []);
|
|
return list
|
|
.map((e) => NotificationModel.fromJson(e as Map<String, dynamic>))
|
|
.toList();
|
|
}
|
|
return [];
|
|
} on DioException catch (e) {
|
|
if (e.response?.statusCode == 404) return [];
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<NotificationModel?> getNotificationById(String id) async {
|
|
try {
|
|
final response = await _apiClient.get('$_baseUrl/$id');
|
|
if (response.statusCode == 200) {
|
|
return NotificationModel.fromJson(response.data as Map<String, dynamic>);
|
|
}
|
|
return null;
|
|
} on DioException catch (e) {
|
|
if (e.response?.statusCode == 404) return null;
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<void> marquerCommeLue(String id) async {
|
|
await _apiClient.post('$_baseUrl/$id/marquer-lue');
|
|
}
|
|
}
|