From c1a8a7ba917e063fa3623322afa6b3dfbc77c27c Mon Sep 17 00:00:00 2001 From: apsr23 <apsr23@inf.ufpr.br> Date: Mon, 24 Mar 2025 10:00:51 -0300 Subject: [PATCH] FIX implants handlers --- src/handlers/implants.ts | 46 ++++++++++++++++++++++++++++------------ src/handlers/user.ts | 4 ++-- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/handlers/implants.ts b/src/handlers/implants.ts index 08919c3..7e81b92 100644 --- a/src/handlers/implants.ts +++ b/src/handlers/implants.ts @@ -2,11 +2,11 @@ import { type Request, type Response } from 'express'; import { implantsTable } from '@/db/schema'; import { db } from "@/db"; import { eq } from 'drizzle-orm'; -import { implantSchema } from '@/validators/implantsValidator'; +import { implantSchema, updateImplantSchema } from '@/validators/implantsValidator'; export default class implant{ static implantRequestValidation (req: Request){ - const validation = implantSchema.safeParse(req.body); + const validation = updateImplantSchema.safeParse(req.body); return validation.success; } @@ -19,13 +19,19 @@ export default class implant{ return searched[0] || null; } - static async implantRead(id: number, req: Request, res: Response){ - if (!this.implantRequestValidation(req)) + static async implantRead(req: Request, res: Response){ + if (!implant.implantRequestValidation(req)) { return res.status(400).json({ error: "Invalid Request"}); } - const holder = await this.getImplant(id); + const { id } = req.params; + const parsedId = parseInt(id, 10); + if (isNaN(parsedId)) { + return res.status(400).json({ error: "ID inválido" }); + } + + const holder = await implant.getImplant(parsedId); if (!holder) { return res.status(404).json({ error: "Not Found"}); @@ -35,13 +41,19 @@ export default class implant{ } - static async implantUpdate(id: number, req: Request, res: Response){ - if (!this.implantRequestValidation(req)) + static async implantUpdate(req: Request, res: Response){ + if (!implant.implantRequestValidation(req)) { return res.status(400).json({ error: "Invalid Request"}); } - const updated_implant = await this.getImplant(id); + const { id } = req.params; + const parsedId = parseInt(id, 10); + if (isNaN(parsedId)) { + return res.status(400).json({ error: "ID inválido" }); + } + + const updated_implant = await implant.getImplant(parsedId); if (!updated_implant) { return res.status(404).json({ error: "Not Found"}); @@ -55,7 +67,7 @@ export default class implant{ }; try{ - await db.update(implantsTable).set(updates).where(eq(implantsTable.id, id)); + await db.update(implantsTable).set(updates).where(eq(implantsTable.id, parsedId)); } catch (error) { return res.status (500).json({ error: "Update User Error"}) } @@ -63,19 +75,25 @@ export default class implant{ return res.status(200).json({ message: "OK"}); } - static async implantDelete(id: number, req: Request, res: Response){ - if (!this.implantRequestValidation(req)) + static async implantDelete(req: Request, res: Response){ + if (!implant.implantRequestValidation(req)) { return res.status(400).json("Invalid Request"); } - if (!this.getImplant(id)) + const { id } = req.params; + const parsedId = parseInt(id, 10); + if (isNaN(parsedId)) { + return res.status(400).json({ error: "ID inválido" }); + } + + if (!implant.getImplant(parsedId)) { return res.status(404).json("Not Found"); } try{ - await db.delete(implantsTable).where(eq(implantsTable.id, id)); + await db.delete(implantsTable).where(eq(implantsTable.id, parsedId)); return res.status(200).json({ message: "Implant removed successfully"}); } catch (error) { return res.status(500).json({ error: "Implant Removal Error"}); @@ -83,7 +101,7 @@ export default class implant{ } static async implantCreate(req: Request, res: Response){ - const implant_package = this.implantRequestUnzip(req); + const implant_package = implant.implantRequestUnzip(req);setInterval if (!implant_package.success) { return res.status(400).json("Invalid Request"); diff --git a/src/handlers/user.ts b/src/handlers/user.ts index f4adb5d..8f8ce48 100644 --- a/src/handlers/user.ts +++ b/src/handlers/user.ts @@ -133,7 +133,7 @@ export default class User{ static readUser = async (req: Request, res: Response): Promise<any> => { try { - // extrair id e nome da requisição + // extrair id da requisição const { id } = req.params; // converter id para número @@ -182,7 +182,7 @@ export default class User{ return res.status(401).json({ message: 'Senha incorreta' }); } - // criar chave secreta para assinar e validar tokens, guardar no .env + // acessar no .env a chave secreta para assinar e validar tokens const jwtSecret = process.env["APP_SECRET"]; if (!jwtSecret) { return res.status(500).json({ error: "Chave secreta do JWT não configurada" }); -- GitLab