Files
checker/lib/db.dart

82 lines
2.4 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:async';
import 'dart:io';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
const String tableSession = "session";
const String columnMerchantID = "merchant_id"; // DIN code, который вводится при авторизации
const String columnToken = "token"; // Токен для pos. Приходит с бэкэнда.
const String columnPosID = "pos_id"; // идентификатор для создания токена на бэке.
const String columnDocID = "doc_id"; // идентификатор, для проведения покупки на бэкенде.
//{
// columnMerchantID: merchantID,
// columnToken: token,
// columnPosID: posID,
// columnDocID: docID
//}
/// База данных, для хранения временных данных (din, token, locale, etc.)
class SqliteHelper {
Database db;
Future open() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, "demo.db");
db = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
await db.execute('''create table session (
$columnMerchantID text primary key,
$columnToken text,
$columnPosID text,
$columnDocID integer)''');
});
}
Future insert(String merchantID, String posID, String token) async {
Map session = {
columnMerchantID: merchantID,
columnPosID: posID,
columnToken: token
};
return db.insert(tableSession, session);
}
Future<String> getToken() async {
Map session = await _getSession();
return session != null ? session[columnToken] : null;
}
Future<String> getMerchantID() async {
Map session = await _getSession();
return session != null ? session[columnMerchantID] : null;
}
Future<String> getPosID() async {
Map session = await _getSession();
return session != null ? session[columnPosID] : new DateTime.now().millisecondsSinceEpoch.toString();
}
Future<Map> _getSession() async {
List<Map> maps = await db.query(tableSession, columns: null);
if (maps.length > 0) {
return maps.first;
}
return null;
}
Future clear() async {
return await db.delete(tableSession, where: null);
}
Future close() async => db.close();
}