Include network info

This commit is contained in:
Douglas Barone 2023-06-21 14:07:49 -04:00
parent 3b368d4761
commit e1aadc3837
2 changed files with 47 additions and 38 deletions

View File

@ -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
}
]
})

View File

@ -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)