From 0863e92e002c27c40ab49b37fc32507bedf79934 Mon Sep 17 00:00:00 2001 From: "J. A. Messias" Date: Mon, 2 Dec 2024 08:23:43 -0300 Subject: [PATCH] WIP --- .../molecules/modules/constants/index.dart | 1 + .../modules/constants/license_constants.dart | 44 +++++++++++++++++++ .../license_local_data_source.dart | 43 +----------------- .../modules/domain/entities/license.dart | 3 +- .../components/molecules/modules/index.dart | 1 + lib/shared/constants/index.dart | 1 + lib/shared/constants/keychain_constants.dart | 13 ++++++ .../helpers/database/database_helper.dart | 20 ++++----- .../helpers/storage/keychain_storage.dart | 3 +- lib/shared/index.dart | 0 10 files changed, 74 insertions(+), 55 deletions(-) create mode 100644 lib/shared/components/molecules/modules/constants/index.dart create mode 100644 lib/shared/components/molecules/modules/constants/license_constants.dart create mode 100644 lib/shared/constants/index.dart create mode 100644 lib/shared/constants/keychain_constants.dart create mode 100644 lib/shared/index.dart diff --git a/lib/shared/components/molecules/modules/constants/index.dart b/lib/shared/components/molecules/modules/constants/index.dart new file mode 100644 index 00000000..799c6845 --- /dev/null +++ b/lib/shared/components/molecules/modules/constants/index.dart @@ -0,0 +1 @@ +export 'license_constants.dart'; diff --git a/lib/shared/components/molecules/modules/constants/license_constants.dart b/lib/shared/components/molecules/modules/constants/license_constants.dart new file mode 100644 index 00000000..c57c07bb --- /dev/null +++ b/lib/shared/components/molecules/modules/constants/license_constants.dart @@ -0,0 +1,44 @@ + + const String tableLicense = 'license'; + + String get createLicenseTable => ''' + CREATE TABLE IF NOT EXISTS $tableLicense ( + key TEXT UNIQUE, + display TEXT, + expirationDate TEXT, + startDate TEXT, + quantity TEXT + ); + '''; + + String get deleteLicenseTable => 'DROP TABLE IF EXISTS $tableLicense;'; + + String get updatePetsHistoryTrigger => ''' + CREATE TRIGGER update_fre_hub_pets_history + AFTER INSERT ON $tableLicense + WHEN NEW.key = 'FRE-HUB-PETS' + BEGIN + INSERT OR REPLACE INTO $tableLicense (key, display, expirationDate, startDate, quantity) + VALUES ('FRE-HUB-PETS-HISTORY', NEW.display, NEW.expirationDate, NEW.startDate, NEW.quantity); + END; + '''; + + String get dropPetsHistoryTrigger => 'DROP TRIGGER IF EXISTS update_fre_hub_pets_history;'; + + String get updateDisplayTrigger => ''' + CREATE TRIGGER update_display_trigger + AFTER UPDATE ON $tableLicense + WHEN NEW.key IN ('FRE-HUB-OPENED-VISITS', 'FRE-HUB-VEHICLES', 'FRE-HUB-RESIDENTS', 'FRE-HUB-PETS') + BEGIN + UPDATE $tableLicense + SET display = CASE + WHEN (SELECT COUNT(*) FROM $tableLicense WHERE key IN ('FRE-HUB-OPENED-VISITS', 'FRE-HUB-VEHICLES', 'FRE-HUB-RESIDENTS', 'FRE-HUB-PETS') AND display = 'VISIVEL') > 0 + THEN 'VISIVEL' + ELSE 'INVISIVEL' + END + WHERE key = 'FRE-HUB-ABOUT-PROPERTY'; + END; + '''; + + String get dropDisplayTrigger => 'DROP TRIGGER IF EXISTS update_display_trigger;'; + diff --git a/lib/shared/components/molecules/modules/data/data_sources/license_local_data_source.dart b/lib/shared/components/molecules/modules/data/data_sources/license_local_data_source.dart index e9fb7a32..01a71a31 100644 --- a/lib/shared/components/molecules/modules/data/data_sources/license_local_data_source.dart +++ b/lib/shared/components/molecules/modules/data/data_sources/license_local_data_source.dart @@ -1,5 +1,6 @@ import 'dart:developer'; +import 'package:hub/shared/components/molecules/modules/index.dart'; import 'package:hub/shared/helpers/database/database_helper.dart'; import 'package:hub/shared/helpers/storage/base_storage.dart'; import 'package:sqflite/sqflite.dart'; @@ -19,48 +20,6 @@ class LicenseLocalDataSourceImpl implements LicenseLocalDataSource { factory LicenseLocalDataSourceImpl() => _instance; LicenseLocalDataSourceImpl._internal(); - static const String tableLicense = 'license'; - - static String get createLicenseTable => ''' - CREATE TABLE IF NOT EXISTS $tableLicense ( - key TEXT UNIQUE, - display TEXT, - expirationDate TEXT, - startDate TEXT, - quantity TEXT - ); - '''; - - static String get deleteLicenseTable => 'DROP TABLE IF EXISTS $tableLicense;'; - - static String get updatePetsHistoryTrigger => ''' - CREATE TRIGGER update_fre_hub_pets_history - AFTER INSERT ON $tableLicense - WHEN NEW.key = 'FRE-HUB-PETS' - BEGIN - INSERT OR REPLACE INTO $tableLicense (key, display, expirationDate, startDate, quantity) - VALUES ('FRE-HUB-PETS-HISTORY', NEW.display, NEW.expirationDate, NEW.startDate, NEW.quantity); - END; - '''; - - static String get dropPetsHistoryTrigger => 'DROP TRIGGER IF EXISTS update_fre_hub_pets_history;'; - - static String get updateDisplayTrigger => ''' - CREATE TRIGGER update_display_trigger - AFTER UPDATE ON $tableLicense - WHEN NEW.key IN ('FRE-HUB-OPENED-VISITS', 'FRE-HUB-VEHICLES', 'FRE-HUB-RESIDENTS', 'FRE-HUB-PETS') - BEGIN - UPDATE $tableLicense - SET display = CASE - WHEN (SELECT COUNT(*) FROM $tableLicense WHERE key IN ('FRE-HUB-OPENED-VISITS', 'FRE-HUB-VEHICLES', 'FRE-HUB-RESIDENTS', 'FRE-HUB-PETS') AND display = 'VISIVEL') > 0 - THEN 'VISIVEL' - ELSE 'INVISIVEL' - END - WHERE key = 'FRE-HUB-ABOUT-PROPERTY'; - END; - '''; - - static String get dropDisplayTrigger => 'DROP TRIGGER IF EXISTS update_display_trigger;'; Future init() async { await DatabaseStorage.instance.init(); diff --git a/lib/shared/components/molecules/modules/domain/entities/license.dart b/lib/shared/components/molecules/modules/domain/entities/license.dart index 98aa8797..50f71990 100644 --- a/lib/shared/components/molecules/modules/domain/entities/license.dart +++ b/lib/shared/components/molecules/modules/domain/entities/license.dart @@ -169,8 +169,7 @@ class License { startDate: '', quantity: 0, ), - ] - ); + ]); } } diff --git a/lib/shared/components/molecules/modules/index.dart b/lib/shared/components/molecules/modules/index.dart index aa560630..385ad97a 100644 --- a/lib/shared/components/molecules/modules/index.dart +++ b/lib/shared/components/molecules/modules/index.dart @@ -1,3 +1,4 @@ export 'data/index.dart'; export 'domain/index.dart'; export 'presentation/index.dart'; +export 'constants/index.dart'; \ No newline at end of file diff --git a/lib/shared/constants/index.dart b/lib/shared/constants/index.dart new file mode 100644 index 00000000..a810e4ce --- /dev/null +++ b/lib/shared/constants/index.dart @@ -0,0 +1 @@ +export 'keychain_constants.dart'; diff --git a/lib/shared/constants/keychain_constants.dart b/lib/shared/constants/keychain_constants.dart new file mode 100644 index 00000000..4a44fcfb --- /dev/null +++ b/lib/shared/constants/keychain_constants.dart @@ -0,0 +1,13 @@ + const String tableKeychain = 'keychain'; + + String get createKeychainTable => ''' + CREATE TABLE $tableKeychain ( + key TEXT UNIQUE, + value TEXT, + type TEXT, + updateAt TEXT, + resolvedAt TEXT, + createdAt TEXT + ); + '''; + String get deleteKeychainTable => 'DROP TABLE IF EXISTS $tableKeychain;'; \ No newline at end of file diff --git a/lib/shared/helpers/database/database_helper.dart b/lib/shared/helpers/database/database_helper.dart index 1311fbdc..29264dd3 100644 --- a/lib/shared/helpers/database/database_helper.dart +++ b/lib/shared/helpers/database/database_helper.dart @@ -1,5 +1,5 @@ -import 'package:hub/shared/components/molecules/modules/data/data_sources/license_local_data_source.dart'; -import 'package:hub/shared/components/molecules/modules/data/repositories/license_repository_impl.dart'; +import 'package:hub/shared/components/molecules/modules/index.dart'; +import 'package:hub/shared/constants/index.dart'; import 'package:hub/shared/helpers/storage/keychain_storage.dart'; import 'package:sqflite/sqflite.dart'; import 'package:path/path.dart'; @@ -38,33 +38,33 @@ class DatabaseStorage { Future _onCreate(Database database, int version) async { log('Creating database...'); - await database.execute(KeychainHelper.createKeychainTable); - await database.execute(LicenseLocalDataSourceImpl.createLicenseTable); + await database.execute(createKeychainTable); + await database.execute(createLicenseTable); if (version >= 2) { - await database.execute(LicenseLocalDataSourceImpl.updatePetsHistoryTrigger); + await database.execute(updatePetsHistoryTrigger); } if (version >= 3) { - await database.execute(LicenseLocalDataSourceImpl.updateDisplayTrigger); + await database.execute(updateDisplayTrigger); } } Future _onUpgrade(Database database, int oldVersion, int newVersion) async { log('Upgrading database from version $oldVersion to $newVersion...'); if (oldVersion < 2 && newVersion >= 2) { - await database.execute(LicenseLocalDataSourceImpl.updatePetsHistoryTrigger); + await database.execute(updatePetsHistoryTrigger); } if (oldVersion < 3 && newVersion >= 3) { - await database.execute(LicenseLocalDataSourceImpl.updateDisplayTrigger); + await database.execute(updateDisplayTrigger); } } Future _onDowngrade(Database database, int oldVersion, int newVersion) async { log('Downgrading database from version $oldVersion to $newVersion...'); if (oldVersion >= 3 && newVersion < 3) { - await database.execute(LicenseLocalDataSourceImpl.dropDisplayTrigger); + await database.execute(dropDisplayTrigger); } if (oldVersion >= 2 && newVersion < 2) { - await database.execute(LicenseLocalDataSourceImpl.dropPetsHistoryTrigger); + await database.execute(dropPetsHistoryTrigger); } } } diff --git a/lib/shared/helpers/storage/keychain_storage.dart b/lib/shared/helpers/storage/keychain_storage.dart index d41cd1e4..f89c5394 100644 --- a/lib/shared/helpers/storage/keychain_storage.dart +++ b/lib/shared/helpers/storage/keychain_storage.dart @@ -9,9 +9,10 @@ class KeychainHelper implements BaseStorage { static final KeychainHelper instance = KeychainHelper._(); - static const String tableKeychain = 'keychain'; bool _isInitialized = false; + static const String tableKeychain = 'keychain'; + static String get createKeychainTable => ''' CREATE TABLE $tableKeychain ( key TEXT UNIQUE, diff --git a/lib/shared/index.dart b/lib/shared/index.dart new file mode 100644 index 00000000..e69de29b