diff --git a/postman/Web3API.postman_collection.json b/postman/Web3API.postman_collection.json new file mode 100644 index 0000000..b3102b3 --- /dev/null +++ b/postman/Web3API.postman_collection.json @@ -0,0 +1,636 @@ +{ + "info": { + "_postman_id": "ce5a44b5-d64d-40b4-bfe4-303e12d9622f", + "name": "Web3API", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "26855955" + }, + "item": [ + { + "name": "User", + "item": [ + { + "name": "Register", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"username\": \"mateus\",\n \"password\": \"teste\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{url}}/api/v1/user/new", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "user", + "new" + ] + } + }, + "response": [] + }, + { + "name": "Auth", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"username\": \"mateus\",\n \"password\": \"teste\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{url}}/api/v1/user/authenticate", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "user", + "authenticate" + ] + } + }, + "response": [] + }, + { + "name": "GetInfo", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{url}}/api/v1/user/me", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "user", + "me" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "Vaga", + "item": [ + { + "name": "Criar nova vaga", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{url}}/api/v1/vaga/new", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "vaga", + "new" + ] + } + }, + "response": [] + }, + { + "name": "Deletar vaga por id", + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "{{url}}/api/v1/vaga/25", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "vaga", + "25" + ] + } + }, + "response": [] + }, + { + "name": "Listar Vagas", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{url}}/api/v1/vaga/list", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "vaga", + "list" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "Tipo", + "item": [ + { + "name": "Novo Tipo", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"preco\": \"12.89\",\n \"descr\": \"Hora carro\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{url}}/api/v1/tipo/new", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "tipo", + "new" + ] + } + }, + "response": [] + }, + { + "name": "Update Tipo", + "request": { + "method": "PUT", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"id\": \"5\",\n \"preco\": \"14.90\",\n \"descr\": \"Via API\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{url}}/api/v1/tipo/update", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "tipo", + "update" + ] + } + }, + "response": [] + }, + { + "name": "Delete Tipo", + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "{{url}}/api/v1/tipo/5", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "tipo", + "5" + ] + } + }, + "response": [] + }, + { + "name": "Listar Tipos", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{url}}/api/v1/tipo/list", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "tipo", + "list" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "Carro", + "item": [ + { + "name": "Inserir Carro", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"placa\": \"HRO7J76\",\n \"cliente_id\": 2\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{url}}/api/v1/car/new", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "car", + "new" + ] + } + }, + "response": [] + }, + { + "name": "Atualizar Carro", + "request": { + "method": "PUT", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"id\": \"5\",\n \"placa\": \"HRO7J76\",\n \"cliente_id\": 1\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{url}}/api/v1/car/update", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "car", + "update" + ] + } + }, + "response": [] + }, + { + "name": "Deletar Carro", + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "{{url}}/api/v1/car/4", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "car", + "4" + ] + } + }, + "response": [] + }, + { + "name": "Listar Carros", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{url}}/api/v1/car/list", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "car", + "list" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "Client", + "item": [ + { + "name": "Novo Client", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"nome\":\"Junior Ben10 mil\",\n \"cpf\":\"09889076545\",\n \"telefone\": \"999999999\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{url}}/api/v1/client/new", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "client", + "new" + ] + } + }, + "response": [] + }, + { + "name": "Update Client", + "request": { + "method": "PUT", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"id\": \"6\",\n \"nome\":\"Junior Ben10 mil milhoes\",\n \"cpf\":\"09889076545\",\n \"telefone\": \"999999999\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{url}}/api/v1/client/update", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "client", + "update" + ] + } + }, + "response": [] + }, + { + "name": "Delete Client", + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "{{url}}/api/v1/client/6", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "client", + "6" + ] + } + }, + "response": [] + }, + { + "name": "Listar Clientes", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{url}}/api/v1/client/list", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "client", + "list" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "Ticket", + "item": [ + { + "name": "Inserir Ticket", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"carro\": \"1\",\n \"vaga\": \"2\",\n \"tipo\": \"1\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{url}}/api/v1/ticket/new", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "ticket", + "new" + ] + } + }, + "response": [] + }, + { + "name": "Fechar Ticket", + "request": { + "method": "PUT", + "header": [], + "url": { + "raw": "{{url}}/api/v1/ticket/14", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "ticket", + "14" + ] + } + }, + "response": [] + }, + { + "name": "Listar tickets ativos", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{url}}/api/v1/ticket/listAtivo", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "ticket", + "listAtivo" + ] + } + }, + "response": [] + }, + { + "name": "Listar tickets", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{url}}/api/v1/ticket/listAll", + "host": [ + "{{url}}" + ], + "path": [ + "api", + "v1", + "ticket", + "listAll" + ] + } + }, + "response": [] + } + ] + } + ], + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{token}}", + "type": "string" + } + ] + }, + "event": [ + { + "listen": "prerequest", + "script": { + "type": "text/javascript", + "exec": [ + "let body = `{\"username\": \"${pm.variables.get('username')}\",", + " \"password\": \"${pm.variables.get('password')}\"}`", + " console.log(body);", + "", + " const sendrequest = {", + " url: pm.variables.get(\"url\")+ \"/api/v1/user/authenticate\",", + " method: \"POST\",", + " header: {", + " 'Content-Type': 'application/json',", + " 'Accept': '*/*'", + " ", + " },", + " body: body", + " }", + " pm.sendRequest(sendrequest, function (err, response) {", + " ", + " try{", + " pm.expect(response.json().token).to.not.empty;", + " pm.variables.set('token', response.json().token);", + " } ", + " catch(e){", + " console.log(e);", + " }", + " })" + ] + } + }, + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": [ + "" + ] + } + } + ], + "variable": [ + { + "key": "url", + "value": "localhost:3000", + "type": "default" + }, + { + "key": "username", + "value": "mateus", + "type": "default" + }, + { + "key": "password", + "value": "teste", + "type": "default" + }, + { + "key": "token", + "value": "1", + "type": "default" + } + ] +} \ No newline at end of file diff --git a/src/app.js b/src/app.js index e6d6c66..8249484 100644 --- a/src/app.js +++ b/src/app.js @@ -14,9 +14,11 @@ app.use(appBase, router); pool.connect((err) => { if (err) { console.error('Error connecting to database', err); - process.exit(1); - } + console.log('Desconectado da database'); + // process.exit(1); + } else { console.log('Connected to database'); + } }); module.exports = app; \ No newline at end of file diff --git a/src/controllers/cars.js b/src/controllers/cars.js index b182019..ce8b6f3 100644 --- a/src/controllers/cars.js +++ b/src/controllers/cars.js @@ -10,6 +10,15 @@ const createCar = async (req, res, next) => { } }; +const listCar = async (req, res) => { + try { + const cars = await Car.listCar(); + res.send(cars); + } catch (err) { + res.status(500).json({ error: err.message }); + } +}; + const updateCar = async (req, res) => { try { const car = req.body; @@ -32,4 +41,4 @@ const deleteCar = async (req, res) => { } }; -module.exports = { createCar, deleteCar, updateCar }; \ No newline at end of file +module.exports = { createCar, deleteCar, updateCar, listCar }; \ No newline at end of file diff --git a/src/controllers/client.js b/src/controllers/client.js index ae4919f..3b04a92 100644 --- a/src/controllers/client.js +++ b/src/controllers/client.js @@ -10,6 +10,17 @@ const createClient = async (req, res, next) => { } }; + +const listarCliente = async (req, res) => { + try{ + const clientes = await Client.listarCliente(); + res.send(clientes); + } catch (error) { + console.log(error); + res.status(500).json({error: err.message}); + } +}; + const updateClient = async (req, res) => { try { const client = req.body; @@ -34,4 +45,4 @@ const updateClient = async (req, res) => { -module.exports = { createClient, updateClient, deleteClient }; \ No newline at end of file +module.exports = { createClient, updateClient, deleteClient, listarCliente }; \ No newline at end of file diff --git a/src/controllers/ticket.js b/src/controllers/ticket.js index c1a8c32..a8084b9 100644 --- a/src/controllers/ticket.js +++ b/src/controllers/ticket.js @@ -10,6 +10,26 @@ const createTicket = async (req, res, next) => { } }; +const listarTickets = async (req, res) =>{ + try{ + const tickets = await Ticket.listarTickets(); + res.send(tickets); + } catch (error) { + console.error(error); + res.status(500).json({ error: 'Erro ao listar Ticket' }); + } +}; + +const listarTicketsAll = async (req, res) =>{ + try{ + const tickets = await Ticket.listarTicketsAll(); + res.send(tickets); + } catch (error) { + console.error(error); + res.status(500).json({ error: 'Erro ao listar Ticket' }); + } +}; + const encerrarTicket = async (req, res) => { try { const { id } = req.params; @@ -22,4 +42,4 @@ const encerrarTicket = async (req, res) => { }; -module.exports = { createTicket, encerrarTicket }; \ No newline at end of file +module.exports = { createTicket, encerrarTicket, listarTickets,listarTicketsAll }; \ No newline at end of file diff --git a/src/controllers/tipo.js b/src/controllers/tipo.js index 8c4e086..d494345 100644 --- a/src/controllers/tipo.js +++ b/src/controllers/tipo.js @@ -21,6 +21,16 @@ const updateTipo = async (req, res) => { } }; +const listarTipos = async (req, res) => { + try { + const tipos = await Tipo.listarTipos(); + res.send(tipos); + } catch (error){ + console.log(error); + res.status(500).json({ error: 'Erro ao listar tipo' }); + } +}; + const deleteTipo = async (req, res) => { try { const { id } = req.params; @@ -32,4 +42,4 @@ const deleteTipo = async (req, res) => { } }; -module.exports = { createTipo, deleteTipo, updateTipo }; \ No newline at end of file +module.exports = { createTipo, deleteTipo, updateTipo, listarTipos }; \ No newline at end of file diff --git a/src/controllers/vaga.js b/src/controllers/vaga.js index 17acbba..ce8e2ec 100644 --- a/src/controllers/vaga.js +++ b/src/controllers/vaga.js @@ -10,6 +10,15 @@ const createVaga = async (req, res, next) => { } }; +const listarVagas = async (req,res) =>{ + try{ + const vagas = await Vaga.listarVagas(); + res.send(vagas); + } catch (error) { + console.log(error); + } +}; + const deleteVaga = async (req, res) => { try { const { id } = req.params; @@ -22,4 +31,4 @@ const deleteVaga = async (req, res) => { } }; -module.exports = { createVaga, deleteVaga }; \ No newline at end of file +module.exports = { createVaga, deleteVaga, listarVagas }; \ No newline at end of file diff --git a/src/models/cars.js b/src/models/cars.js index 56239ef..7a54479 100644 --- a/src/models/cars.js +++ b/src/models/cars.js @@ -10,6 +10,18 @@ const { placa, cliente_id } = car; return result.rows[0]; }; +const listCar = async () => { + + const query = { + text: `select car.id id, car.placa placa, car.cliente_id cliente_id, cli.nome as cliente_nome + from carro car + full join cliente cli + on cli.id = car.cliente_id`, + + }; + const result = await pool.query(query); + return result.rows; + }; const updateCar = async (car) => { const { placa, cliente_id, id } = car; @@ -31,5 +43,5 @@ const deleteCar = async (id) => { } module.exports = { - insertCar, deleteCar, updateCar + insertCar, deleteCar, updateCar, listCar }; \ No newline at end of file diff --git a/src/models/client.js b/src/models/client.js index c069709..d22869a 100644 --- a/src/models/client.js +++ b/src/models/client.js @@ -10,6 +10,11 @@ const { nome, cpf, telefone } = client; return result.rows[0]; }; +const listarCliente = async () =>{ + const query = {text: 'select * from cliente'}; + const result = await pool.query(query); + return result.rows; +} const updateClient = async (client) => { const { nome, cpf, telefone, id } = client; @@ -31,5 +36,5 @@ const deleteClient = async (id) => { } module.exports = { - insertClient, deleteClient, updateClient + insertClient, deleteClient, updateClient, listarCliente }; \ No newline at end of file diff --git a/src/models/ticket.js b/src/models/ticket.js index ffc4030..8f27939 100644 --- a/src/models/ticket.js +++ b/src/models/ticket.js @@ -10,6 +10,39 @@ const insertTicket = async (req, res) => { return result.rows[0]; }; +const listarTickets = async (req, res) => { + const query = { + text: `SELECT to_char(ticket.hora_entrada at time zone 'UTF-4', 'HH24:MI:SS') as hora_entrada, + cliente.nome as nome_cliente, carro.placa as nome_placa, tipo.descr as tipo_vaga, + tipo.preco as preco_tipo, ticket.vaga_id as id_vaga, ticket.id as ticket_id, + coalesce(to_char(ticket.hora_saida at time zone 'UTF-4', 'HH24:MI:SS'), + 'Não recuperado') as hora_saida, coalesce(total_pago, 0.0) as custo + FROM ticket + join tipo on tipo.id = ticket.tipo_id + join carro on carro.id = ticket.carro_id + join cliente on cliente.id = carro.cliente_id + where ticket.estado = true` + } + const result = await pool.query(query); + return result.rows; +} + +const listarTicketsAll = async (req, res) => { + const query = { + text: `SELECT to_char(ticket.hora_entrada at time zone 'UTF-4', 'HH24:MI:SS') as hora_entrada, + cliente.nome as nome_cliente, carro.placa as nome_placa, tipo.descr as tipo_vaga, + tipo.preco as preco_tipo, ticket.vaga_id as id_vaga, ticket.id as ticket_id, + coalesce(to_char(ticket.hora_saida at time zone 'UTF-4', 'HH24:MI:SS'), + 'Não recuperado') as hora_saida, coalesce(total_pago, 0.0) as custo + FROM ticket + join tipo on tipo.id = ticket.tipo_id + join carro on carro.id = ticket.carro_id + join cliente on cliente.id = carro.cliente_id` + } + const result = await pool.query(query); + return result.rows; +} + const encerraTicket = async (id) => { const query = { text: 'select encerrar_ticket($1)', @@ -20,5 +53,5 @@ const encerraTicket = async (id) => { } module.exports = { - insertTicket, encerraTicket + insertTicket, encerraTicket, listarTickets, listarTicketsAll }; \ No newline at end of file diff --git a/src/models/tipo.js b/src/models/tipo.js index a252fd7..27aa59c 100644 --- a/src/models/tipo.js +++ b/src/models/tipo.js @@ -10,6 +10,12 @@ const { preco, descr } = tipo; return result.rows[0]; }; +const listarTipos = async () => { + const query = {text: "select * from tipo"}; + const result = await pool.query(query); + return result.rows; +}; + const updateTipo = async (tipo) => { console.log(tipo); @@ -32,5 +38,5 @@ const deleteTipo = async (id) => { } module.exports = { - insertTipo, deleteTipo, updateTipo + insertTipo, deleteTipo, updateTipo, listarTipos }; \ No newline at end of file diff --git a/src/models/vaga.js b/src/models/vaga.js index 406b1c6..48e3273 100644 --- a/src/models/vaga.js +++ b/src/models/vaga.js @@ -9,6 +9,16 @@ const insertVaga = async () => { return result.rows[0]; }; +const listarVagas = async () =>{ + const query ={ + text: 'select * from vaga' + }; + const result = await pool.query(query); + return result.rows; + + +}; + const deleteVaga = async (id) => { const query = { text: 'delete from vaga where id=$1 RETURNING *', @@ -19,5 +29,5 @@ const deleteVaga = async (id) => { } module.exports = { - insertVaga, deleteVaga + insertVaga, deleteVaga, listarVagas }; \ No newline at end of file diff --git a/src/routes/cars.js b/src/routes/cars.js index df92aae..f705eb5 100644 --- a/src/routes/cars.js +++ b/src/routes/cars.js @@ -15,4 +15,8 @@ router.put('/update', verifyToken, carController.updateCar, (req, res) => { res.status(200).json({message: 'car atualizado com sucesso'}) }); +router.get('/list', verifyToken, carController.listCar, (req, res) => { + +}); + module.exports = router; \ No newline at end of file diff --git a/src/routes/client.js b/src/routes/client.js index 0be51af..0f3f3c8 100644 --- a/src/routes/client.js +++ b/src/routes/client.js @@ -15,4 +15,6 @@ router.put('/update', verifyToken, clientController.updateClient, (req, res) => res.status(200).json({message: 'Client atualizado com sucesso'}) }); +router.get('/list', verifyToken, clientController.listarCliente, (req, res)=>{}); + module.exports = router; \ No newline at end of file diff --git a/src/routes/ticket.js b/src/routes/ticket.js index eb18ea7..86c3a75 100644 --- a/src/routes/ticket.js +++ b/src/routes/ticket.js @@ -11,5 +11,8 @@ router.put('/:id', verifyToken, ticketController.encerrarTicket, (req, res) => { res.status(200).json({ message: 'Ticket encerrado com sucesso' }); }); +router.get('/listAtivo', verifyToken, ticketController.listarTickets, (req, res)=>{}); +router.get('/listAll', verifyToken, ticketController.listarTicketsAll, (req, res)=>{}); + module.exports = router; \ No newline at end of file diff --git a/src/routes/tipo.js b/src/routes/tipo.js index e84aaf1..118b224 100644 --- a/src/routes/tipo.js +++ b/src/routes/tipo.js @@ -15,4 +15,8 @@ router.put('/update', verifyToken, tipoController.updateTipo, (req, res) => { res.status(200).json({message: 'Tipo atualizado com sucesso'}) }); +router.get('/list', verifyToken, tipoController.listarTipos, (req, res) => { + +}); + module.exports = router; \ No newline at end of file diff --git a/src/routes/vaga.js b/src/routes/vaga.js index 856b996..032af76 100644 --- a/src/routes/vaga.js +++ b/src/routes/vaga.js @@ -11,4 +11,6 @@ router.delete('/:id', verifyToken, vagaController.deleteVaga, (req, res) => { res.status(200).json({ message: 'Vaga excluída com sucesso' }); }); +router.get('/list', verifyToken, vagaController.listarVagas, (req, res) => {}) + module.exports = router; \ No newline at end of file