Skip to content

Commit

Permalink
Merge pull request #13 from unb-mds/test
Browse files Browse the repository at this point in the history
filtragem sql
  • Loading branch information
gabriel-lima258 authored Nov 14, 2024
2 parents 7f7b822 + e5a7fcc commit 908a245
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Projeto" ALTER COLUMN "especie" DROP NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- AlterTable
ALTER TABLE "Executor" ALTER COLUMN "codigo" SET DATA TYPE TEXT;

-- AlterTable
ALTER TABLE "Repassador" ALTER COLUMN "codigo" SET DATA TYPE TEXT;

-- AlterTable
ALTER TABLE "Tomador" ALTER COLUMN "codigo" SET DATA TYPE TEXT;

-- AddForeignKey
ALTER TABLE "Tipo" ADD CONSTRAINT "Tipo_idEixo_fkey" FOREIGN KEY ("idEixo") REFERENCES "Eixo"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
19 changes: 19 additions & 0 deletions backend/prisma/migrations/20241114233015_int_codigo/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Warnings:
- Changed the type of `codigo` on the `Executor` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `codigo` on the `Repassador` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `codigo` on the `Tomador` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
*/
-- AlterTable
ALTER TABLE "Executor" DROP COLUMN "codigo",
ADD COLUMN "codigo" INTEGER NOT NULL;

-- AlterTable
ALTER TABLE "Repassador" DROP COLUMN "codigo",
ADD COLUMN "codigo" INTEGER NOT NULL;

-- AlterTable
ALTER TABLE "Tomador" DROP COLUMN "codigo",
ADD COLUMN "codigo" INTEGER NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- AlterTable
ALTER TABLE "Executor" ALTER COLUMN "codigo" SET DATA TYPE BIGINT;

-- AlterTable
ALTER TABLE "Repassador" ALTER COLUMN "codigo" SET DATA TYPE BIGINT;

