43 lines
1.2 KiB
Dart
43 lines
1.2 KiB
Dart
import '../../core/network/i_api_client.dart';
|
|
import 'models/inbox_message.dart';
|
|
|
|
abstract class InboxRepository {
|
|
Future<List<InboxMessage>> getMessages({bool? isRead});
|
|
Future<InboxMessage> markAsRead(String messageId);
|
|
}
|
|
|
|
class InboxRepositoryImpl implements InboxRepository {
|
|
final IApiClient _apiClient;
|
|
static const _prefix = '/api/v1/inbox/messages';
|
|
|
|
InboxRepositoryImpl(this._apiClient);
|
|
|
|
@override
|
|
Future<List<InboxMessage>> getMessages({bool? isRead}) async {
|
|
final queryParams = isRead != null ? '?is_read=$isRead' : '';
|
|
final response = await _apiClient.get<List<dynamic>>(
|
|
'$_prefix$queryParams',
|
|
);
|
|
final data = response.data;
|
|
if (data == null) {
|
|
throw StateError('Invalid getMessages response: empty payload');
|
|
}
|
|
return data
|
|
.whereType<Map<String, dynamic>>()
|
|
.map(InboxMessage.fromJson)
|
|
.toList(growable: false);
|
|
}
|
|
|
|
@override
|
|
Future<InboxMessage> markAsRead(String messageId) async {
|
|
final response = await _apiClient.patch<Map<String, dynamic>>(
|
|
'$_prefix/$messageId/read',
|
|
);
|
|
final data = response.data;
|
|
if (data == null) {
|
|
throw StateError('Invalid markAsRead response: empty payload');
|
|
}
|
|
return InboxMessage.fromJson(data);
|
|
}
|
|
}
|