2023-04-07 15:56:17 +12:00
|
|
|
import 'package:path/path.dart';
|
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
|
|
|
|
class GymSet {
|
2023-04-08 14:05:01 +12:00
|
|
|
int? id;
|
2023-04-07 15:56:17 +12:00
|
|
|
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 {
|
2023-04-08 14:05:01 +12:00
|
|
|
static const _databaseName = "gym_set_database.db";
|
|
|
|
static const _databaseVersion = 1;
|
|
|
|
static const table = 'gym_sets';
|
|
|
|
static const columnId = '_id';
|
|
|
|
static const columnName = 'name';
|
|
|
|
static const columnReps = 'reps';
|
|
|
|
static const columnWeight = 'weight';
|
|
|
|
static const columnCreated = 'created';
|
2023-04-07 15:56:17 +12:00
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|