Skip to content

Commit

Permalink
atualização de filtros e rotas
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-lima258 committed Nov 19, 2024
1 parent 681a569 commit 7c0631b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions backend/src/controllers/schemas/ProjetosRequestSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import z from "zod";
export const GetProjetosRequestSchema = z.object({
page: z.string().optional(),
pageSize: z.string().optional(),
natureza: z.string().optional(),
situacao: z.string().optional(),
uf: z.string().optional(),
sortBy: z.enum(['situacao', 'uf', 'createdAt']).optional(),
Expand Down
4 changes: 2 additions & 2 deletions backend/src/database/fetchAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import axios from "axios";
import { prisma } from "@/database";

async function fetchAndSaveProjects() {
let pagina = 0;
const tamanhoDaPagina = 100; // Ajuste este valor conforme a capacidade da API
let pagina = 56;
const tamanhoDaPagina = 10; // Ajuste este valor conforme a capacidade da API
let hasMoreData = true;
let uf = "DF";

Expand Down
6 changes: 6 additions & 0 deletions backend/src/repositories/prisma/PrismaProjetosRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export class PrismaProjetosRepository implements ProjetosRepository {
equals: params.where?.uf?.equals,
mode: params.where?.uf?.mode
},
natureza: {
contains: params.where?.natureza?.like,
equals: params.where?.natureza?.equals,
mode: params.where?.natureza?.mode
}

}

const projetos = await prisma.projeto.findMany({
Expand Down
7 changes: 6 additions & 1 deletion backend/src/repositories/projetoRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ export interface ProjetoWhereParams {
equals?: string;
mode?: "default" | "insensitive"
}
natureza?: {
like: string;
equals?: string;
mode?: "default" | "insensitive"
}
}

export interface FindProjetosParams {
where?: ProjetoWhereParams
sortBy?: "situacao" | "uf" | "createdAt"
sortBy?: "natureza"| "situacao" | "uf" | "createdAt"
order?: "asc" | "desc"
limit?: number
offset?: number
Expand Down
4 changes: 2 additions & 2 deletions backend/src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Router } from "express";

const router = Router()

router.get('/projects', projetosController.index)
router.get('/projects/:id', projetosController.show)
router.get('/projeto-investimento', projetosController.index)
router.get('/projeto-investimento/:id', projetosController.show)

export { router }
2 changes: 1 addition & 1 deletion backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const app = express();

app.use(cors());
app.use(express.json());
app.use('/api', router)
app.use('/', router)
app.use(errorHandling);

const PORT = process.env.PORT || 3333;
Expand Down
6 changes: 4 additions & 2 deletions backend/src/use-cases/ProjetosService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ import { ProjetosRepository, ProjetoWhereParams } from "@/repositories/projetoRe
interface GetProjetosWithPaginationParams {
page?: number
pageSize?: number
natureza?: string
situacao?: string
uf?: string
sortBy?: "situacao" | "uf" | "createdAt"
sortBy?: "natureza" | "situacao" | "uf" | "createdAt"
order?: "asc" | "desc"
}

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

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

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

const where: ProjetoWhereParams = {}

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

const projetos = await this.projetosRepository.find({
Expand Down

0 comments on commit 7c0631b

Please sign in to comment.