Files
rog_app/lib/utils/database_helper.dart

124 lines
3.5 KiB
Dart
Raw Normal View History

2022-07-09 22:51:34 +05:30
import 'dart:io';
import 'package:path_provider/path_provider.dart';
2022-07-30 20:42:44 +05:30
import 'package:rogapp/model/location.dart';
2022-07-09 22:51:34 +05:30
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);
}
2022-07-30 20:42:44 +05:30
2022-07-09 22:51:34 +05:30
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE destination(
location_id INTEGER PRIMARY KEY,
2022-07-30 20:42:44 +05:30
location_name TEXT,
2022-07-09 22:51:34 +05:30
category TEXT,
2022-07-30 20:42:44 +05:30
zip TEXT,
address TEXT,
prefecture TEXT,
area TEXT,
city TEXT,
latitude REAL,
longitude REAL,
2022-07-09 22:51:34 +05:30
photos TEXT,
2022-07-30 20:42:44 +05:30
fax TEXT,
email TEXT,
facility TEXT,
remark TEXT,
tags TEXT,
event_name TEXT,
event_active INTEGER,
hidden_location INTEGER,
2022-07-14 23:10:24 +05:30
auto_checkin INTEGER,
2022-07-30 20:42:44 +05:30
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
2022-07-09 22:51:34 +05:30
)
''');
}
2022-07-30 20:42:44 +05:30
Future<List<Location>> getDestinations() async {
2022-07-09 22:51:34 +05:30
Database db = await instance.database;
var dest = await db.query('destination');
2022-07-30 20:42:44 +05:30
List<Location> destList = dest.isNotEmpty ?
dest.map((e) => Location.fromMap(e)).toList() : [];
2022-07-09 22:51:34 +05:30
print("--------- ${destList}");
return destList;
}
2022-07-30 20:42:44 +05:30
Future<List<Location>> getDestinationByLatLon(double lat, double lon) async {
2022-07-09 22:51:34 +05:30
Database db = await instance.database;
var dest = await db.query('destination', where: "lat = ${lat} and lon= ${lon}");
2022-07-30 20:42:44 +05:30
List<Location> destList = dest.isNotEmpty
? dest.map((e) => Location.fromMap(e)).toList() : [];
2022-07-09 22:51:34 +05:30
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;
}
2022-07-10 23:50:43 +05:30
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;
}
2022-07-30 20:42:44 +05:30
Future<int> insertDestination(Location dest) async {
2022-07-09 22:51:34 +05:30
Database db = await instance.database;
int res = await db.insert(
'destination',
dest.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
2022-07-10 23:50:43 +05:30
print("------ database helper insert ${res}-----------::::::::");
2022-07-09 22:51:34 +05:30
return res;
}
2022-07-30 20:42:44 +05:30
Future<int> updateAction(Location destination, bool checkin)async {
2022-07-14 23:10:24 +05:30
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]
);
}
2022-07-09 22:51:34 +05:30
}