102 lines
3.3 KiB
Dart
102 lines
3.3 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../../../core/api/api_client.dart';
|
|
import '../../../core/api/api_endpoints.dart';
|
|
import '../../../core/api/api_response.dart';
|
|
import '../../../core/auth/auth_provider.dart';
|
|
import '../domain/booking.dart';
|
|
|
|
/// Dépôt de données pour les réservations.
|
|
class BookingRepository {
|
|
BookingRepository(this._apiClient);
|
|
|
|
final ApiClient _apiClient;
|
|
|
|
Future<List<Booking>> fetchBookings() async {
|
|
final response = await _apiClient.get(ApiEndpoints.bookings);
|
|
final list = ApiResponse.list(response.data, 'bookings');
|
|
return list.map((json) => Booking.fromJson(json as Map<String, dynamic>)).toList();
|
|
}
|
|
|
|
Future<Booking> fetchBooking(String uuid) async {
|
|
final response = await _apiClient.get(ApiEndpoints.booking(uuid));
|
|
final json = ApiResponse.object(response.data, 'booking');
|
|
return Booking.fromJson(json);
|
|
}
|
|
|
|
Future<Booking> createBooking({
|
|
required String machineUuid,
|
|
required DateTime slotStart,
|
|
required DateTime slotEnd,
|
|
}) async {
|
|
try {
|
|
final response = await _apiClient.post(
|
|
ApiEndpoints.bookings,
|
|
data: {
|
|
'machine_uuid': machineUuid,
|
|
'slot_start': slotStart.toUtc().toIso8601String(),
|
|
'slot_end': slotEnd.toUtc().toIso8601String(),
|
|
},
|
|
);
|
|
final json = ApiResponse.object(response.data, 'booking');
|
|
return Booking.fromJson(json);
|
|
} on DioException catch (error) {
|
|
throw BookingException(ApiResponse.errorMessage(error, fallback: 'Impossible de réserver'));
|
|
}
|
|
}
|
|
|
|
Future<Booking> cancelBooking(String uuid) async {
|
|
try {
|
|
final response = await _apiClient.patch(ApiEndpoints.bookingCancel(uuid));
|
|
final json = ApiResponse.object(response.data, 'booking');
|
|
return Booking.fromJson(json);
|
|
} on DioException catch (error) {
|
|
throw BookingException(ApiResponse.errorMessage(error, fallback: 'Impossible d\'annuler'));
|
|
}
|
|
}
|
|
|
|
Future<Booking> moveBooking({
|
|
required String uuid,
|
|
required DateTime slotStart,
|
|
required DateTime slotEnd,
|
|
}) async {
|
|
try {
|
|
final response = await _apiClient.patch(
|
|
ApiEndpoints.bookingMove(uuid),
|
|
data: {
|
|
'slot_start': slotStart.toUtc().toIso8601String(),
|
|
'slot_end': slotEnd.toUtc().toIso8601String(),
|
|
},
|
|
);
|
|
final json = ApiResponse.object(response.data, 'booking');
|
|
return Booking.fromJson(json);
|
|
} on DioException catch (error) {
|
|
throw BookingException(ApiResponse.errorMessage(error, fallback: 'Impossible de modifier'));
|
|
}
|
|
}
|
|
}
|
|
|
|
class BookingException implements Exception {
|
|
BookingException(this.message);
|
|
final String message;
|
|
@override
|
|
String toString() => message;
|
|
}
|
|
|
|
final bookingRepositoryProvider = Provider<BookingRepository>((ref) {
|
|
return BookingRepository(ref.watch(apiClientProvider));
|
|
});
|
|
|
|
final bookingsProvider = FutureProvider<List<Booking>>((ref) async {
|
|
final token = ref.watch(authProvider.select((state) => state.accessToken));
|
|
if (token == null || token.isEmpty) {
|
|
throw StateError('Non authentifié');
|
|
}
|
|
return ref.watch(bookingRepositoryProvider).fetchBookings();
|
|
});
|
|
|
|
final bookingDetailProvider = FutureProvider.family<Booking, String>((ref, uuid) async {
|
|
return ref.watch(bookingRepositoryProvider).fetchBooking(uuid);
|
|
});
|