74 lines
2.1 KiB
Dart
74 lines
2.1 KiB
Dart
|
|
import 'dart:convert';
|
||
|
|
import 'package:http/http.dart' as http;
|
||
|
|
import '../core/constants.dart';
|
||
|
|
import '../models/booking.dart';
|
||
|
|
import 'auth_service.dart';
|
||
|
|
import 'booking_repository.dart';
|
||
|
|
|
||
|
|
export 'booking_repository.dart';
|
||
|
|
|
||
|
|
class BookingService implements BookingRepository {
|
||
|
|
final _auth = AuthService();
|
||
|
|
|
||
|
|
Future<Map<String, String>> _headers() async {
|
||
|
|
final token = await _auth.getToken();
|
||
|
|
return {
|
||
|
|
if (token != null) 'Authorization': 'Bearer $token',
|
||
|
|
'Accept': 'application/json',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<List<BookingActivity>> activities() async {
|
||
|
|
final res = await http.get(
|
||
|
|
Uri.parse('$kApiBase/activities/book'),
|
||
|
|
headers: await _headers(),
|
||
|
|
);
|
||
|
|
if (res.statusCode == 200) {
|
||
|
|
final list = jsonDecode(res.body) as List<dynamic>;
|
||
|
|
return list
|
||
|
|
.map((e) => BookingActivity.fromJson(e as Map<String, dynamic>))
|
||
|
|
.toList();
|
||
|
|
}
|
||
|
|
throw Exception('Error ${res.statusCode}');
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<BookingGrid> grid({required int activityId, required String date}) async {
|
||
|
|
final uri = Uri.parse('$kApiBase/booking-admin/grid').replace(
|
||
|
|
queryParameters: {
|
||
|
|
'fk_i_activity_id': activityId.toString(),
|
||
|
|
'd_date': date,
|
||
|
|
},
|
||
|
|
);
|
||
|
|
final res = await http.get(uri, headers: await _headers());
|
||
|
|
if (res.statusCode == 200) {
|
||
|
|
return BookingGrid.fromJson(jsonDecode(res.body) as Map<String, dynamic>);
|
||
|
|
}
|
||
|
|
throw Exception('Error ${res.statusCode}');
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Future<BookingDetail?> singleBooking({
|
||
|
|
required int activityId,
|
||
|
|
required String date,
|
||
|
|
required int slotIndex,
|
||
|
|
required int courtIndex,
|
||
|
|
}) async {
|
||
|
|
final uri =
|
||
|
|
Uri.parse('$kApiBase/booking-admin/single').replace(queryParameters: {
|
||
|
|
'fk_i_activity_id': activityId.toString(),
|
||
|
|
'd_date': date,
|
||
|
|
'hour': slotIndex.toString(),
|
||
|
|
'aPos[1]': courtIndex.toString(),
|
||
|
|
});
|
||
|
|
final res = await http.get(uri, headers: await _headers());
|
||
|
|
if (res.statusCode == 200) {
|
||
|
|
final data = jsonDecode(res.body) as Map<String, dynamic>;
|
||
|
|
if (data.isEmpty) return null;
|
||
|
|
return BookingDetail.fromJson(data);
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|