67 lines
1.7 KiB
Dart
67 lines
1.7 KiB
Dart
import 'dart:convert';
|
|
import 'dart:developer';
|
|
|
|
import 'package:hub/shared/helpers/base_storage.dart';
|
|
import 'package:hub/shared/helpers/database_storage.dart';
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
class LicenseDelegate implements BaseStorage {
|
|
LicenseDelegate._();
|
|
|
|
static final LicenseDelegate instance = LicenseDelegate._();
|
|
|
|
static const String tableLicense = 'license';
|
|
|
|
static String get createTableQuery => '''
|
|
CREATE TABLE $tableLicense (
|
|
key TEXT UNIQUE,
|
|
display TEXT,
|
|
expirationDate TEXT,
|
|
startDate TEXT,
|
|
quantity TEXT
|
|
);
|
|
''';
|
|
|
|
@override
|
|
Future<void> init() async {
|
|
await DatabaseStorage.instance.init();
|
|
}
|
|
|
|
@override
|
|
Future<String?> get(String key) async {
|
|
var response = await DatabaseStorage.database.query(tableLicense, where: 'key = ?', whereArgs: [key]);
|
|
if (response.isEmpty) {
|
|
return null;
|
|
}
|
|
return response.first['value'].toString();
|
|
}
|
|
|
|
@override
|
|
Future<void> set(String key, String value) async {
|
|
log('setLicense($key, $value)');
|
|
Map<String, dynamic> valueMap = jsonDecode(value);
|
|
log('String to Map<String, dynamic>: $value to $valueMap');
|
|
|
|
await DatabaseStorage.database.insert(
|
|
tableLicense,
|
|
{
|
|
'key': key,
|
|
'display': valueMap['display'],
|
|
'expirationDate': valueMap['expirationDate'],
|
|
'startDate': valueMap['startDate'],
|
|
'quantity': valueMap['quantity'],
|
|
},
|
|
conflictAlgorithm: ConflictAlgorithm.replace);
|
|
}
|
|
|
|
@override
|
|
Future<void> delete(String key) async {
|
|
await DatabaseStorage.database.delete(tableLicense, where: 'key = ?', whereArgs: [key]);
|
|
}
|
|
|
|
@override
|
|
Future<void> clearAll() async {
|
|
await DatabaseStorage.database.delete(tableLicense);
|
|
}
|
|
}
|