147 lines
4.1 KiB
Dart
147 lines
4.1 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:docuframe/docuframe.dart' as df;
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:hl_lieferservice/services/erpframe.dart';
|
|
|
|
import '../../../dto/basic_response.dart';
|
|
import '../../../dto/car_add.dart';
|
|
import '../../../dto/car_add_response.dart';
|
|
import '../../../dto/car_get_response.dart';
|
|
import '../../../model/car.dart';
|
|
|
|
class CarService extends ErpFrameService {
|
|
CarService({required super.config});
|
|
|
|
Future<Car> addCar(String plate, int teamId) async {
|
|
df.LoginSession? session;
|
|
|
|
try {
|
|
session = await getSession();
|
|
df.DocuFrameMacroResponse response =
|
|
await df.Macro(config: dfConfig, session: session).execute(
|
|
"_web_addCar",
|
|
parameter: CarAddDTO.make(teamId, plate).toJson()
|
|
as Map<String, dynamic>);
|
|
|
|
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
|
debugPrint(responseJson.toString());
|
|
CarAddResponseDTO responseDto = CarAddResponseDTO.fromJson(responseJson);
|
|
|
|
if (responseDto.succeeded == true) {
|
|
return Car(
|
|
id: int.parse(responseDto.car.id), plate: responseDto.car.plate);
|
|
} else {
|
|
throw responseDto.message;
|
|
}
|
|
} catch (e, st) {
|
|
debugPrint("ERROR WHILE ADDING CAR");
|
|
debugPrint(e.toString());
|
|
debugPrint(st.toString());
|
|
|
|
rethrow;
|
|
} finally {
|
|
await logout(session);
|
|
}
|
|
}
|
|
|
|
Future<void> editCar(Car car) async {
|
|
df.LoginSession? session;
|
|
|
|
try {
|
|
session = await getSession();
|
|
|
|
debugPrint(car.plate);
|
|
debugPrint(car.id.toString());
|
|
|
|
df.DocuFrameMacroResponse response =
|
|
await df.Macro(config: dfConfig, session: session).execute(
|
|
"_web_editCar",
|
|
parameter: {"id": car.id, "plate": car.plate});
|
|
|
|
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
|
debugPrint(responseJson.toString());
|
|
|
|
BasicResponseDTO responseDto = BasicResponseDTO.fromJson(responseJson);
|
|
|
|
if (responseDto.succeeded == true) {
|
|
return;
|
|
} else {
|
|
throw responseDto.message;
|
|
}
|
|
} catch (e, st) {
|
|
debugPrint("ERROR WHILE EDITING CAR ${car.id}");
|
|
debugPrint("$e");
|
|
debugPrint(st.toString());
|
|
|
|
rethrow;
|
|
} finally {
|
|
await logout(session);
|
|
}
|
|
}
|
|
|
|
Future<void> removeCar(int carId, int teamId) async {
|
|
df.LoginSession? session;
|
|
|
|
try {
|
|
session = await getSession();
|
|
df.DocuFrameMacroResponse response =
|
|
await df.Macro(config: dfConfig, session: session).execute(
|
|
"_web_removeCar",
|
|
parameter: {"team_id": teamId, "id": carId});
|
|
|
|
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
|
debugPrint(responseJson.toString());
|
|
BasicResponseDTO responseDto = BasicResponseDTO.fromJson(responseJson);
|
|
|
|
if (responseDto.succeeded == true) {
|
|
return;
|
|
} else {
|
|
throw responseDto.message;
|
|
}
|
|
} catch (e, st) {
|
|
debugPrint("ERROR WHILE REMOVING CAR");
|
|
debugPrint(e.toString());
|
|
debugPrint(st.toString());
|
|
|
|
rethrow;
|
|
} finally {
|
|
await logout(session);
|
|
}
|
|
}
|
|
|
|
Future<List<Car>> getCars(int teamId) async {
|
|
df.LoginSession? session;
|
|
|
|
try {
|
|
session = await getSession();
|
|
df.DocuFrameMacroResponse response =
|
|
await df.Macro(config: dfConfig, session: session)
|
|
.execute("_web_getCars", parameter: {"team_id": teamId});
|
|
|
|
debugPrint(teamId.toString());
|
|
|
|
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
|
debugPrint("RESPONSE");
|
|
debugPrint(responseJson.toString());
|
|
CarGetResponseDTO responseDto = CarGetResponseDTO.fromJson(responseJson);
|
|
|
|
if (responseDto.succeeded == true) {
|
|
return responseDto.cars!
|
|
.map((carDto) => Car(id: int.parse(carDto.id), plate: carDto.plate))
|
|
.toList();
|
|
} else {
|
|
throw responseDto.message;
|
|
}
|
|
} catch (e, st) {
|
|
debugPrint("ERROR WHILE FETCHING CARS");
|
|
debugPrint(e.toString());
|
|
debugPrint(st.toString());
|
|
|
|
rethrow;
|
|
} finally {
|
|
await logout(session);
|
|
}
|
|
}
|
|
}
|