Umbau auf directus API Rest Service
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import '../helper/helper.dart' as helper;
|
||||
import '../models/locationiq_model.dart';
|
||||
import '../models/tank_model.dart';
|
||||
import '../services/appwrite_service.dart';
|
||||
//import '../models/tank_model.dart';
|
||||
//import '../services/appwrite_service.dart';
|
||||
import '../models/tank_guru_directus_model.dart';
|
||||
import '../services/directus_service.dart';
|
||||
import '../services/geolocation_service.dart';
|
||||
import '../services/location_iq_service.dart';
|
||||
import '../pages/home_view.dart';
|
||||
|
||||
class EditController extends GetxController {
|
||||
final AppwriteService appwriteService = AppwriteService();
|
||||
final DirectusService directusService = DirectusService();
|
||||
|
||||
// Form controllers
|
||||
final dateController = TextEditingController();
|
||||
@@ -23,8 +26,9 @@ class EditController extends GetxController {
|
||||
final calculatedTotal = '0.00'.obs;
|
||||
final isLoadingLocation = false.obs;
|
||||
|
||||
TankModel? editingTankModel;
|
||||
TankGuruDirectusModel? editingTankModel;
|
||||
LocationIQ? currentLocationIQ;
|
||||
String currentUserIdentId = helper.kCurrentUserIdentId; // Use the current user identId from helper
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
@@ -32,7 +36,7 @@ class EditController extends GetxController {
|
||||
|
||||
// Check if we're editing or creating new
|
||||
if (Get.arguments != null) {
|
||||
editingTankModel = Get.arguments as TankModel;
|
||||
editingTankModel = Get.arguments as TankGuruDirectusModel;
|
||||
isNewEntry.value = false;
|
||||
_loadExistingData();
|
||||
} else {
|
||||
@@ -47,12 +51,12 @@ class EditController extends GetxController {
|
||||
}
|
||||
|
||||
void _loadExistingData() {
|
||||
dateController.text = editingTankModel!.szDate;
|
||||
odometerController.text = editingTankModel!.szOdometer;
|
||||
litersController.text = editingTankModel!.szLiters;
|
||||
pricePerLiterController.text = editingTankModel!.szPricePerLiter;
|
||||
locationController.text = editingTankModel!.szLocation;
|
||||
calculatedTotal.value = editingTankModel!.szPriceTotal;
|
||||
dateController.text = editingTankModel!.tankDate!;
|
||||
odometerController.text = editingTankModel!.odometer!;
|
||||
litersController.text = editingTankModel!.liter!;
|
||||
pricePerLiterController.text = editingTankModel!.pricePerLiter!;
|
||||
locationController.text = editingTankModel!.standort!;
|
||||
calculatedTotal.value = editingTankModel!.szPriceTotal!;
|
||||
}
|
||||
|
||||
void _setDefaultDate() {
|
||||
@@ -147,26 +151,26 @@ class EditController extends GetxController {
|
||||
isLoading.value = true;
|
||||
|
||||
try {
|
||||
final userId = await appwriteService.getCurrentUserId();
|
||||
if (userId == null) {
|
||||
final userId = currentUserIdentId;
|
||||
if (userId == '') {
|
||||
throw Exception('Benutzer nicht eingeloggt');
|
||||
}
|
||||
|
||||
final data = {
|
||||
'userId': userId,
|
||||
'date': dateController.text,
|
||||
'odometer': odometerController.text,
|
||||
'liters': litersController.text.replaceAll(',', '.'),
|
||||
'pricePerLiter': pricePerLiterController.text.replaceAll(',', '.'),
|
||||
'location': locationController.text,
|
||||
'UserIdentId': userId,
|
||||
'TankDate': dateController.text,
|
||||
'Odometer': odometerController.text,
|
||||
'Liter': litersController.text.replaceAll(',', '.'),
|
||||
'PricePerLiter': pricePerLiterController.text.replaceAll(',', '.'),
|
||||
'Standort': locationController.text,
|
||||
};
|
||||
|
||||
bool success;
|
||||
if (isNewEntry.value) {
|
||||
success = await appwriteService.createDocumentInCollection(data);
|
||||
success = await directusService.createDocumentInTable(data);
|
||||
} else {
|
||||
success = await appwriteService.updateDocumentInCollection(
|
||||
editingTankModel!.szDocumentId,
|
||||
success = await directusService.updateDocumentInTable(
|
||||
editingTankModel!.iD,
|
||||
data,
|
||||
);
|
||||
}
|
||||
@@ -181,7 +185,7 @@ class EditController extends GetxController {
|
||||
backgroundColor: Colors.green[100],
|
||||
colorText: Colors.green[900],
|
||||
);
|
||||
Get.offAllNamed(HomePage.namedRoute);
|
||||
Get.offAllNamed(HomePage.namedRoute, arguments: {'userIdentId': userId});
|
||||
} else {
|
||||
throw Exception('Speichern fehlgeschlagen');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user