Adding sqflite and creating the database, mappers and User Field 🔨
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
0e265bbead
commit
7d02d4747a
@ -0,0 +1,4 @@
|
|||||||
|
class StatFields {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
class UserFields {
|
||||||
|
static final List<String> values = [
|
||||||
|
id, name, image, mail
|
||||||
|
];
|
||||||
|
|
||||||
|
static final String id = '_id';
|
||||||
|
static final String name = '_name';
|
||||||
|
static final String image = '_image';
|
||||||
|
static final String mail = '_mail';
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,26 @@
|
|||||||
|
import '../../model/Stat.dart';
|
||||||
|
import '../../model/User.dart';
|
||||||
|
import '../fields/StatFields.dart';
|
||||||
|
|
||||||
|
class UserMapper {
|
||||||
|
static Map<String, dynamic> toJson(User user) {
|
||||||
|
return {
|
||||||
|
UserFields.id: user.id,
|
||||||
|
UserFields.name: user.name,
|
||||||
|
UserFields.image: user.image,
|
||||||
|
UserFields.mail: user.mail,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static User toModel(Map<String, dynamic> json) {
|
||||||
|
return User(
|
||||||
|
json[UserFields.id],
|
||||||
|
json[UserFields.name],
|
||||||
|
json[UserFields.image],
|
||||||
|
json[UserFields.mail],
|
||||||
|
[],
|
||||||
|
[],
|
||||||
|
Stat(0,0,0,0,0,0,0,0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
import 'package:bowl_in/model/User.dart';
|
||||||
|
import 'package:path/path.dart';
|
||||||
|
import 'package:sqflite/sqflite.dart';
|
||||||
|
|
||||||
|
import '../fields/UserFields.dart';
|
||||||
|
import '../mappers/UserMapper.dart';
|
||||||
|
|
||||||
|
class UserDatabase {
|
||||||
|
static final UserDatabase instance = UserDatabase._init();
|
||||||
|
|
||||||
|
static Database? _database;
|
||||||
|
|
||||||
|
UserDatabase._init();
|
||||||
|
|
||||||
|
Future<Database> get database async {
|
||||||
|
if (_database != null) return _database!;
|
||||||
|
|
||||||
|
_database = await _initDB('user.db');
|
||||||
|
return _database!;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Database> _initDB(String filePath) async {
|
||||||
|
final dbPath = await getDatabasesPath();
|
||||||
|
final path = join(dbPath, filePath);
|
||||||
|
|
||||||
|
return await openDatabase(path, version: 1, onCreate: _createDB);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future _createDB(Database db, int version) async {
|
||||||
|
final idType = 'INTEGER PRIMARY KEY AUTOINCREMENT';
|
||||||
|
final textType = 'TEXT NOT NULL';
|
||||||
|
final boolType = 'BOOLEAN NOT NULL';
|
||||||
|
final integerType = 'INTEGER NOT NULL';
|
||||||
|
|
||||||
|
await db.execute('''
|
||||||
|
CREATE TABLE $tableUser (
|
||||||
|
${UserFields.id} $idType,
|
||||||
|
${UserFields.name} $boolType,
|
||||||
|
${UserFields.image} $integerType,
|
||||||
|
${UserFields.mail} $textType,
|
||||||
|
)
|
||||||
|
''');
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> createUser(User user) async {
|
||||||
|
final db = await instance.database;
|
||||||
|
await db.insert(tableUser, UserMapper.toJson(user));
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<User?> readUser(int id) async {
|
||||||
|
final db = await instance.database;
|
||||||
|
final result = await db.query(
|
||||||
|
tableUser,
|
||||||
|
where: '${UserFields.id} = ?',
|
||||||
|
whereArgs: [id]);
|
||||||
|
return result.isNotEmpty ? UserMapper.toModel(result.first) : null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<int> deleteUser(int id) async {
|
||||||
|
final db = await instance.database;
|
||||||
|
|
||||||
|
return await db.delete(
|
||||||
|
tableUser,
|
||||||
|
where: '${UserFields.id} = ?',
|
||||||
|
whereArgs: [id],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future close() async {
|
||||||
|
final db = await instance.database;
|
||||||
|
|
||||||
|
db.close();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue