-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientes.js
More file actions
83 lines (78 loc) · 3.31 KB
/
clientes.js
File metadata and controls
83 lines (78 loc) · 3.31 KB
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
const express = require('express');
const router = express.Router();
const pool = require('../db');
router.get('/', async (req, res) => {
try {
const { filtro } = req.query;
const query = `
SELECT c.*, COALESCE(json_agg(v) FILTER (WHERE v.id IS NOT NULL), '[]') AS veiculos
FROM Clientes c
LEFT JOIN Veiculos v ON c.id = v.cliente_id
WHERE ($1::text IS NULL OR c.nome ILIKE $1)
GROUP BY c.id
`;
const values = filtro ? [`%${filtro}%`] : [null];
const result = await pool.query(query, values);
res.status(200).json(result.rows);
} catch (error) {
console.error('Erro na query:', error);
res.status(500).json({ error: 'Erro ao buscar clientes' });
}
});
router.post('/', async (req, res) => {
const { nome, telefone, email, rg, cpf, ano_nascimento, endereco } = req.body;
try {
const query = 'INSERT INTO Clientes (nome, telefone, email, rg, cpf, ano_nascimento, endereco) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *';
const values = [nome, telefone, email, rg, cpf, ano_nascimento, endereco];
const result = await pool.query(query, values);
const cliente = result.rows[0];
const queryRelacionada = `
SELECT c.*, COALESCE(json_agg(v) FILTER (WHERE v.id IS NOT NULL), '[]') AS veiculos
FROM Clientes c
LEFT JOIN Veiculos v ON c.id = v.cliente_id
WHERE c.id = $1
GROUP BY c.id
`;
const clienteCompleto = await pool.query(queryRelacionada, [cliente.id]);
res.status(201).json(clienteCompleto.rows[0]);
} catch (error) {
console.error('Erro na query:', error);
res.status(500).json({ error: 'Erro ao criar cliente' });
}
});
router.put('/:id', async (req, res) => {
const { id } = req.params;
const { nome, telefone, email, rg, cpf, ano_nascimento, endereco } = req.body;
try {
const query = 'UPDATE Clientes SET nome = $1, telefone = $2, email = $3, rg = $4, cpf = $5, ano_nascimento = $6, endereco = $7 WHERE id = $8 RETURNING *';
const values = [nome, telefone, email, rg, cpf, ano_nascimento, endereco, id];
const result = await pool.query(query, values);
if (result.rows.length === 0) return res.status(404).json({ error: 'Cliente não encontrado' });
const cliente = result.rows[0];
const queryRelacionada = `
SELECT c.*, COALESCE(json_agg(v) FILTER (WHERE v.id IS NOT NULL), '[]') AS veiculos
FROM Clientes c
LEFT JOIN Veiculos v ON c.id = v.cliente_id
WHERE c.id = $1
GROUP BY c.id
`;
const clienteCompleto = await pool.query(queryRelacionada, [cliente.id]);
res.status(200).json(clienteCompleto.rows[0]);
} catch (error) {
console.error('Erro na query:', error);
res.status(500).json({ error: 'Erro ao atualizar cliente' });
}
});
router.delete('/:id', async (req, res) => {
const { id } = req.params;
try {
const query = 'DELETE FROM Clientes WHERE id = $1 RETURNING *';
const result = await pool.query(query, [id]);
if (result.rows.length === 0) return res.status(404).json({ error: 'Cliente não encontrado' });
res.status(200).json({ message: 'Cliente deletado com sucesso' });
} catch (error) {
console.error('Erro na query:', error);
res.status(500).json({ error: 'Erro ao deletar cliente' });
}
});
module.exports = router;