work in progress database
This commit is contained in:
81
lib/database/controller.dart
Normal file
81
lib/database/controller.dart
Normal file
@@ -0,0 +1,81 @@
|
||||
import '../model/note.dart';
|
||||
import '../model/promemoria.dart';
|
||||
import 'database.dart';
|
||||
|
||||
class Controller {
|
||||
NoteDatabase database = NoteDatabase.instance;
|
||||
|
||||
Future<List<Note>> getAllNote() async {
|
||||
final db = await database.database;
|
||||
var notes = await db.query(noteTable);
|
||||
List<Note> noteList = notes.map((e) => Note.fromJson(e)).toList();
|
||||
return noteList;
|
||||
}
|
||||
|
||||
Future<List<Promemoria>> getAllPromemoria() async {
|
||||
final db = await database.database;
|
||||
var promemorias = await db.query(promemoriaTable);
|
||||
List<Promemoria> promemoriaList =
|
||||
promemorias.map((e) => Promemoria.fromJson(e)).toList();
|
||||
return promemoriaList;
|
||||
}
|
||||
|
||||
Future<Note> 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<Promemoria> 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}'
|
||||
)
|
||||
''');
|
||||
}
|
||||
}
|
||||
@@ -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<Database> 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<void> deleteDatabase() async {
|
||||
final databasePath = await getDatabasesPath();
|
||||
final path = join(databasePath, 'note.db');
|
||||
databaseFactory.deleteDatabase(path);
|
||||
}
|
||||
|
||||
Future<bool> 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<List<Map>> selectAllPromemoria() async {
|
||||
final db = await database;
|
||||
|
||||
@@ -199,61 +142,6 @@ class NoteDatabase {
|
||||
return maps;
|
||||
}
|
||||
|
||||
Future<List<Map>> selectAllNotes() async {
|
||||
final db = await database;
|
||||
|
||||
final List<Map<String, dynamic>> maps = await db.query(noteTable);
|
||||
|
||||
return maps;
|
||||
}
|
||||
|
||||
Future<void> createNote(Database database, Note note) async {
|
||||
await database.insert(
|
||||
'note',
|
||||
note.toMap(),
|
||||
conflictAlgorithm: ConflictAlgorithm.replace,
|
||||
);
|
||||
|
||||
print('note $note.title inserted');
|
||||
}
|
||||
|
||||
Future<void> createPromemoria(
|
||||
Database database, Promemoria promemoria) async {
|
||||
await database.insert(
|
||||
'promemoria',
|
||||
promemoria.toMap(),
|
||||
conflictAlgorithm: ConflictAlgorithm.replace,
|
||||
);
|
||||
|
||||
print('promemoria $promemoria.title inserted');
|
||||
}
|
||||
|
||||
Future<Map<String, Object?>> readPromemoria(int id) async {
|
||||
final db = await database;
|
||||
|
||||
final results = await db.query('SELECT * FROM note where id=$id');
|
||||
return results.first;
|
||||
}
|
||||
|
||||
Future<Map<String, Object?>> readNote(int id) async {
|
||||
final db = await database;
|
||||
|
||||
final results = await db.query('SELECT * FROM promemoria where id=$id');
|
||||
|
||||
return results.first;
|
||||
}
|
||||
|
||||
Future<void> updatePromemoria(Promemoria promemoria) async {
|
||||
final db = await database;
|
||||
|
||||
await db.update('promemoria', promemoria.toMap(),
|
||||
where: 'id = ?', whereArgs: [promemoria.getId()]);
|
||||
}
|
||||
|
||||
Future<void> updateNote(Note note) async {
|
||||
final db = await database;
|
||||
|
||||
await db.update('note', note.toMap(),
|
||||
where: 'id = ?', whereArgs: [note.getId()]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<String, dynamic> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Test> {
|
||||
NoteDatabase noteDatabase = NoteDatabase.instance;
|
||||
Controller controller = Controller();
|
||||
|
||||
Future<void> _pressed() async {
|
||||
print("Inserting demo data");
|
||||
@@ -22,9 +24,20 @@ class _TestState extends State<Test> {
|
||||
}
|
||||
|
||||
Future<void> _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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user