| 1 | import 'package:built_collection/built_collection.dart'; |
| 2 | import 'package:dio/dio.dart'; |
| 3 | import 'package:openapi/openapi.dart'; |
| 4 | import 'package:plane_pal/constants/main.dart'; |
| 5 | import 'package:plane_pal/utils/error.dart'; |
| 6 | |
| 7 | class ApiException implements Exception |
| 8 | final String message; |
| 9 | final int? statusCode; |
| 10 | |
| 11 | ApiException({required this |
| 12 | |
| 13 | @override |
| 14 | String toString() => |
| 15 | } |
| 16 | |
| 17 | class BookingService { |
| 18 | final Dio _dio; |
| 19 | final _flightApi = openapi.getFlightApi |
| 20 | |
| 21 | BookingService() : _dio = Dio |
| 22 | // Add interceptors for logging |
| 23 | _dio.interceptors.add(LogInterceptor |
| 24 | requestBody: true, |
| 25 | responseBody: true, |
| 26 | error: true, |
| 27 | )); |
| 28 | } |
| 29 | |
| 30 | Future<FlightBookingEntity> createFlightBooking |
| 31 | CreateFlightBookingDTO createBookingDto, |
| 32 | ) async { |
| 33 | try { |
| 34 | final response = await _flightApi. |
| 35 | createFlightBookingDTO: createBookingDto, |
| 36 | ); |
| 37 | return response.data!; |
| 38 | } catch (e) { |
| 39 | if (e is DioException) { |
| 40 | throw ApiException(message: getErrorMessage |
| 41 | } |
| 42 | throw ApiException( |
| 43 | message: 'Failed to create flight booking: $e', |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | Future<FlightBookingEntity> updateFlightBooking |
| 49 | String bookingId, |
| 50 | UpdateFlightBookingDTO updateBookingDto, |
| 51 | ) async { |
| 52 | try { |
| 53 | final response = await _flightApi. |
| 54 | bookingId: bookingId, |
| 55 | updateFlightBookingDTO: updateBookingDto, |
| 56 | ); |
| 57 | return response.data!; |
| 58 | } catch (e) { |
| 59 | if (e is DioException) { |
| 60 | throw ApiException(message: getErrorMessage |
| 61 | } |
| 62 | throw ApiException( |
| 63 | message: 'Failed to update flight booking: $e', |
| 64 | ); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | Future<void> deleteFlightBooking |
| 69 | try { |
| 70 | await _flightApi.flightControllerDeleteFlightBookingV1( |
| 71 | bookingId: bookingId, |
| 72 | ); |
| 73 | } catch (e) { |
| 74 | if (e is DioException) { |
| 75 | throw ApiException(message: getErrorMessage |
| 76 | } |
| 77 | throw ApiException( |
| 78 | message: 'Failed to delete flight booking: $e', |
| 79 | ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | Future<FlightBookingEntity> getFlightBooking |
| 84 | try { |
| 85 | final response = await _flightApi. |
| 86 | bookingId: bookingId, |
| 87 | ); |
| 88 | return response.data!; |
| 89 | } catch (e) { |
| 90 | if (e is DioException) { |
| 91 | throw ApiException(message: getErrorMessage |
| 92 | } |
| 93 | throw ApiException( |
| 94 | message: 'Failed to get flight booking: $e', |
| 95 | ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | Future<BuiltList<FlightBookingEntity |
| 100 | String flightId, |
| 101 | ) async { |
| 102 | try { |
| 103 | final response = |
| 104 | await _flightApi.flightControllerGetFlightBookingsByFlightIdV1( |
| 105 | flightId: flightId, |
| 106 | ); |
| 107 | return response.data!; |
| 108 | } catch (e) { |
| 109 | if (e is DioException) { |
| 110 | throw ApiException(message: getErrorMessage |
| 111 | } |
| 112 | throw ApiException( |
| 113 | message: 'Failed to get flight bookings: $e', |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |