Files
rog_app/lib/utils/database_helper.dart
Mohamed Nouffer 244b7eb9ac refactoring
2022-07-30 20:42:44 +05:30

124 lines
3.5 KiB
Dart

import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:rogapp/model/location.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
class DatabaseHelper{
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
static Database? _database;
Future<Database> get database async => _database ??= await _initDatabase();
Future<Database> _initDatabase() async {
Directory documentDirectory = await getApplicationDocumentsDirectory();
String path = join(documentDirectory.path, 'rog.db');
// return await openDatabase(
// path,
// version: 1,
// onCreate: _onCreate,
// );
return openDatabase(join(await getDatabasesPath(), 'rog.db',), version: 1, onCreate: _onCreate);
}
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE destination(
location_id INTEGER PRIMARY KEY,
location_name TEXT,
category TEXT,
zip TEXT,
address TEXT,
prefecture TEXT,
area TEXT,
city TEXT,
latitude REAL,
longitude REAL,
photos TEXT,
fax TEXT,
email TEXT,
facility TEXT,
remark TEXT,
tags TEXT,
event_name TEXT,
event_active INTEGER,
hidden_location INTEGER,
auto_checkin INTEGER,
checkin_radius INTEGER,
checkin_point INTEGER,
buy_point INTEGER,
evaluation_value TEXT,
shop_closed INTEGER,
shop_shutdown INTEGER,
opening_hours_mon TEXT,
opening_hours_tue TEXT,
opening_hours_wed TEXT,
opening_hours_thu TEXT,
opening_hours_fri TEXT,
opening_hours_sat TEXT,
opening_hours_sun TEXT
parammeters TEXT
)
''');
}
Future<List<Location>> getDestinations() async {
Database db = await instance.database;
var dest = await db.query('destination');
List<Location> destList = dest.isNotEmpty ?
dest.map((e) => Location.fromMap(e)).toList() : [];
print("--------- ${destList}");
return destList;
}
Future<List<Location>> getDestinationByLatLon(double lat, double lon) async {
Database db = await instance.database;
var dest = await db.query('destination', where: "lat = ${lat} and lon= ${lon}");
List<Location> destList = dest.isNotEmpty
? dest.map((e) => Location.fromMap(e)).toList() : [];
return destList;
}
Future<int> deleteDestination(int location_id) async {
Database db = await instance.database;
var dest = await db.delete('destination', where: "location_id = ${location_id}");
int ret = dest > 0 ? dest : -1;
return ret;
}
Future<bool>isAlreadyAvailable(int location_id) async{
Database db = await instance.database;
var dest = await db.delete('destination', where: "location_id = ${location_id}");
return dest > 0 ? true : false;
}
Future<int> insertDestination(Location dest) async {
Database db = await instance.database;
int res = await db.insert(
'destination',
dest.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
print("------ database helper insert ${res}-----------::::::::");
return res;
}
Future<int> updateAction(Location destination, bool checkin)async {
Database db = await instance.database;
int act = checkin == false ? 0 : 1;
Map<String, dynamic> row = {
"checkedin": act
};
return await db.update(
"destination",
row,
where: 'location_id = ?',
whereArgs: [destination.location_id]
);
}
}