From b75903e3cbe3374f50c7ca65f07f7f9d3f7a4e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Fri, 29 Sep 2023 11:38:06 +0200 Subject: [PATCH] work in progress database --- lib/database/controller.dart | 81 +++++++++++++++++++++++ lib/database/database.dart | 120 ++--------------------------------- lib/model/base_entity.dart | 19 ------ lib/model/note.dart | 16 +++-- lib/model/promemoria.dart | 5 +- lib/pages/test.dart | 13 ++++ 6 files changed, 112 insertions(+), 142 deletions(-) create mode 100644 lib/database/controller.dart delete mode 100644 lib/model/base_entity.dart diff --git a/lib/database/controller.dart b/lib/database/controller.dart new file mode 100644 index 0000000..7e01b19 --- /dev/null +++ b/lib/database/controller.dart @@ -0,0 +1,81 @@ +import '../model/note.dart'; +import '../model/promemoria.dart'; +import 'database.dart'; + +class Controller { + NoteDatabase database = NoteDatabase.instance; + + Future> getAllNote() async { + final db = await database.database; + var notes = await db.query(noteTable); + List noteList = notes.map((e) => Note.fromJson(e)).toList(); + return noteList; + } + + Future> getAllPromemoria() async { + final db = await database.database; + var promemorias = await db.query(promemoriaTable); + List promemoriaList = + promemorias.map((e) => Promemoria.fromJson(e)).toList(); + return promemoriaList; + } + + Future getNoteById(int id) async { + final db = await database.database; + var note = await db.query(noteTable, where: 'id = ?', whereArgs: [id]); + return Note.fromJson(note.first); + } + + Future getPromemoriaById(int id) async { + final db = await database.database; + var promemoria = + await db.query(promemoriaTable, where: 'id = ?', whereArgs: [id]); + return Promemoria.fromJson(promemoria.first); + } + + //add note + void addNote(Note note) async { + final db = await database.database; + await db.execute(''' + INSERT INTO note ( + title, + creationDate, + lastModificationDate, + arrayPromemoria, + description + ) VALUES ( + '${note.title}', + '${note.creationDate}', + '${note.lastModificationDate}', + '${note.arrayPromemoria}', + '${note.description}' + ) +'''); + } + + //add Promemoria + void addPromemoria(Promemoria promemoria) async { + final db = await database.database; + await db.execute(''' + INSERT INTO promemoria ( + title, + creationDate, + lastModificationDate, + expirationDate, + arrayPromemoria, + description, + priority, + color + ) VALUES ( + '${promemoria.title}', + '${promemoria.creationDate}', + '${promemoria.lastModificationDate}', + '${promemoria.expirationDate}', + '${promemoria.arrayPromemoria}', + '${promemoria.description}', + '${promemoria.priority}', + '${promemoria.color}' + ) + '''); + } +} diff --git a/lib/database/database.dart b/lib/database/database.dart index 84d67ed..8597e3a 100644 --- a/lib/database/database.dart +++ b/lib/database/database.dart @@ -1,17 +1,12 @@ import 'package:path/path.dart'; import 'package:sqflite/sqflite.dart'; -// Models -import 'package:progetto_m335_flutter/model/note.dart'; import 'package:progetto_m335_flutter/model/promemoria.dart'; class NoteDatabase { static final NoteDatabase instance = NoteDatabase._init(); static Database? _database; - // Zero args constructor needed to extend this class - NoteDatabase(); - NoteDatabase._init(); Future get database async { @@ -29,11 +24,6 @@ class NoteDatabase { } Future _createDB(Database database, int version) async { - if (await isDatabaseExists()) { - print("Database already created"); - deleteDatabase(); - print("Database deleted"); - } await database.execute('''CREATE TABLE promemoria ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -64,22 +54,6 @@ class NoteDatabase { Future fillDemoData(Database database, int version) async { - var nota = Note(); - nota.setTitle("Nota 1"); - nota.setCreationDate("2023-09-56"); - nota.setLastModificationDate("2023-09-56"); - nota.setArrayPromemoria("1,2,3,4,5"); - nota.setDescription("Questo è un esempio di nota 1."); - - await createNote(database, nota); - - print(await readNote(1)); - - nota.setDescription("ciao"); - await updateNote (nota); - - print(await readNote (1)); - // Add fake categories await database.execute(''' INSERT INTO note ( @@ -92,6 +66,7 @@ class NoteDatabase { 'Nota 2', '2023-09-28', '2023-09-28', + '1,1', 'Questo è un esempio di nota 2.' ) '''); @@ -107,11 +82,11 @@ class NoteDatabase { 'Nota 2', '2023-09-28', '2023-09-28', + '1,1', 'Questo è un esempio di nota 2.' ) '''); - // Add currencies await database.execute(''' INSERT INTO promemoria ( title, @@ -127,6 +102,7 @@ class NoteDatabase { '2023-09-27', '2023-09-27', '2023-10-05', + '2,1', 'Questo è un esempio di promemoria 1.', 'Alta', 'Rosso' @@ -149,6 +125,7 @@ class NoteDatabase { '2023-09-28', '2023-09-28', '2023-10-10', + '1,1', 'Questo è un esempio di promemoria 2.', 'Media', 'Verde' @@ -157,40 +134,6 @@ class NoteDatabase { print("Demo data inserted"); } - Future clearDatabase() async { - try { - await _database?.transaction((txn) async { - var batch = txn.batch(); - batch.delete(noteTable); - batch.delete(promemoriaTable); - await batch.commit(); - }); - } catch (error) { - throw Exception('DbBase.cleanDatabase: $error'); - } - } - - Future close() async { - final database = await instance.database; - database.close(); - } - - Future deleteDatabase() async { - final databasePath = await getDatabasesPath(); - final path = join(databasePath, 'note.db'); - databaseFactory.deleteDatabase(path); - } - - Future isDatabaseExists() async { - final databasePath = await getDatabasesPath(); - final path = join(databasePath, 'note.db'); - // Apre il database in modalità sola lettura. - Database database = await openDatabase(path, readOnly: true); - - // Restituisce true se il database è stato aperto correttamente, altrimenti false. - return database.isOpen; - } - Future> selectAllPromemoria() async { final db = await database; @@ -199,61 +142,6 @@ class NoteDatabase { return maps; } - Future> selectAllNotes() async { - final db = await database; - final List> maps = await db.query(noteTable); - return maps; - } - - Future createNote(Database database, Note note) async { - await database.insert( - 'note', - note.toMap(), - conflictAlgorithm: ConflictAlgorithm.replace, - ); - - print('note $note.title inserted'); - } - - Future createPromemoria( - Database database, Promemoria promemoria) async { - await database.insert( - 'promemoria', - promemoria.toMap(), - conflictAlgorithm: ConflictAlgorithm.replace, - ); - - print('promemoria $promemoria.title inserted'); - } - - Future> readPromemoria(int id) async { - final db = await database; - - final results = await db.query('SELECT * FROM note where id=$id'); - return results.first; - } - - Future> readNote(int id) async { - final db = await database; - - final results = await db.query('SELECT * FROM promemoria where id=$id'); - - return results.first; - } - - Future updatePromemoria(Promemoria promemoria) async { - final db = await database; - - await db.update('promemoria', promemoria.toMap(), - where: 'id = ?', whereArgs: [promemoria.getId()]); - } - - Future updateNote(Note note) async { - final db = await database; - - await db.update('note', note.toMap(), - where: 'id = ?', whereArgs: [note.getId()]); - } } diff --git a/lib/model/base_entity.dart b/lib/model/base_entity.dart deleted file mode 100644 index 1a0b7b5..0000000 --- a/lib/model/base_entity.dart +++ /dev/null @@ -1,19 +0,0 @@ -abstract class BaseEntity{ - static String id = 'id'; - static String title = 'Title'; - static String creationDate = 'CreationDate'; - static String lastEditDate = 'LastEditDate'; - - static String get getId{ - return id; - } - static String get getTitle{ - return title; - } - static String get getCreationDate{ - return creationDate; - } - static String get getLastEditDate{ - return lastEditDate; - } -} \ No newline at end of file diff --git a/lib/model/note.dart b/lib/model/note.dart index f6c8b12..4949b2c 100644 --- a/lib/model/note.dart +++ b/lib/model/note.dart @@ -1,13 +1,13 @@ -import 'base_entity.dart'; + const String noteTable = 'note'; class Note{ - String id; + String id=''; String title; String creationDate; String lastModificationDate; - String arrayPromemoria; + String? arrayPromemoria; String description; Map toMap() { @@ -30,6 +30,14 @@ class Note{ this.description, ); + Note.newConstructor( + this.title, + this.creationDate, + this.lastModificationDate, + this.arrayPromemoria, + this.description, + ); + String getId() { return id; } @@ -62,7 +70,7 @@ class Note{ lastModificationDate = lastModificationDate1; } - String getArrayPromemoria() { + String? getArrayPromemoria() { return arrayPromemoria; } diff --git a/lib/model/promemoria.dart b/lib/model/promemoria.dart index 40a5f2f..2b2dd24 100644 --- a/lib/model/promemoria.dart +++ b/lib/model/promemoria.dart @@ -1,4 +1,3 @@ -import 'base_entity.dart'; import 'identifiers/enum/color.dart'; import 'identifiers/enum/priority.dart'; @@ -10,7 +9,7 @@ class Promemoria { String creationDate; String lastModificationDate; String expirationDate; - String arrayPromemoria; + String? arrayPromemoria; String description; String priority; String color; @@ -90,7 +89,7 @@ class Promemoria { expirationDate = expirationDate1; } - String getArrayPromemoria() { + String? getArrayPromemoria() { return arrayPromemoria; } diff --git a/lib/pages/test.dart b/lib/pages/test.dart index 7cda250..552a6e4 100644 --- a/lib/pages/test.dart +++ b/lib/pages/test.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:progetto_m335_flutter/database/controller.dart'; import 'package:progetto_m335_flutter/database/database.dart'; import 'package:progetto_m335_flutter/model/note.dart'; @@ -13,6 +14,7 @@ class Test extends StatefulWidget { class _TestState extends State { NoteDatabase noteDatabase = NoteDatabase.instance; + Controller controller = Controller(); Future _pressed() async { print("Inserting demo data"); @@ -22,9 +24,20 @@ class _TestState extends State { } Future _printdata() async { + + + var nota = Note.newConstructor( + "nota 5", + "2023-09-56", + "2023-09-56", + "1,2,3,4,5", + "Questo è un esempio di nota 1."); + + final db = await noteDatabase.database; print("Printing data"); + print((await controller.getAllNote()).first); print(await db.query(noteTable)); print("Data printed"); }