diff --git a/prisma/seed.ts b/prisma/seed.ts index 6e03565..e701016 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -109,13 +109,6 @@ async function main() { model: 'ECOSYS P6235cdn', serialNumber: 'RCG0304510', networkId: 8 - }, - { - friendlyName: 'PNA', - ip: '10.6.0.32', - model: 'ECOSYS M2040dn', - serialNumber: 'VR91586430', - networkId: 8 } ] }) diff --git a/src/controllers/PrinterController.ts b/src/controllers/PrinterController.ts index f49b85d..8b5f56a 100644 --- a/src/controllers/PrinterController.ts +++ b/src/controllers/PrinterController.ts @@ -11,9 +11,25 @@ const router = Router() class PrinterController { static async index(req: Request, res: Response) { - const printers = await prisma.printer.findMany() + const { campus } = req.query - res.json(printers) + if (!campus) { + const printers = await prisma.printer.findMany({ + include: { network: true } + }) + return res.json(printers) + } + + const printers = await prisma.printer.findMany({ + where: { + network: { + shortName: String(campus) + } + }, + include: { network: true } + }) + + return res.json(printers) } static async show(req: Request, res: Response) { @@ -47,39 +63,39 @@ class PrinterController { else res.status(400).json({ error: 'Printer not found' }) } - static async create(req: Request, res: Response) { - const { friendlyName, ip } = req.body + // static async create(req: Request, res: Response) { + // const { friendlyName, ip } = req.body - const ipBlock = new Netmask(ip) + // const ipBlock = new Netmask(ip) - if (!isIPv4(ip)) { - res.status(400).json({ error: 'Invalid IP' }) - return - } + // if (!isIPv4(ip)) { + // res.status(400).json({ error: 'Invalid IP' }) + // return + // } - try { - const model = await PrinterStatusService.getPrinterModel(ip) - const printer = await prisma.printer.create({ - data: { - friendlyName, - ip, - model, - network: { - connect: {} - } - } - }) + // try { + // const model = await PrinterStatusService.getPrinterModel(ip) + // const printer = await prisma.printer.create({ + // data: { + // friendlyName, + // ip, + // model, + // network: { + // connect: {} + // } + // } + // }) - new PrinterStatusService(printer) + // new PrinterStatusService(printer) - res.json(printer) - } catch (e) { - res - .status(400) - .json({ error: 'Este IP não é de uma impressora suportada.' }) - return - } - } + // res.json(printer) + // } catch (e) { + // res + // .status(400) + // .json({ error: 'Este IP não é de uma impressora suportada.' }) + // return + // } + // } static async edit(req: Request, res: Response) { const { id } = req.params @@ -114,7 +130,7 @@ class PrinterController { router.use(hasRolesMiddleware(['ADMIN', 'INSPECTOR'])) router.get('/', PrinterController.index) -router.post('/', PrinterController.create) +// router.post('/', PrinterController.create) router.get('/:id', PrinterController.show) router.put('/:id', PrinterController.edit) router.delete('/:id', PrinterController.delete)