65 lines
1.8 KiB
Dart
65 lines
1.8 KiB
Dart
import 'dart:io';
|
|
import 'package:lgcc/models/conferences_model.dart';
|
|
import 'package:path/path.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
// This is a singleton class that provides access to the database
|
|
class DBProvider {
|
|
static Database? _database;
|
|
static final DBProvider db = DBProvider._();
|
|
|
|
DBProvider._();
|
|
|
|
Future<Database?> get database async {
|
|
// If database exists, return database
|
|
if (_database != null) return _database;
|
|
|
|
// If database don't exists, create one
|
|
_database = await initDB();
|
|
|
|
return _database;
|
|
}
|
|
|
|
// Create the database and the Employee table
|
|
initDB() async {
|
|
Directory documentsDirectory = await getApplicationDocumentsDirectory();
|
|
final path = join(documentsDirectory.path, 'conferences.db');
|
|
|
|
return await openDatabase(path, version: 1, onOpen: (db) {},
|
|
onCreate: (Database db, int version) async {
|
|
await db.execute('CREATE TABLE Conferences('
|
|
'id INTEGER PRIMARY KEY,'
|
|
'title TEXT,'
|
|
'date TEXT,'
|
|
'activity TEXT,'
|
|
'duration TEXT,'
|
|
'place TEXT,'
|
|
'city TEXT,'
|
|
'state TEXT,'
|
|
'country TEXT,'
|
|
'thumb TEXT,'
|
|
'translations TEXT'
|
|
')');
|
|
});
|
|
}
|
|
|
|
// Insert conference on database
|
|
createConference(ConferencesModel newConference) async {
|
|
final db = await database;
|
|
final res = await db?.insert('Conferences', newConference.toJson());
|
|
|
|
return res;
|
|
}
|
|
|
|
Future<List<ConferencesModel>> getAllConferences() async {
|
|
final db = await database;
|
|
final res = await db?.rawQuery("SELECT * FROM Conferences");
|
|
|
|
List<ConferencesModel> list =
|
|
res!.isNotEmpty ? res.map((c) => ConferencesModel.fromJson(c)).toList() : [];
|
|
|
|
return list;
|
|
}
|
|
} |