This commit is contained in:
J. A. Messias 2024-11-13 14:18:40 -03:00
parent 32c0b96074
commit 6c72e17515
2 changed files with 28 additions and 14 deletions

View File

@ -33,16 +33,24 @@ class SQLiteStorage implements BaseStorage {
_onCreate(Database database, int version) async { _onCreate(Database database, int version) async {
log('Creating database...'); log('Creating database...');
try {
await database.execute(_keychain); await database.execute(_keychain);
await database.execute(_license); await database.execute(_license);
await initLicense(database); await setupLicense();
} catch (e) {
log('Tables keychain and license already exists');
}
} }
_onUpgrade(Database database, int oldVersion, int newVersion) async { _onUpgrade(Database database, int oldVersion, int newVersion) async {
log('Upgrading database from version $oldVersion to $newVersion...'); log('Upgrading database from version $oldVersion to $newVersion...');
if (oldVersion < 2) { if (oldVersion < 2) {
try {
await database.execute(_license); await database.execute(_license);
await initLicense(database); await setupLicense();
} catch (e) {
log('Table license already exists');
}
} }
} }
@ -69,7 +77,7 @@ class SQLiteStorage implements BaseStorage {
); );
'''; ''';
static Future<void> initLicense(Database database) async { static Future<void> setupLicense() async {
log('initLicense()'); log('initLicense()');
for (var element in LicenseStorageKey.values.map((e) => e.value)) { for (var element in LicenseStorageKey.values.map((e) => e.value)) {
await database.insert( await database.insert(

View File

@ -8,7 +8,6 @@ import 'package:hub/shared/helpers/sqlite_storage.dart';
import 'dart:convert'; import 'dart:convert';
import 'package:hub/shared/helpers/storage_helper.dart'; import 'package:hub/shared/helpers/storage_helper.dart';
import 'package:sqflite/sqflite.dart';
class LicenseService { class LicenseService {
static final LicenseService _instance = LicenseService._internal(); static final LicenseService _instance = LicenseService._internal();
@ -37,16 +36,22 @@ class LicenseService {
static Future<void> fetchLicenses() async { static Future<void> fetchLicenses() async {
try { try {
log('Obtendo licenças...'); log('Obtendo licenças...');
final ApiCallResponse response = await PhpGroup.testCall(); final ApiCallResponse response = await PhpGroup.getLicense();
final List<dynamic> responseBody = response.jsonBody; final dynamic responseBody = response.jsonBody;
log('Licenças obtidas: $responseBody');
if (responseBody == []) { if (response.jsonBody is! List<dynamic>) {
// throw Exception('Licensa ausente ou vazia.'); late final String error;
if (response.jsonBody is Map<String, dynamic>)
error = response.jsonBody['error_msg'];
else
error = 'Erro desconhecido';
throw Exception('Erro ao consultar licenças: $error');
} }
if (response.jsonBody is! List<dynamic> && response.jsonBody['error'] != null) { if (responseBody == []) {
final String error = response.jsonBody['error_msg'] ?? 'Erro desconhecido'; await SQLiteStorage.setupLicense();
throw Exception('Erro ao consultar licenças: $error');
} }
for (var element in responseBody) { for (var element in responseBody) {
@ -97,6 +102,7 @@ class LicenseService {
} }
} catch (e, s) { } catch (e, s) {
log('Erro ao obter licença', error: e, stackTrace: s); log('Erro ao obter licença', error: e, stackTrace: s);
await SQLiteStorage.setupLicense();
} }
} }