-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from unb-mds/developer
[PR] Implementação do Protótipo de API, Backend, Frontend e Responsividade Web
- Loading branch information
Showing
45 changed files
with
1,867 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
node_modules | ||
venv | ||
venv | ||
db-scripts/node_modules | ||
backend/novas_noticias.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const express = require('express'); | ||
const pool = require('./db'); | ||
const app = express(); | ||
|
||
app.get('/licitacoes', async (req, res) => { | ||
const { inicio, fim } = req.query; | ||
|
||
if (!inicio || !fim) { | ||
return res.status(400).json({ error: "Data inicial e data final são obrigatórias" }); | ||
} | ||
|
||
const inicioComparacao = parseInt(inicio); | ||
const fimComparacao = parseInt(fim); | ||
|
||
try { | ||
const result = await pool.query(` | ||
SELECT * FROM licitacoes WHERE | ||
(CAST(ano AS INTEGER) * 100 + CAST(mes AS INTEGER)) BETWEEN $1 AND $2 | ||
`, [inicioComparacao, fimComparacao]); | ||
|
||
if (result.rows.length > 0) { | ||
res.json(result.rows); | ||
} else { | ||
res.status(404).json({ message: "Nenhum dado encontrado para o intervalo de datas informado" }); | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
res.status(500).json({ error: err.message }); | ||
} | ||
}); | ||
|
||
const PORT = process.env.PORT || 5000; | ||
app.listen(PORT, () => { | ||
console.log(`Servidor rodando na porta ${PORT}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const { Pool } = require('pg'); | ||
|
||
const pool = new Pool ({ | ||
host: 'localhost', | ||
port: 5432, | ||
user: 'docker', | ||
password: 'docker', | ||
database: 'test_db', | ||
}); | ||
|
||
module.exports = pool; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const { Client } = require('pg'); | ||
|
||
const client = new Client({ | ||
host: 'localhost', | ||
port: 5432, | ||
user: 'docker', | ||
password: 'docker', | ||
database: 'test_db', | ||
}); | ||
|
||
(async () => { | ||
try { | ||
await client.connect(); | ||
console.log('Conectando ao Pg'); | ||
|
||
const query = ` | ||
CREATE TABLE licitacoes ( | ||
id SERIAL, | ||
sigla VARCHAR(255), | ||
unidade_adm VARCHAR(255), | ||
valor_empenhado NUMERIC, | ||
valor_liquidado NUMERIC, | ||
valor_pago NUMERIC, | ||
ano INTEGER, | ||
mes VARCHAR(255), | ||
PRIMARY KEY (unidade_adm, mes, ano) | ||
); | ||
`; | ||
await client.query(query); | ||
console.log('Database criado com sucesso!') | ||
} catch (err) { | ||
console.error('Erro ao criar database: ', err); | ||
} finally { | ||
await client.end(); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const { Client } = require('pg'); | ||
const fs = require('fs'); | ||
|
||
// Config de conexão do PostgreSQL | ||
const client = new Client({ | ||
host: 'localhost', | ||
port: 5432, | ||
user: 'docker', | ||
password: 'docker', | ||
database: 'test_db', | ||
}); | ||
|
||
function formatValue(value) { | ||
if (typeof value !== 'string') { | ||
value = value.toString(); | ||
} | ||
// Remove os pontos | ||
let formattedValue = value.replace(/\./g, ''); | ||
// Substitui a vírgula por ponto | ||
formattedValue = formattedValue.replace(',', '.'); | ||
return formattedValue; | ||
} | ||
|
||
(async () => { | ||
try { | ||
await client.connect(); | ||
console.log('Conectando ao Postgre'); | ||
|
||
// Lendo o JSON | ||
const data = JSON.parse(fs.readFileSync('../webscrapy/minas_de_cultura_scrapy/resultado.json', 'utf-8')); | ||
|
||
// Loop de inserção no banco de dados | ||
for (const item of data) { | ||
|
||
const { | ||
"Sigla": sigla, | ||
"Unidade administrativa": unidade_adm, | ||
"Valor empenhado": valor_empenhado, | ||
"Valor liquidado": valor_liquidado, | ||
"Valor pago": valor_pago, | ||
"ano": ano, | ||
"mes": mes | ||
} = item; | ||
|
||
const query = ` | ||
INSERT INTO licitacoes (sigla, unidade_adm, valor_empenhado, valor_liquidado, valor_pago, ano, mes) | ||
VALUES ($1, $2, $3, $4, $5, $6, $7) | ||
ON CONFLICT (unidade_adm, mes, ano) DO NOTHING; | ||
`; | ||
const values = [ | ||
sigla ? sigla.toString() : null, | ||
unidade_adm ? unidade_adm.toString().substring(0, 255) : null, | ||
valor_empenhado ? parseFloat(formatValue(valor_empenhado)) : null, | ||
valor_liquidado ? parseFloat(formatValue(valor_liquidado)) : null, | ||
valor_pago ? parseFloat(formatValue(valor_pago)) : null, | ||
ano ? parseInt(ano) : null, | ||
mes ? mes.toString().substring(0, 255) : null | ||
]; | ||
|
||
await client.query(query, values); | ||
} | ||
|
||
console.log('Dados inseridos com sucesso'); | ||
} catch (err) { | ||
console.error('Erro ao inserir dados:', err); | ||
} finally { | ||
await client.end(); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Use a imagem oficial do Python 3.10 | ||
FROM python:3.10 | ||
|
||
# Defina o diretório de trabalho | ||
WORKDIR /backend | ||
|
||
# Copie o arquivo de requisitos | ||
COPY requirements.txt . | ||
|
||
# Instale as dependências | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
|
||
# Copie o restante do código da aplicação | ||
COPY . . | ||
|
Oops, something went wrong.