-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_dao.dart
106 lines (93 loc) · 3.91 KB
/
task_dao.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
import 'package:nosso_primeiro_projeto/components/task.dart';
import 'package:nosso_primeiro_projeto/data/database.dart';
import 'package:sqflite/sqflite.dart';
class TaskDao {
//nome do banco de dados
static const String tableSql = 'CREATE TABLE $_tableName('
'$_name TEXT, '
'$_difficulty INTEGER, ' //aqui eu esqueci uma virgula e ele bugou tudo, e o pior é que pra voltar a funcionar eu tenho de deletar esse banco de dados ou fazer um novo!
'$_image TEXT, '
'$_level INTEGER)';
// nome das variaveis do banco de dados
//separadamente assim fica mais facil de alterar o banco de dados.
static const String _tableName = 'tarefas';
static const String _name = 'nome';
static const String _difficulty = 'dificuldade';
static const String _image = 'imagem';
static const String _level = 'nivel';
save(Task tarefa) async {
//abre o banco de dados no caminho que está no sistema,
// com o nome que definimos
final Database bancoDeDados = await getDatabase();
print('Banco de dados encontrado em: $bancoDeDados');
//procura a tarefa
var itemExists = await find(tarefa.nome);
print('Tarefa encontrada${itemExists}');
//transforma a informação em um mapa
//ruim
// Map<String, dynamic> taskMap(Task tarefa) {
// final Map<String, dynamic> mapaDeTarefas = Map();
// mapaDeTarefas[_name] = tarefa.nome;
// mapaDeTarefas[_difficulty] = tarefa.dificuldade;
// mapaDeTarefas[_image] = tarefa.foto;
// return mapaDeTarefas;
// bom porque estara fora do save, pode ser reutilizado
Map<String, dynamic> taskMap = _toMap(tarefa);
print('Mapa criado: $taskMap');
if (itemExists.isEmpty) {
print('a Tarefa não existia: $taskMap');
// se não existir ( tamanho é zero ), adicione novo nome.
return await bancoDeDados.insert(
_tableName, taskMap); // só aceita mapas com String
} else {
//se existir, altere tudo baseado no nome!
print('a Tarefa existia: $taskMap');
return await bancoDeDados.update(_tableName, taskMap,
where: '$_name = ?', whereArgs: [tarefa.nome]);
}
}
Future<List<Task>> findAll() async {
print('Acessando findAll: ');
final Database bancoDeDados = await getDatabase();
final List<Map<String, dynamic>> result =
await bancoDeDados.query(_tableName);
print('Procurando dados no banco de dados... encontrado :$result');
return _toList(result);
}
Future<List<Task>> find(String nomeDaTarefa) async {
print('Acessando find: ');
final Database bancoDeDados = await getDatabase();
print('Procurando tarefa com o nome: ${nomeDaTarefa}');
final List<Map<String, dynamic>> result = await bancoDeDados
.query(_tableName, where: '$_name = ?', whereArgs: [nomeDaTarefa]);
print('Tarefa encontrada: ${_toList(result)}');
return _toList(result);
}
delete(String nomeDaTarefa) async {
print('Deletando tarefa: $nomeDaTarefa');
final Database bancoDeDados = await getDatabase();
return await bancoDeDados
.delete(_tableName, where: '$_name = ?', whereArgs: [nomeDaTarefa]);
}
List<Task> _toList(List<Map<String, dynamic>> listaDeTarefa) {
print('Convertendo to List: ');
final List<Task> tarefas = [];
for (Map<String, dynamic> linha in listaDeTarefa) {
final Task tarefa = Task(linha[_name], linha[_image], linha[_difficulty],
linha[_level]); // tudo porque o DB não aceita o tipo int.
tarefas.add(tarefa);
}
print('Lista de Tarefas: $tarefas');
return tarefas;
}
Map<String, dynamic> _toMap(Task tarefa) {
print('Convertendo to Map: ');
final Map<String, dynamic> mapaDeTarefas = Map();
mapaDeTarefas[_name] = tarefa.nome;
mapaDeTarefas[_difficulty] = tarefa.dificuldade;
mapaDeTarefas[_image] = tarefa.foto;
mapaDeTarefas[_level] = tarefa.nivel;
print('Mapa de Tarefas: $mapaDeTarefas');
return mapaDeTarefas;
}
}