Commit 0825952e authored by Administrator's avatar Administrator

create table athletes, if it does not exist

parent 49f873a7
......@@ -2,13 +2,13 @@ import 'package:jaguar_query/jaguar_query.dart';
import 'package:flutter/material.dart';
import 'package:jaguar_orm/jaguar_orm.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:encrateia/utils/database.dart';
import 'package:encrateia/utils/database_helper.dart';
part 'athlete.jorm.dart';
class Athlete extends Model {
@PrimaryKey()
String id;
int id;
String firstName;
String lastName;
String state = "";
......@@ -24,6 +24,7 @@ class Athlete extends Model {
void set({firstName, lastName, state, stravaId, stravaUsername, photoPath}) {
this
..id = stravaId
..firstName = firstName
..lastName = lastName
..state = state
......@@ -49,8 +50,9 @@ class Athlete extends Model {
}
persist() async{
var connection = await Database.connection();
AthleteBean(connection).insert(this);
var databaseHelper = DatabaseHelper();
var adapter = await databaseHelper.adapter;
AthleteBean(adapter).insert(this);
}
static Athlete of(BuildContext context) => ScopedModel.of<Athlete>(context);
......
......@@ -7,19 +7,31 @@ part of 'athlete.dart';
// **************************************************************************
abstract class _AthleteBean implements Bean<Athlete> {
final id = StrField('id');
final id = IntField('id');
final firstName = StrField('first_name');
final lastName = StrField('last_name');
final state = StrField('state');
final stravaUsername = StrField('strava_username');
final photoPath = StrField('photo_path');
final stravaId = IntField('strava_id');
Map<String, Field> _fields;
Map<String, Field> get fields => _fields ??= {
id.name: id,
firstName.name: firstName,
lastName.name: lastName,
state.name: state,
stravaUsername.name: stravaUsername,
photoPath.name: photoPath,
stravaId.name: stravaId,
};
Athlete fromMap(Map map) {
Athlete model = Athlete();
model.id = adapter.parseValue(map['id']);
model.firstName = adapter.parseValue(map['first_name']);
model.lastName = adapter.parseValue(map['last_name']);
model.state = adapter.parseValue(map['state']);
model.stravaUsername = adapter.parseValue(map['strava_username']);
model.photoPath = adapter.parseValue(map['photo_path']);
model.stravaId = adapter.parseValue(map['strava_id']);
return model;
......@@ -32,11 +44,21 @@ abstract class _AthleteBean implements Bean<Athlete> {
if (only == null && !onlyNonNull) {
ret.add(id.set(model.id));
ret.add(firstName.set(model.firstName));
ret.add(lastName.set(model.lastName));
ret.add(state.set(model.state));
ret.add(stravaUsername.set(model.stravaUsername));
ret.add(photoPath.set(model.photoPath));
ret.add(stravaId.set(model.stravaId));
} else if (only != null) {
if (only.contains(id.name)) ret.add(id.set(model.id));
if (only.contains(firstName.name))
ret.add(firstName.set(model.firstName));
if (only.contains(lastName.name)) ret.add(lastName.set(model.lastName));
if (only.contains(state.name)) ret.add(state.set(model.state));
if (only.contains(stravaUsername.name))
ret.add(stravaUsername.set(model.stravaUsername));
if (only.contains(photoPath.name))
ret.add(photoPath.set(model.photoPath));
if (only.contains(stravaId.name)) ret.add(stravaId.set(model.stravaId));
} else /* if (onlyNonNull) */ {
if (model.id != null) {
......@@ -45,6 +67,18 @@ abstract class _AthleteBean implements Bean<Athlete> {
if (model.firstName != null) {
ret.add(firstName.set(model.firstName));
}
if (model.lastName != null) {
ret.add(lastName.set(model.lastName));
}
if (model.state != null) {
ret.add(state.set(model.state));
}
if (model.stravaUsername != null) {
ret.add(stravaUsername.set(model.stravaUsername));
}
if (model.photoPath != null) {
ret.add(photoPath.set(model.photoPath));
}
if (model.stravaId != null) {
ret.add(stravaId.set(model.stravaId));
}
......@@ -55,8 +89,12 @@ abstract class _AthleteBean implements Bean<Athlete> {
Future<void> createTable({bool ifNotExists = false}) async {
final st = Sql.create(tableName, ifNotExists: ifNotExists);
st.addStr(id.name, primary: true, isNullable: false);
st.addInt(id.name, primary: true, isNullable: false);
st.addStr(firstName.name, isNullable: false);
st.addStr(lastName.name, isNullable: false);
st.addStr(state.name, isNullable: false);
st.addStr(stravaUsername.name, isNullable: false);
st.addStr(photoPath.name, isNullable: false);
st.addInt(stravaId.name, isNullable: false);
return adapter.createTable(st);
}
......@@ -129,13 +167,13 @@ abstract class _AthleteBean implements Bean<Athlete> {
return;
}
Future<Athlete> find(String id,
Future<Athlete> find(int id,
{bool preload = false, bool cascade = false}) async {
final Find find = finder.where(this.id.eq(id));
return await findOne(find);
}
Future<int> remove(String id) async {
Future<int> remove(int id) async {
final Remove remove = remover.where(this.id.eq(id));
return adapter.remove(remove);
}
......
import 'package:sqflite/sqflite.dart';
import 'package:jaguar_query_sqflite/jaguar_query_sqflite.dart';
class Database {
static connection() async {
SqfliteAdapter adapter = new SqfliteAdapter(await getDatabasesPath());
await adapter.connect();
return adapter;
}
}
import 'package:encrateia/models/athlete.dart';
import 'package:sqflite/sqflite.dart';
import 'package:jaguar_query_sqflite/jaguar_query_sqflite.dart';
class DatabaseHelper {
static DatabaseHelper _databaseHelper;
static SqfliteAdapter _adapter;
DatabaseHelper._createInstance();
factory DatabaseHelper() {
if (_databaseHelper == null) {
_databaseHelper = DatabaseHelper._createInstance();
}
return _databaseHelper;
}
Future<SqfliteAdapter> get adapter async {
if (_adapter == null) _adapter = await getAdapter();
return _adapter;
}
Future<SqfliteAdapter> getAdapter() async {
var db = await openDatabase('encrateia.db');
var path = await getDatabasesPath();
SqfliteAdapter adapter = new SqfliteAdapter("$path $db");
await adapter.connect();
final athleteBean = AthleteBean(adapter);
try {
await db.query(athleteBean.tableName);
} catch (e) {
await athleteBean.createTable();
}
return adapter;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment