diff --git a/rescueapp/lib/bloc_patient/patient_bloc.dart b/rescueapp/lib/bloc_patient/patient_bloc.dart new file mode 100644 index 0000000000000000000000000000000000000000..95321ac66646b4bcf812041d02ce4116e008b3f0 --- /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 0000000000000000000000000000000000000000..b4f47c1587f2a5a8f60983989745c24897924114 --- /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 0000000000000000000000000000000000000000..0401323c339e90ba332e6aa117fecf3bf5222f87 --- /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 0000000000000000000000000000000000000000..f01bc37e564799b9a77db98a189a4369d02cf1e2 --- /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 0000000000000000000000000000000000000000..5859e1eb2a1c7b76980c97fc91768a1fa21a49e7 --- /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 0000000000000000000000000000000000000000..edc8e899eb4e6bad7f4dd14515f831385e90f951 --- /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 0000000000000000000000000000000000000000..3f59f8656fe485bb334f7cc203c4e7c0a38965ad --- /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