61 lines
1.8 KiB
Dart
61 lines
1.8 KiB
Dart
import '../../core/network/i_api_client.dart';
|
|
import 'models/calendar_event.dart';
|
|
|
|
abstract class CalendarEventRepository {
|
|
Future<List<CalendarEvent>> listByRange({
|
|
required DateTime startAt,
|
|
required DateTime endAt,
|
|
});
|
|
|
|
Future<CalendarEvent> getById(String id);
|
|
Future<void> acceptSubscription(String itemId);
|
|
Future<void> rejectSubscription(String itemId);
|
|
}
|
|
|
|
class CalendarEventRepositoryImpl implements CalendarEventRepository {
|
|
final IApiClient _apiClient;
|
|
static const _prefix = '/api/v1/schedule-items';
|
|
|
|
CalendarEventRepositoryImpl(this._apiClient);
|
|
|
|
@override
|
|
Future<List<CalendarEvent>> listByRange({
|
|
required DateTime startAt,
|
|
required DateTime endAt,
|
|
}) async {
|
|
final start = Uri.encodeQueryComponent(startAt.toUtc().toIso8601String());
|
|
final end = Uri.encodeQueryComponent(endAt.toUtc().toIso8601String());
|
|
final response = await _apiClient.get<List<dynamic>>(
|
|
'$_prefix?start_at=$start&end_at=$end',
|
|
);
|
|
final data = response.data;
|
|
if (data == null) {
|
|
throw StateError('Invalid listByRange response: empty payload');
|
|
}
|
|
return data
|
|
.whereType<Map<String, dynamic>>()
|
|
.map(CalendarEvent.fromJson)
|
|
.toList(growable: false);
|
|
}
|
|
|
|
@override
|
|
Future<CalendarEvent> getById(String id) async {
|
|
final response = await _apiClient.get<Map<String, dynamic>>('$_prefix/$id');
|
|
final event = response.data;
|
|
if (event == null) {
|
|
throw StateError('Invalid getById response: empty payload');
|
|
}
|
|
return CalendarEvent.fromJson(event);
|
|
}
|
|
|
|
@override
|
|
Future<void> acceptSubscription(String itemId) {
|
|
return _apiClient.post<void>('$_prefix/$itemId/accept');
|
|
}
|
|
|
|
@override
|
|
Future<void> rejectSubscription(String itemId) {
|
|
return _apiClient.post<void>('$_prefix/$itemId/reject');
|
|
}
|
|
}
|