From 77fabc995d159fa4cc97f214a07d787c6a27d99b Mon Sep 17 00:00:00 2001 From: Trang Nguyen <thi_huyen_trang.nguyen@student.reutlingen-university.de> Date: Tue, 17 Nov 2020 16:23:31 +0100 Subject: [PATCH] add database to database_provide.dart --- rescueapp/lib/bloc_patient/patient_bloc.dart | 36 ++++++ rescueapp/lib/database/database_provide.dart | 108 ++++++++++++++++++ rescueapp/lib/events-patient/add_patient.dart | 10 ++ .../lib/events-patient/delete_patient.dart | 9 ++ .../lib/events-patient/patient_event.dart | 1 + rescueapp/lib/events-patient/set_patient.dart | 10 ++ .../lib/events-patient/update_patient.dart | 14 +++ 7 files changed, 188 insertions(+) create mode 100644 rescueapp/lib/bloc_patient/patient_bloc.dart create mode 100644 rescueapp/lib/database/database_provide.dart create mode 100644 rescueapp/lib/events-patient/add_patient.dart create mode 100644 rescueapp/lib/events-patient/delete_patient.dart create mode 100644 rescueapp/lib/events-patient/patient_event.dart create mode 100644 rescueapp/lib/events-patient/set_patient.dart create mode 100644 rescueapp/lib/events-patient/update_patient.dart diff --git a/rescueapp/lib/bloc_patient/patient_bloc.dart b/rescueapp/lib/bloc_patient/patient_bloc.dart new file mode 100644 index 0000000..95321ac --- /dev/null +++ b/rescueapp/lib/bloc_patient/patient_bloc.dart @@ -0,0 +1,36 @@ + + +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:rescueapp/events-patient/add_patient.dart'; +import 'package:rescueapp/events-patient/delete_patient.dart'; +import 'package:rescueapp/events-patient/patient_event.dart'; +import 'package:rescueapp/events-patient/set_patient.dart'; +import 'package:rescueapp/events-patient/update_patient.dart'; +import 'package:rescueapp/model-patient/patient.dart'; + +class PatientBloc extends Bloc<PatientEvent, List<Patient>> { + @override + List<Patient> get initialState => List<Patient>(); +// events wird definiert durch PatientEvent. + // mapEventToState wird aufgerufen um das bestimmte event mit einem bestimmten status zu definieren + @override + Stream<List<Patient>> mapEventToState(PatientEvent event) async* { + if (event is SetPatient) { + yield event.patientList; + } else if (event is AddPatient) { + List<Patient> newState = List.from(state); + if (event.newPatient != null) { + newState.add(event.newPatient); + } + yield newState; + } else if (event is DeletePatient) { + List<Patient> newState = List.from(state); + newState.removeAt(event.patientIndex); + yield newState; + } else if (event is UpdatePatient) { + List<Patient> newState = List.from(state); + newState[event.patientIndex] = event.newPatient; + yield newState; + } + } +} \ No newline at end of file diff --git a/rescueapp/lib/database/database_provide.dart b/rescueapp/lib/database/database_provide.dart new file mode 100644 index 0000000..b4f47c1 --- /dev/null +++ b/rescueapp/lib/database/database_provide.dart @@ -0,0 +1,108 @@ + +import 'package:path/path.dart'; +import 'package:rescueapp/model-patient/patient.dart'; +import 'package:sqflite/sqflite.dart'; +import 'package:sqflite/sqlite_api.dart'; + +//Einige asynchronous Operationen sind: +// Daten über ein Netzwerk abrufen +// Eine Datenbank zu schreiben +//Asynchronisierung +// Daten aus einem File lesen: Async means that this function is asynchronous and you might need to wait a bit to get its result. +// Await literally means - wait here until this function is finished and you will get its return value. +class DatabaseProvider { + static const String TABLE_PATIENT = "patient"; + static const String COLUMN_ID = "id"; + static const String COLUMN_NAME = "name"; + static const String COLUMN_BIRTHDAY = "geburtsdatum"; + static const String COLUMN_ISAMBILATORY = "isGehfähig"; + static const String COLUMN_IMAGEPATH = "imagePath"; + static const String COLUMN_CATEGORIE = "category"; + +// Constructor + DatabaseProvider._(); + static final DatabaseProvider db = DatabaseProvider._(); + + Database _database; + + Future <Database> get database async { + print("database getter called"); + + if (_database != null) { + return _database; + } + + _database = await createDatabase(); + + return _database; + } + + Future<Database> createDatabase() async { + String dbPath = await getDatabasesPath(); + + return await openDatabase( + join(dbPath, 'patientDB.db'), + version: 1, + onCreate: (Database database, int version) async { + print("Creating patient table"); + + await database.execute( + "CREATE TABLE $TABLE_PATIENT (" + "$COLUMN_ID INTEGER PRIMARY KEY," + "$COLUMN_NAME TEXT," + "$COLUMN_BIRTHDAY TEXT," + "$COLUMN_ISAMBILATORY INTEGER," + "$COLUMN_IMAGEPATH STRING," + "$COLUMN_CATEGORIE STRING," + + + ")", + ); + }, + ); + } + + Future<List<Patient>> getPatients() async { + final db = await database; + + var patients = await db + .query(TABLE_PATIENT, columns: [COLUMN_ID, COLUMN_NAME, COLUMN_IMAGEPATH,COLUMN_BIRTHDAY, COLUMN_ISAMBILATORY, COLUMN_CATEGORIE]); + + List<Patient> patientList = List<Patient>(); + + patients.forEach((currentPatient) { + Patient patient = Patient.fromMap(currentPatient); + + patientList.add(patient); + }); + + return patientList; + } + + Future<Patient> insert(Patient patient) async { + final db = await database; + patient.id = await db.insert(TABLE_PATIENT, patient.toMap()); + return patient; + } + + Future<int> delete(int id) async { + final db = await database; + + return await db.delete( + TABLE_PATIENT, + where: "id = ?", + whereArgs: [id], + ); + } + + Future<int> update(Patient patient) async { + final db = await database; + + return await db.update( + TABLE_PATIENT, + patient.toMap(), + where: "id = ?", + whereArgs: [patient.id], + ); + } +} \ No newline at end of file diff --git a/rescueapp/lib/events-patient/add_patient.dart b/rescueapp/lib/events-patient/add_patient.dart new file mode 100644 index 0000000..0401323 --- /dev/null +++ b/rescueapp/lib/events-patient/add_patient.dart @@ -0,0 +1,10 @@ +import 'package:rescueapp/events-patient/patient_event.dart'; +import 'package:rescueapp/model-patient/patient.dart'; + +class AddPatient extends PatientEvent { + Patient newPatient; + + AddPatient(Patient patient) { + newPatient = patient; + } +} diff --git a/rescueapp/lib/events-patient/delete_patient.dart b/rescueapp/lib/events-patient/delete_patient.dart new file mode 100644 index 0000000..f01bc37 --- /dev/null +++ b/rescueapp/lib/events-patient/delete_patient.dart @@ -0,0 +1,9 @@ +import 'package:rescueapp/events-patient/patient_event.dart'; + +class DeletePatient extends PatientEvent { + int patientIndex; + + DeletePatient(int index) { + patientIndex = index; + } +} \ No newline at end of file diff --git a/rescueapp/lib/events-patient/patient_event.dart b/rescueapp/lib/events-patient/patient_event.dart new file mode 100644 index 0000000..5859e1e --- /dev/null +++ b/rescueapp/lib/events-patient/patient_event.dart @@ -0,0 +1 @@ +abstract class PatientEvent {} \ No newline at end of file diff --git a/rescueapp/lib/events-patient/set_patient.dart b/rescueapp/lib/events-patient/set_patient.dart new file mode 100644 index 0000000..edc8e89 --- /dev/null +++ b/rescueapp/lib/events-patient/set_patient.dart @@ -0,0 +1,10 @@ +import 'package:rescueapp/events-patient/patient_event.dart'; +import 'package:rescueapp/model-patient/patient.dart'; + +class SetPatient extends PatientEvent { + List<Patient> patientList; + + SetPatient(List<Patient> patient) { + patientList = patient; + } +} \ No newline at end of file diff --git a/rescueapp/lib/events-patient/update_patient.dart b/rescueapp/lib/events-patient/update_patient.dart new file mode 100644 index 0000000..3f59f86 --- /dev/null +++ b/rescueapp/lib/events-patient/update_patient.dart @@ -0,0 +1,14 @@ +import 'package:rescueapp/events-patient/patient_event.dart'; +import 'package:rescueapp/model-patient/patient.dart'; + + + +class UpdatePatient extends PatientEvent { + Patient newPatient; + int patientIndex; + + UpdatePatient(int index, Patient patient) { + newPatient = patient; + patientIndex = index; + } +} \ No newline at end of file -- GitLab