You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.3 KiB
Dart
81 lines
2.3 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../models/work_day.dart';
|
|
import '../models/work_interval.dart';
|
|
import '../utils/helpers.dart';
|
|
|
|
class BookingApi {
|
|
final String host;
|
|
final String path;
|
|
final http.Client client;
|
|
|
|
const BookingApi({
|
|
required this.client,
|
|
this.host = 'api.windesign.at',
|
|
this.path = '/workinghours.php',
|
|
});
|
|
|
|
Future<List<WorkDay>> getBookingList(DateTime monthStart) async {
|
|
final y = monthStart.year.toString().padLeft(4, '0');
|
|
final m = monthStart.month.toString().padLeft(2, '0');
|
|
final uri = Uri.https(host, path, {
|
|
'module': 'booking',
|
|
'function': 'getList',
|
|
'date': '$y-$m',
|
|
});
|
|
|
|
http.Response res;
|
|
try {
|
|
res = await client
|
|
.get(uri, headers: {'Accept': 'application/json'})
|
|
.timeout(const Duration(seconds: 15));
|
|
} on http.ClientException catch (e) {
|
|
throw Exception('ClientException: ${e.message} (uri=$uri)');
|
|
} on Object catch (e) {
|
|
throw Exception('Network error: $e (uri=$uri)');
|
|
}
|
|
|
|
if (res.statusCode != 200) {
|
|
throw Exception('HTTP ${res.statusCode}: ${res.body}');
|
|
}
|
|
|
|
final map = jsonDecode(res.body) as Map<String, dynamic>;
|
|
final List items = map['bookings'] ?? [];
|
|
final out = <WorkDay>[];
|
|
|
|
for (final row in items) {
|
|
final d = DateTime.parse(row['bookingDay'] as String);
|
|
final isWeekend = d.weekday == DateTime.saturday || d.weekday == DateTime.sunday;
|
|
final target = isWeekend ? 0 : 8 * 60;
|
|
|
|
final intervals = <WorkInterval>[];
|
|
TimeOfDay? p(dynamic v) => parseHHMM(v);
|
|
void addPair(String a, String b) {
|
|
final s = p(row[a]);
|
|
final e = p(row[b]);
|
|
if (s != null && e != null) intervals.add(WorkInterval(s, e));
|
|
}
|
|
addPair('come1', 'leave1');
|
|
addPair('come2', 'leave2');
|
|
addPair('come3', 'leave3');
|
|
addPair('come4', 'leave4');
|
|
addPair('come5', 'leave5');
|
|
|
|
final absence =
|
|
(row['code']?.toString().trim().isEmpty ?? true) ? null : row['code'].toString();
|
|
|
|
out.add(WorkDay(
|
|
date: d,
|
|
intervals: intervals,
|
|
targetMinutes: target,
|
|
absenceType: absence,
|
|
));
|
|
}
|
|
|
|
out.sort((a, b) => a.date.compareTo(b.date));
|
|
return out;
|
|
}
|
|
}
|