-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListaDAO.js
128 lines (110 loc) · 3.76 KB
/
ListaDAO.js
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
function ListaDAO() {
var listas = {};
this.criarLista = function (nome) {
//gerar id
var novoID = "lista-" + Object.keys(this.listas).length;
//criar objeto da lista
var novaLista = {
id: novoID,
nome: nome,
tarefas: [],
numeroDeTarefas: 0
};
//adicionar nova lista ao objeto listas
this.listas[novoID] = novaLista;
return{
sucess: true,
message: "Lista \'" + nome + "\' criada"
}
};
this.renomearLista = function (listaID, novoNome) {
//verifica se a lista existe
if (this.listas.hasOwnProperty(listaIF)) {
//renomear com novo nome
this.listas[listaID].nome = novoNome;
console.log();
return{
sucess: true,
message: "Lista renomeada para \'" + novoNome + "\'."
}
} else {
return{
sucess: false,
mesage: "Lista não pode ser encontrada."
}
}
};
this.apagarLista = function (listaID) {
//verifica se lista existe
if (this.listas.hasOwnProperty(listaID)){
var nomeDaLista = this.listas[listaID].nome;
//exclui a lista
delete this.listas[lista.ID];
return{
sucess: true,
message: "Lista \'" + nomeDaLista + "\' apagada com sucesso."
}
} else {
return {
sucess: false,
message: "Lista não pode ser encontrada."
}
}
};
this.getListas = function () {
//retornar as listas
return this.listas;
};
this.novaTarefa = function (descricao, listaID) {
//cria id da tarefa
var tarefaID = "tarefa-" + this.listas[listaID].numeroDeTarefas;
//cria objeto da tarefa
var tarefa = {
id: tarefaID,
descricao: descricao,
completa: false
};
//adiciona tarefa na lista de tarefas
this.listas[listaID].tarefas.push(tarefa);
this.listas[listaID].numeroDeTarefas += 1;
return {
sucess: true,
message: "Tarefa adicionada na lista " + this.listas[listaID].nome
}
};
this.toggleTarefa = function (listaID, tarefaID) {
//funcao para alternar tarefa entre completa "true" ou "false"
//percorre lista de tarefas
for (var i = 0; i < this.listas[listaID].tarefas.length; i++) {
//encontra a tarefa
if (this.listas[listaID].tarefas[i].id == tarefaID) {
//mudar valor da propriedade "completa"
this.listas[listaID].tarefas[i].completa = !this.listas[listaID].tarefas[i].completa;
return {
sucess: true,
message: "Tarefa \'" + tarefaID + "\': " + this.listas[listaID].tarefas[i].completa
};
};
return {
sucess: false,
message: "Tarefa \'" + tarefaID + "\' não encontrada."
}
}
};
this.apagarTarefa = function (listaID, tarefaID) {
//percorre as tarefas da lista
for (var i =0; i < this.listas[listaID].tarefas.length; i++) {
//encontra a tarefa
if (this.listas[listaID].tarefas[i].id == tarefaID) {
//apaga a tarefa
this.listas[listaID].tarefas.splice(i,1);
console.log("ListaDAD> Tarefa \'%s\' apagada com sucesso.", tarefaID);
break
}
}
};
this.getTarefas = function (listaID) {
return this.listas[listaID].tarefas;
};
}
module.exports = new ListaDAO();