61 lines
2.0 KiB
Dart
61 lines
2.0 KiB
Dart
// * Shared method for saving main entries to the database
|
|
// * Handles both place and excursion entries
|
|
// * Supports:
|
|
// * - Creating new entries
|
|
// * - Converting templates to entries
|
|
// * - Updating existing entries
|
|
// * - Marking entries as sent to server
|
|
|
|
import 'package:fforte/enums/databases.dart';
|
|
import 'package:fforte/interfaces/i_db.dart';
|
|
import 'package:fforte/methods/excursion_db_helper.dart';
|
|
import 'package:fforte/methods/place_db_helper.dart';
|
|
|
|
/// Helper class for saving main entries to the database
|
|
class SaveMainEntryMethod {
|
|
/// Save or update a main entry in the database
|
|
/// @param entryData Map containing the entry data
|
|
/// @param isTemplate Whether this is being converted from a template
|
|
/// @param dbType The type of database (place/excursion)
|
|
/// @param sent Whether the entry has been sent to the server
|
|
/// @return ID of the saved entry
|
|
static Future<int> saveEntry({
|
|
required Map<String, String> entryData,
|
|
required bool isTemplate,
|
|
required DatabasesEnum dbType,
|
|
bool sent = false,
|
|
}) async {
|
|
// Select appropriate database helper
|
|
IDb? placeDB;
|
|
|
|
if (dbType == DatabasesEnum.place) {
|
|
placeDB = PlaceDBHelper();
|
|
} else if (dbType == DatabasesEnum.excursion) {
|
|
placeDB = ExcursionDBHelper();
|
|
}
|
|
|
|
// If converting from template, delete the template first
|
|
if (isTemplate) await placeDB!.deleteTemplateById(entryData["ID"]!);
|
|
|
|
// Handle new entry creation vs update
|
|
int entryId;
|
|
if (entryData["ID"] == "" || isTemplate) {
|
|
// Create new entry
|
|
entryData.remove("ID");
|
|
entryId = await placeDB!.addMainEntry(entryData);
|
|
// Commented out template deletion by CID
|
|
// await placeDB.deleteTemplateById(entryData["CID"]!);
|
|
} else {
|
|
// Update existing entry
|
|
entryId = await placeDB!.updateMainEntry(entryData);
|
|
}
|
|
|
|
// Update sent status if entry was sent to server
|
|
if (sent == true) {
|
|
placeDB.updateSent(entryId);
|
|
}
|
|
|
|
return entryId;
|
|
}
|
|
}
|