59 lines
1.6 KiB
Dart
59 lines
1.6 KiB
Dart
import 'package:dio/dio.dart';
|
|
|
|
import '../models/calendar_event.dart';
|
|
import 'api_client.dart';
|
|
|
|
class EventsApi {
|
|
final Dio _dio;
|
|
const EventsApi(this._dio);
|
|
|
|
/// GET /api/events?from={iso}&to={iso}
|
|
Future<List<CalendarEvent>> getEvents(DateTime from, DateTime to) async {
|
|
try {
|
|
final response = await _dio.get(
|
|
'/api/events',
|
|
queryParameters: {
|
|
'from': from.toUtc().toIso8601String(),
|
|
'to': to.toUtc().toIso8601String(),
|
|
},
|
|
);
|
|
final list = response.data as List<dynamic>;
|
|
return list
|
|
.map((e) => CalendarEvent.fromJson(e as Map<String, dynamic>))
|
|
.toList();
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
|
|
/// POST /api/events
|
|
Future<CalendarEvent> createEvent(Map<String, dynamic> payload) async {
|
|
try {
|
|
final response = await _dio.post('/api/events', data: payload);
|
|
return CalendarEvent.fromJson(response.data as Map<String, dynamic>);
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
|
|
/// PATCH /api/events/{id}
|
|
Future<CalendarEvent> updateEvent(
|
|
int id, Map<String, dynamic> fields) async {
|
|
try {
|
|
final response = await _dio.patch('/api/events/$id', data: fields);
|
|
return CalendarEvent.fromJson(response.data as Map<String, dynamic>);
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
|
|
/// DELETE /api/events/{id}
|
|
Future<void> deleteEvent(int id) async {
|
|
try {
|
|
await _dio.delete('/api/events/$id');
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
}
|