-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarefa.service.php
156 lines (140 loc) · 5 KB
/
tarefa.service.php
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
// Definindo a classe TarefaService para realizar operações CRUD em tarefas
class TarefaService
{
private $conexao; // Propriedade para armazenar a conexão com o banco de dados
private $tarefa; // Propriedade para armazenar uma instância da classe Tarefa
// Construtor para inicializar as propriedades com os objetos Conexao e Tarefa
public function __construct(Conexao $conexao, Tarefa $tarefa)
{
$this->conexao = $conexao->conectar(); // Estabelecendo conexão com o banco de dados
$this->tarefa = $tarefa; // Armazenando a referência da tarefa
}
// Método para inserir uma nova tarefa no banco de dados
public function inserir()
{
// Preparando a consulta SQL para inserção
$query = 'INSERT INTO tb_tarefas(tarefa, categoria, data_prazo) VALUES (:tarefa, :categoria, :data_prazo)';
$stmt = $this->conexao->prepare($query);
// Vinculando os valores da tarefa
$stmt->bindValue(':tarefa', $this->tarefa->__get('tarefa'));
$stmt->bindValue(':categoria', $this->tarefa->__get('categoria'));
$stmt->bindValue(':data_prazo', $this->tarefa->__get('data_prazo'));
$stmt->execute(); // Executando a consulta
}
// Método para filtrar tarefas por status
public function filtrarTarefasPorStatus($status)
{
if ($status == 'todas') {
// Seleciona todas as tarefas se o status for 'todas'
$query = "SELECT t.id, s.status, t.tarefa, t.categoria, t.data_cadastrado, t.data_prazo FROM tb_tarefas as t LEFT JOIN tb_status as s on (t.id_status = s.id)";
} else {
// Filtra tarefas por status específico
$query = "SELECT t.id, s.status, t.tarefa, t.categoria, t.data_cadastrado, t.data_prazo FROM tb_tarefas as t LEFT JOIN tb_status as s on (t.id_status = s.id) WHERE s.status = :status";
}
$stmt = $this->conexao->prepare($query);
if ($status != 'todas') {
$stmt->bindValue(':status', $status); // Vincula o status à consulta, se aplicável
}
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ); // Retorna os resultados como objetos
}
// Método para recuperar tarefas com ordenação específica
public function recuperar($ordenacao = 'mais_recentes')
{
$ordemSql = $ordenacao == 'mais_recentes' ? 'DESC' : 'ASC'; // Define a ordem da consulta
$query = "
SELECT
t.id, t.tarefa, t.categoria, s.status, t.data_cadastrado, t.data_prazo
FROM
tb_tarefas AS t
JOIN
tb_status AS s ON t.id_status = s.id
ORDER BY
t.data_cadastrado $ordemSql
";
$stmt = $this->conexao->prepare($query);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
// Método para atualizar os detalhes de uma tarefa
public function atualizar()
{
$query = "update tb_tarefas set tarefa = ? where id = ?";
$stmt = $this->conexao->prepare($query);
$stmt->bindValue(1, $this->tarefa->__get('tarefa'));
$stmt->bindValue(2, $this->tarefa->__get('id'));
return $stmt->execute(); // Retorna true se a atualização for bem-sucedida
}
// Método para remover uma tarefa pelo ID
public function remover()
{
$query = 'delete from tb_tarefas where id = :id';
$stmt = $this->conexao->prepare($query);
$stmt->bindValue(':id', $this->tarefa->__get('id'));
$stmt->execute(); // Executa a remoção
}
// Método para marcar uma tarefa como realizada
public function marcarRealizada()
{
$query = "update tb_tarefas set id_status = ? where id = ?";
$stmt = $this->conexao->prepare($query);
$stmt->bindValue(1, $this->tarefa->__get('id_status'));
$stmt->bindValue(2, $this->tarefa->__get('id'));
return $stmt->execute(); // Retorna true se a atualização for bem-sucedida
}
// Método para recuperar tarefas pendentes
public function recuperarTarefasPendentes()
{
$query = '
SELECT
t.id, s.status, t.tarefa, t.categoria, t.data_cadastrado
FROM
tb_tarefas as t
LEFT JOIN tb_status as s on (t.id_status = s.id)
WHERE
t.id_status = :id_status
';
$stmt = $this->conexao->prepare($query);
$stmt->bindValue(':id_status', $this->tarefa->__get('id_status'));
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
// Método para filtrar tarefas por categoria
public function filtrarPorCategoria($categoria)
{
$query = "
SELECT
t.id, t.tarefa, t.categoria, s.status, t.data_cadastrado, t.data_prazo
FROM
tb_tarefas AS t
LEFT JOIN
tb_status AS s ON t.id_status = s.id
WHERE
t.categoria LIKE :categoria
ORDER BY
t.data_cadastrado DESC
";
$stmt = $this->conexao->prepare($query);
$stmt->bindValue(':categoria', '%' . $categoria . '%');
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
// Método para recuperar tarefas ordenadas por data de cadastro
public function recuperarOrdenadoPorData()
{
$query = "
SELECT
t.id, t.tarefa, t.categoria, s.status, t.data_cadastrado, t.data_prazo
FROM
tb_tarefas AS t
JOIN
tb_status AS s ON t.id_status = s.id
ORDER BY
t.data_cadastrado DESC
";
$stmt = $this->conexao->prepare($query);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
}