104 lines
2.8 KiB
Dart
104 lines
2.8 KiB
Dart
|
import 'package:path/path.dart';
|
||
|
import 'package:sqflite/sqflite.dart';
|
||
|
|
||
|
class GymSet {
|
||
|
int? id; // added id for SQLite
|
||
|
String name;
|
||
|
int reps;
|
||
|
int weight;
|
||
|
DateTime created;
|
||
|
|
||
|
GymSet(
|
||
|
{this.id,
|
||
|
required this.name,
|
||
|
required this.reps,
|
||
|
required this.weight,
|
||
|
required this.created});
|
||
|
}
|
||
|
|
||
|
class GymSetDatabaseHelper {
|
||
|
static final _databaseName = "gym_set_database.db";
|
||
|
static final _databaseVersion = 1;
|
||
|
|
||
|
static final table = 'gym_sets';
|
||
|
|
||
|
static final columnId = '_id';
|
||
|
static final columnName = 'name';
|
||
|
static final columnReps = 'reps';
|
||
|
static final columnWeight = 'weight';
|
||
|
static final columnCreated = 'created';
|
||
|
|
||
|
GymSetDatabaseHelper._privateConstructor();
|
||
|
static final GymSetDatabaseHelper instance =
|
||
|
GymSetDatabaseHelper._privateConstructor();
|
||
|
|
||
|
static Database? _database;
|
||
|
Future<Database> get database async {
|
||
|
if (_database != null) return _database!;
|
||
|
_database = await _initDatabase();
|
||
|
return _database!;
|
||
|
}
|
||
|
|
||
|
_initDatabase() async {
|
||
|
String path = join(await getDatabasesPath(), _databaseName);
|
||
|
return await openDatabase(path,
|
||
|
version: _databaseVersion, onCreate: _onCreate);
|
||
|
}
|
||
|
|
||
|
Future _onCreate(Database db, int version) async {
|
||
|
await db.execute('''
|
||
|
CREATE TABLE $table (
|
||
|
$columnId INTEGER PRIMARY KEY,
|
||
|
$columnName TEXT NOT NULL,
|
||
|
$columnReps INTEGER NOT NULL,
|
||
|
$columnWeight INTEGER NOT NULL,
|
||
|
$columnCreated TEXT NOT NULL
|
||
|
)
|
||
|
''');
|
||
|
}
|
||
|
|
||
|
Future<int> insert(GymSet gymSet) async {
|
||
|
Database db = await instance.database;
|
||
|
return await db.insert(table, {
|
||
|
columnName: gymSet.name,
|
||
|
columnReps: gymSet.reps,
|
||
|
columnWeight: gymSet.weight,
|
||
|
columnCreated: gymSet.created.toIso8601String()
|
||
|
});
|
||
|
}
|
||
|
|
||
|
Future<List<GymSet>> getAll() async {
|
||
|
Database db = await instance.database;
|
||
|
List<Map<String, dynamic>> result = await db.query(table);
|
||
|
return result
|
||
|
.map((gymSetMap) => GymSet(
|
||
|
id: gymSetMap[columnId],
|
||
|
name: gymSetMap[columnName],
|
||
|
reps: gymSetMap[columnReps],
|
||
|
weight: gymSetMap[columnWeight],
|
||
|
created: DateTime.parse(gymSetMap[columnCreated]),
|
||
|
))
|
||
|
.toList();
|
||
|
}
|
||
|
|
||
|
Future<int> update(GymSet gymSet) async {
|
||
|
Database db = await instance.database;
|
||
|
return await db.update(
|
||
|
table,
|
||
|
{
|
||
|
columnName: gymSet.name,
|
||
|
columnReps: gymSet.reps,
|
||
|
columnWeight: gymSet.weight,
|
||
|
columnCreated: gymSet.created.toIso8601String()
|
||
|
},
|
||
|
where: '$columnId = ?',
|
||
|
whereArgs: [gymSet.id]);
|
||
|
}
|
||
|
|
||
|
Future<int> delete(GymSet gymSet) async {
|
||
|
Database db = await instance.database;
|
||
|
return await db
|
||
|
.delete(table, where: '$columnId = ?', whereArgs: [gymSet.id]);
|
||
|
}
|
||
|
}
|