-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_services.dart
136 lines (126 loc) · 3.81 KB
/
database_services.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import '../model/tasks.dart';
class DatabaseServices {
static Database? _db;
static final DatabaseServices instance = DatabaseServices._constructor();
final String _taskTableName = "tasks";
final String _taskIdColumnName = "id";
final String _taskContentColumnName = "content";
final String _taskTitleColumnName = "title";
final String _taskIsCompletedColumnName = "isCompleted";
final String _taskTimeColumName = "time";
final String _taskDateColumnName = "date";
final String _taskRepeatColumnName = "repeat";
final String _taskCategoryColumnName = "category";
DatabaseServices._constructor();
Future<Database> get database async {
if (_db != null) return _db!;
_db = await getDatabase();
return _db!;
}
Future<Database> getDatabase() async {
final databaseDirPath = await getDatabasesPath();
final databasePath = join(databaseDirPath, "master_db.db");
final database = await openDatabase(
databasePath,
version: 1,
onCreate: (db, version) {
db.execute('''
CREATE TABLE $_taskTableName(
$_taskIdColumnName INTEGER PRIMARY KEY AUTOINCREMENT ,
$_taskTitleColumnName STRING NOT NULL,
$_taskContentColumnName STRING NOT NULL,
$_taskDateColumnName STRING NOT NULL,
$_taskTimeColumName STRING NOT NULL,
$_taskRepeatColumnName STRING NOT NULL,
$_taskCategoryColumnName STRING NOT NULL,
$_taskIsCompletedColumnName INTEGER NOT NULL
)
''');
},
);
return database;
}
void addTask(
String title,
String content,
String date,
String time,
String repeat,
String category,
) async {
final db = await database;
await db.insert(
_taskTableName,
{
_taskTitleColumnName: title,
_taskContentColumnName: content,
_taskDateColumnName: date,
_taskTimeColumName: time,
_taskRepeatColumnName: repeat,
_taskCategoryColumnName: category,
_taskIsCompletedColumnName: 0,
},
);
}
Future<List<Task>> getTasks() async {
final db = await database;
final data = await db.query(_taskTableName);
List<Task> task = data
.map((e) => Task(
id: e["id"] as int,
title: e["title"] as String,
content: e["content"] as String,
date: e["date"] as String,
time: e["time"] as String,
repeat: e["repeat"] as String,
category: e["category"] as String,
isCompleted: e["isCompleted"] as int,
))
.toList();
return task;
}
Future<List<Task>> getTasksByCategory(String category) async {
final db = await database;
final data = await db.query(_taskTableName,
where: 'category=?',
whereArgs: [category],
orderBy: _taskIsCompletedColumnName);
List<Task> task = data
.map((e) => Task(
id: e["id"] as int,
title: e["title"] as String,
content: e["content"] as String,
date: e["date"] as String,
time: e["time"] as String,
repeat: e["repeat"] as String,
category: e["category"] as String,
isCompleted: e["isCompleted"] as int,
))
.toList();
return task;
}
void updateComplete(int id, int isComplete) async {
final db = await database;
await db.update(
_taskTableName,
{
_taskIsCompletedColumnName: isComplete,
},
where: 'id = ?',
whereArgs: [
id,
]);
}
void deleteTask(int id) async {
final db = await database;
await db.delete(
_taskTableName,
where: 'id = ?',
whereArgs: [
id,
],
);
}
}