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', model: 'ECOSYS P6235cdn',
serialNumber: 'RCG0304510', serialNumber: 'RCG0304510',
networkId: 8 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 { class PrinterController {
static async index(req: Request, res: Response) { 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) { static async show(req: Request, res: Response) {
@ -47,39 +63,39 @@ class PrinterController {
else res.status(400).json({ error: 'Printer not found' }) else res.status(400).json({ error: 'Printer not found' })
} }
static async create(req: Request, res: Response) { // static async create(req: Request, res: Response) {
const { friendlyName, ip } = req.body // const { friendlyName, ip } = req.body
const ipBlock = new Netmask(ip) // const ipBlock = new Netmask(ip)
if (!isIPv4(ip)) { // if (!isIPv4(ip)) {
res.status(400).json({ error: 'Invalid IP' }) // res.status(400).json({ error: 'Invalid IP' })
return // return
} // }
try { // try {
const model = await PrinterStatusService.getPrinterModel(ip) // const model = await PrinterStatusService.getPrinterModel(ip)
const printer = await prisma.printer.create({ // const printer = await prisma.printer.create({
data: { // data: {
friendlyName, // friendlyName,
ip, // ip,
model, // model,
network: { // network: {
connect: {} // connect: {}
} // }
} // }
}) // })
new PrinterStatusService(printer) // new PrinterStatusService(printer)
res.json(printer) // res.json(printer)
} catch (e) { // } catch (e) {
res // res
.status(400) // .status(400)
.json({ error: 'Este IP não é de uma impressora suportada.' }) // .json({ error: 'Este IP não é de uma impressora suportada.' })
return // return
} // }
} // }
static async edit(req: Request, res: Response) { static async edit(req: Request, res: Response) {
const { id } = req.params const { id } = req.params
@ -114,7 +130,7 @@ class PrinterController {
router.use(hasRolesMiddleware(['ADMIN', 'INSPECTOR'])) router.use(hasRolesMiddleware(['ADMIN', 'INSPECTOR']))
router.get('/', PrinterController.index) router.get('/', PrinterController.index)
router.post('/', PrinterController.create) // router.post('/', PrinterController.create)
router.get('/:id', PrinterController.show) router.get('/:id', PrinterController.show)
router.put('/:id', PrinterController.edit) router.put('/:id', PrinterController.edit)
router.delete('/:id', PrinterController.delete) router.delete('/:id', PrinterController.delete)