-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37c16d0
commit 78d3237
Showing
6 changed files
with
221 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,18 @@ | ||
import 'package:byte_banck/models/contact.dart'; | ||
import 'package:byte_banck/database/dao/contact_dao.dart'; | ||
import 'package:path/path.dart'; | ||
import 'package:sqflite/sqflite.dart'; | ||
|
||
Future<Database> createDatabase() { | ||
Future<Database> getDatabase() { | ||
return getDatabasesPath().then((dbPath) { | ||
final String path = join(dbPath, 'bytebanck.db'); | ||
return openDatabase( | ||
path, | ||
onCreate: (database, version) { | ||
database.execute('CREATE TABLE contacts( ' | ||
'id INTEGER PRIMARY KEY, ' | ||
'full_name TEXT, ' | ||
'account_number INTEGER)'); | ||
database.execute(ContactDao.tableSql); | ||
}, | ||
version: 1, | ||
onDowngrade: onDatabaseDowngradeDelete, | ||
); | ||
}); | ||
} | ||
|
||
Future<int> save(Contact contact) { | ||
return createDatabase().then((database) { | ||
Map<String, dynamic> contactMap = {}; | ||
contactMap['full_name'] = contact.fullName; | ||
contactMap['account_number'] = contact.accountNumber; | ||
return database.insert('contacts', contactMap); | ||
}); | ||
} | ||
|
||
Future<List<Contact>> findAll() { | ||
return createDatabase().then((database) { | ||
return database.query('contacts').then((maps) { | ||
{ | ||
List<Contact> contacts = []; | ||
for (Map<String, dynamic> map in maps) { | ||
contacts.add( | ||
Contact( | ||
map['id'], | ||
map['full_name'], | ||
map['account_number'], | ||
), | ||
); | ||
} | ||
return contacts; | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import 'package:byte_banck/models/contact.dart'; | ||
|
||
import '../app_database.dart'; | ||
import 'package:sqflite_common/sqlite_api.dart'; | ||
|
||
class ContactDao { | ||
static const String _tableName = 'contacts'; | ||
static const String _id = 'id'; | ||
static const String _fullName = 'full_name'; | ||
static const String _accountNumber = 'account_number'; | ||
|
||
static const String tableSql = 'CREATE TABLE $_tableName( ' | ||
'$_id INTEGER PRIMARY KEY, ' | ||
'$_fullName TEXT, ' | ||
'$_accountNumber INTEGER)'; | ||
|
||
Future<int> save(Contact contact) async { | ||
final Database database = await getDatabase(); | ||
return database.insert(_tableName, _toMap(contact)); | ||
} | ||
|
||
Future<List<Contact>> findAll() async { | ||
final Database database = await getDatabase(); | ||
return _toList(await database.query(_tableName)); | ||
} | ||
|
||
Map<String, dynamic> _toMap(Contact contact) { | ||
Map<String, dynamic> contactMap = { | ||
_fullName: contact.fullName, | ||
_accountNumber: contact.accountNumber, | ||
}; | ||
return contactMap; | ||
} | ||
|
||
List<Contact> _toList(List<Map<String, Object?>> maps) { | ||
List<Contact> contacts = []; | ||
for (Map<String, dynamic> map in maps) { | ||
contacts.add( | ||
Contact( | ||
id: map[_id], | ||
fullName: map[_fullName], | ||
accountNumber: map[_accountNumber], | ||
), | ||
); | ||
} | ||
return contacts; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.