-- AlterTable
ALTER TABLE "Tomador" ALTER COLUMN "codigo" SET DATA TYPE BIGINT;
2 changes: 2 additions & 0 deletions backend/prisma/migrations/20241114234133_a/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- DropForeignKey
ALTER TABLE "Tipo" DROP CONSTRAINT "Tipo_idEixo_fkey";
2 changes: 1 addition & 1 deletion backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ model Projeto {
dataInicialEfetiva DateTime?
dataFinalEfetiva DateTime?
dataCadastro DateTime
especie String
especie String?
natureza String
situacao String
uf String
Expand Down
4 changes: 2 additions & 2 deletions backend/src/controllers/schemas/ProjetosRequestSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import z from "zod";
export const GetProjetosRequestSchema = z.object({
page: z.string().optional(),
pageSize: z.string().optional(),
nome: z.string().optional(),
situacao: z.string().optional(),
sortBy: z.enum(['nome', 'situacao', 'createdAt']).optional(),
uf: z.string().optional(),
sortBy: z.enum(['situacao', 'uf', 'createdAt']).optional(),
order: z.enum(['asc', 'desc']).optional(),
})
59 changes: 45 additions & 14 deletions backend/src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,49 @@ import { PrismaClient } from '@prisma/client';
export const prisma = new PrismaClient();

async function fetchAndSaveProjects() {
try {
const response = await axios.get('https://api.obrasgov.gestao.gov.br/obrasgov/api/projeto-investimento?pagina=0&tamanhoDaPagina=10');
if (response.status !== 200) {
throw new Error(`Erro ao buscar dados: ${response.statusText}`);
}
let pagina = 0;
const tamanhoDaPagina = 100; // Ajuste este valor conforme a capacidade da API
let hasMoreData = true;
let uf = "DF";

const { content: projetos } = response.data;
try {
while (hasMoreData) {
console.log(`Buscando página ${pagina}...`);

for (const projeto of projetos) {
const existingProject = await prisma.projeto.findUnique({
where: { id: projeto.idUnico }
// Faz a requisição para a API externa
const response = await axios.get('https://api.obrasgov.gestao.gov.br/obrasgov/api/projeto-investimento', {
params: {
pagina,
tamanhoDaPagina,
uf,
}
});

if (!existingProject) {
if (response.status !== 200) {
throw new Error(`Erro ao buscar dados: ${response.statusText}`);
}

const { content: projetos } = response.data;

// Se não há mais projetos, encerre o loop
if (projetos.length === 0) {
hasMoreData = false;
break;
}

// Salvar ou atualizar cada projeto no banco de dados
for (const projeto of projetos) {
// Verifica se o projeto já existe no banco de dados
const existingProject = await prisma.projeto.findUnique({
where: { id: projeto.idUnico }
});

// Se o projeto já existe, pule para o próximo
if (existingProject) {
console.log(`Projeto ${projeto.nome} já existe, ignorando.`);
continue;
}

await prisma.projeto.create({
data: {
id: projeto.idUnico,
Expand All @@ -32,7 +61,7 @@ async function fetchAndSaveProjects() {
dataInicialEfetiva: projeto.dataInicialEfetiva ? new Date(projeto.dataInicialEfetiva) : null,
dataFinalEfetiva: projeto.dataFinalEfetiva ? new Date(projeto.dataFinalEfetiva) : null,
dataCadastro: new Date(projeto.dataCadastro),
especie: projeto.especie,
especie: projeto.especie ?? null,
natureza: projeto.natureza,
situacao: projeto.situacao,
uf: projeto.uf,
Expand Down Expand Up @@ -82,9 +111,10 @@ async function fetchAndSaveProjects() {
}
});
console.log(`Projeto ${projeto.nome} salvo com sucesso!`);
} else {
console.log(`Projeto ${projeto.nome} já existe, ignorando.`);
}

// Incrementa para a próxima página
pagina++;
}
} catch (error) {
console.error('Erro ao buscar e salvar projetos:', error);
Expand All @@ -93,4 +123,5 @@ async function fetchAndSaveProjects() {
}
}

fetchAndSaveProjects()
// Sincroniza os dados a cada hora
fetchAndSaveProjects();
27 changes: 14 additions & 13 deletions backend/src/repositories/prisma/PrismaProjetosRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@ import { prisma } from "@/database";
export class PrismaProjetosRepository implements ProjetosRepository {
async find (params: FindProjetosParams): Promise<Projeto[]> {
let where: Prisma.ProjetoWhereInput = {
nome: {
contains: params.where?.nome?.like,
equals: params.where?.nome?.equals,
mode: params.where?.nome?.mode
situacao: {
contains: params.where?.situacao?.like,
equals: params.where?.situacao?.equals,
mode: params.where?.situacao?.mode
},
uf: {
contains: params.where?.uf?.like,
equals: params.where?.uf?.equals,
mode: params.where?.uf?.mode
},
situacao: params.where?.situacao,
}

const projetos = await prisma.projeto.findMany({
where,
orderBy: { [params.sortBy ?? "nome"]: params.order},
orderBy: { [params.sortBy ?? "uf"]: params.order},
skip: params.offset,
take: params.limit,
include: {
eixos: params.include?.eixos,
executores: params.include?.executores,
repassadores: params.include?.repassadores,
tomadores: params.include?.tomadores,
tipos: params.include?.tipos,
geometrias: params.include?.geometrias,
fontesDeRecurso: params.include?.fontesDeRecurso
eixos: true,
tipos: true,
geometrias: true,
fontesDeRecurso: true
}
})

Expand Down
10 changes: 7 additions & 3 deletions backend/src/repositories/projetoRepository.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { Projeto } from "@prisma/client";

export interface ProjetoWhereParams {
nome?: {
situacao?: {
like: string;
equals?: string;
mode?: "default" | "insensitive"
}
uf?: {
like: string;
equals?: string;
mode?: "default" | "insensitive"
}
situacao?: string;
}

export interface FindProjetosParams {
where?: ProjetoWhereParams
sortBy?: "nome" | "situacao" | "createdAt"
sortBy?: "situacao" | "uf" | "createdAt"
order?: "asc" | "desc"
limit?: number
offset?: number
Expand Down
12 changes: 6 additions & 6 deletions backend/src/use-cases/ProjetosService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import { ProjetosRepository, ProjetoWhereParams } from "@/repositories/projetoRe
interface GetProjetosWithPaginationParams {
page?: number
pageSize?: number
nome?: string
situacao?: string
sortBy?: "nome" | "situacao" | "createdAt"
uf?: string
sortBy?: "situacao" | "uf" | "createdAt"
order?: "asc" | "desc"
}

export class ProjetosService {
constructor(private readonly projetosRepository: ProjetosRepository) {}

async getAllProjetosPaginated(params: GetProjetosWithPaginationParams) {
const { nome, situacao, page = 1, pageSize = 10, sortBy, order } = params
const { situacao, uf, page = 1, pageSize = 10, sortBy, order } = params

const limit = pageSize
const offset = (page - 1) * limit

const where: ProjetoWhereParams = {}

if (nome) where.nome = { like: nome, mode: 'insensitive' }
if (situacao) where.situacao = situacao

if (situacao) where.situacao = { like: situacao, mode: 'insensitive' }
if (uf) where.uf = { like: uf, mode: 'insensitive' }
const projetos = await this.projetosRepository.find({
where,
sortBy,
Expand Down

0 comments on commit 908a245

Please sign in to comment.