unknown
12 months ago
6 changed files with 217 additions and 66 deletions
@ -0,0 +1,18 @@
|
||||
class IpDomain { |
||||
int? id; |
||||
String? ipOrDomain; |
||||
|
||||
IpDomain({this.id, this.ipOrDomain}); |
||||
|
||||
factory IpDomain.fromJson(Map<String, dynamic> json) { |
||||
return IpDomain( |
||||
id: json['id'] ? json['id'] : 0, |
||||
ipOrDomain: json['ipOrDomain'] ? json['ipOrDomain'] : '', |
||||
); |
||||
} |
||||
|
||||
Map<String, dynamic> toJson() => { |
||||
'id': id, |
||||
'ipOrDomain': ipOrDomain, |
||||
}; |
||||
} |
@ -0,0 +1,101 @@
|
||||
import 'package:sqflite/sqflite.dart'; |
||||
|
||||
import '../../../migrations/databasehelper.dart'; |
||||
import '../../../models/ip_domain_model.dart'; |
||||
|
||||
class ControllerLogin { |
||||
final conn = SqfliteDatabaseHelper.instance; |
||||
|
||||
Future<int> addData(IpDomain ipDomain) async { |
||||
var dbclient = await conn.db; |
||||
int result = 0; // Provide an initial value |
||||
try { |
||||
result = await dbclient! |
||||
.insert(SqfliteDatabaseHelper.ipDomainTable, ipDomain.toJson()); |
||||
} catch (e) { |
||||
print(e.toString()); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
Future<int> updateData(IpDomain ipDomain) async { |
||||
var dbclient = await conn.db; |
||||
late int result; |
||||
try { |
||||
result = await dbclient!.update( |
||||
SqfliteDatabaseHelper.ipDomainTable, |
||||
ipDomain.toJson(), |
||||
where: 'id=?', |
||||
whereArgs: [ipDomain.id], |
||||
); |
||||
} catch (e) { |
||||
print(e.toString()); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
Future<List> fetchData() async { |
||||
var dbclient = await conn.db; |
||||
List userList = []; |
||||
try { |
||||
List<Map<String, dynamic>> maps = await dbclient! |
||||
.query(SqfliteDatabaseHelper.ipDomainTable, orderBy: 'id DESC'); |
||||
for (var item in maps) { |
||||
userList.add(item); |
||||
} |
||||
} catch (e) { |
||||
print(e.toString()); |
||||
} |
||||
return userList; |
||||
} |
||||
|
||||
Future<List<IpDomain>> fetchDataId() async { |
||||
var dbclient = await conn.db; |
||||
List<IpDomain> ipDomainList = []; |
||||
try { |
||||
List<Map<String, dynamic>> maps = await dbclient! |
||||
.query(SqfliteDatabaseHelper.ipDomainTable, orderBy: 'id DESC'); |
||||
for (var item in maps) { |
||||
ipDomainList.add(IpDomain.fromJson(item)); |
||||
} |
||||
} catch (e) { |
||||
print(e.toString()); |
||||
} |
||||
return ipDomainList; |
||||
} |
||||
|
||||
// Future<void> deleteAllData() async { |
||||
// var dbClient = await conn.db; |
||||
// await dbClient!.delete(SqfliteDatabaseHelper.ipDomainTable); |
||||
// } |
||||
|
||||
Future<void> resetAutoIncrement() async { |
||||
var dbClient = await conn.db; |
||||
await dbClient!.execute( |
||||
'DELETE FROM SQLITE_SEQUENCE WHERE NAME = "${SqfliteDatabaseHelper.ipDomainTable}"'); |
||||
} |
||||
|
||||
Future<void> deleteAllData() async { |
||||
var dbClient = await conn.db; |
||||
|
||||
// Hapus semua data dari tabel |
||||
await dbClient!.delete(SqfliteDatabaseHelper.ipDomainTable); |
||||
|
||||
// Reset auto-increment to 1 |
||||
await resetAutoIncrement(); |
||||
} |
||||
|
||||
Future<void> addAllData(List<IpDomain> ipDomainList) async { |
||||
var dbclient = await conn.db; |
||||
Batch batch = dbclient!.batch(); |
||||
|
||||
for (var ipDomain in ipDomainList) { |
||||
batch.insert( |
||||
SqfliteDatabaseHelper.ipDomainTable, |
||||
ipDomain.toJson(), |
||||
); |
||||
} |
||||
|
||||
await batch.commit(); |
||||
} |
||||
} |
Loading…
Reference in new issue