diff --git a/lib/database/database.dart b/lib/database/database.dart index 098ea74..84d67ed 100644 --- a/lib/database/database.dart +++ b/lib/database/database.dart @@ -62,8 +62,24 @@ class NoteDatabase { await fillDemoData(database, version); } - 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 ( @@ -80,7 +96,6 @@ class NoteDatabase { ) '''); - await database.execute(''' INSERT INTO note ( title, @@ -160,7 +175,6 @@ class NoteDatabase { database.close(); } - Future deleteDatabase() async { final databasePath = await getDatabasesPath(); final path = join(databasePath, 'note.db'); @@ -177,28 +191,6 @@ class NoteDatabase { return database.isOpen; } - - Future createNote(Database database, Note note) async { - await database.execute(''' - INSERT INTO note ( - title, - creationDate, - lastModificationDate, - arrayPromemoria, - description, - - ) VALUES ( - '$note.title}', - '$note.creationDate', - '$note.lastModificationDate', - '$note.arrayPromemoria.toString()', - '$note.description', - ) - '''); - - print('note $note.title inserted'); - } - Future> selectAllPromemoria() async { final db = await database; @@ -207,7 +199,6 @@ class NoteDatabase { return maps; } - Future> selectAllNotes() async { final db = await database; @@ -216,35 +207,27 @@ class NoteDatabase { return maps; } + Future createNote(Database database, Note note) async { + await database.insert( + 'note', + note.toMap(), + conflictAlgorithm: ConflictAlgorithm.replace, + ); - Future createPromemoria(Database database, - Promemoria promemoria) async { - await database.execute(''' - INSERT INTO promemoria ( - title, - creationDate, - lastModificationDate, - expirationDate, - arrayPromemoria, - description, - priority, - color - ) VALUES ( - '$promemoria.title', - '$promemoria.creationDate', - '$promemoria.lastModificationDate', - '$promemoria.expirationDate', - '$promemoria.arrayPromemoria.toString()', - '$promemoria.description', - '$promemoria.priority', - '$promemoria.color' - ) - '''); + 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; @@ -252,23 +235,25 @@ class NoteDatabase { return results.first; } - Future> readNote(int id) async { final db = await database; - final results= await db.query('SELECT * FROM promemoria where id=$id'); + final results = await db.query('SELECT * FROM promemoria where id=$id'); return results.first; } -// Future updatePromemoria(Promemoria promemoria) async { - // final db = await database; + Future updatePromemoria(Promemoria promemoria) async { + final db = await database; - //await db.update('promemoria', promemoria, where: 'id = ?', - // Pass the Dog's id as a whereArg to prevent SQL injection. -// whereArgs: [dog.id],) -//} + 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/note.dart b/lib/model/note.dart index e68d260..7172796 100644 --- a/lib/model/note.dart +++ b/lib/model/note.dart @@ -9,7 +9,63 @@ class Note extends BaseEntity { static String lastModificationDate = BaseEntity.getLastEditDate; static String arrayPromemoria = ''; static String description = ''; + Map toMap() { - return {'id': id, 'title': title, 'desc': description,'CreationDate': creationDate, 'lastM': lastModificationDate }; + return { + 'id': id, + 'title': title, + 'creationDate': creationDate, + 'lastModificationDate': lastModificationDate, + 'arrayPromemoria': arrayPromemoria, + 'description': description + }; + } + + String getId() { + return id; + } + + void setId(String id) { + id = id; + } + + String getTitle() { + return title; + } + + void setTitle(String title) { + title = title; + } + + String getCreationDate() { + return creationDate; + } + + void setCreationDate(String creationDate) { + creationDate = creationDate; + } + + String getLastModificationDate() { + return lastModificationDate; + } + + void setLastModificationDate(String lastModificationDate) { + lastModificationDate = lastModificationDate; + } + + String getArrayPromemoria() { + return arrayPromemoria; + } + + void setArrayPromemoria(String arrayPromemoria) { + arrayPromemoria = arrayPromemoria; + } + + String getDescription() { + return description; + } + + void setDescription(String description) { + description = description; } } diff --git a/lib/model/promemoria.dart b/lib/model/promemoria.dart index 5326c1d..d57a9fc 100644 --- a/lib/model/promemoria.dart +++ b/lib/model/promemoria.dart @@ -13,6 +13,85 @@ class Promemoria extends BaseEntity { static String arrayPromemoria = ''; static String description = ''; static Priority priority = Priority.none; - static Color color = Color.none; + + Map toMap() { + return { + 'id': id, + 'title': title, + 'creationDate': creationDate, + 'lastModificationDate': lastModificationDate, + 'expirationDate': expirationDate, + 'arrayPromemoria': arrayPromemoria, + 'description': description, + 'priority': priority, + 'color': color, + }; + } + + String getId(){ + return id; + } + + void setId(String id) { + id = id; + } + + String getTitle() { + return title; + } + + void setTitle(String title) { + title = title; + } + + String getCreationDate() { + return creationDate; + } + + void setCreationDate(String creationDate) { + creationDate = creationDate; + } + + String getLastModificationDate() { + return lastModificationDate; + } + + void setLastModificationDate(String lastModificationDate) { + lastModificationDate = lastModificationDate; + } + + String getExpirationDate() { + return expirationDate; + } + + void setExpirationDate(String expirationDate) { + expirationDate = expirationDate; + } + + String getArrayPromemoria() { + return arrayPromemoria; + } + + void setArrayPromemoria(String arrayPromemoria) { + arrayPromemoria = arrayPromemoria; + } + + String getDescription() { + return description; + } + + void setDescription(String description) { + description = description; + } + + Priority getPriority() { + return priority; + } + + void setPriority(Priority priority) { + priority = priority; + } + + }