135 lines
3.8 KiB
TypeScript

// src/controllers/RouterController.ts
import { Request, Response } from 'express';
import { RouterService } from '../services/RouterService';
import { Pool } from 'mysql2/promise';
import { RouterData, VMUpdate, VMUpdateRequest } from '../types';
export class RouterController {
private service: RouterService;
constructor(pool: Pool) {
this.service = new RouterService(pool);
}
// src/controllers/RouterController.ts
// src/controllers/RouterController.ts
updateRouterVMs = async (req: Request, res: Response) => {
try {
const routerId = req.query.router_id as string;
const { vms } = req.body;
// Add debugging logs
console.log('Received request:');
console.log('Router ID:', routerId);
console.log('VMs data:', vms);
if (!routerId) {
return res.status(400).json({ error: 'router_id is required' });
}
if (!Array.isArray(vms)) {
return res.status(400).json({ error: 'VMs must be an array' });
}
const updatedVMs = await this.service.updateRouterVMs(routerId, vms);
res.json(updatedVMs);
} catch (err) {
// Type cast the error
const error = err as Error;
// Enhanced error logging
console.error('Error in updateRouterVMs:', {
message: error?.message || 'Unknown error',
stack: error?.stack,
type: error?.constructor.name
});
res.status(500).json({
error: 'Failed to update VMs',
details: error?.message || 'Unknown error'
});
}
};
getAllRouters = async (req: Request, res: Response) => {
try {
const routers = await this.service.getAllRouters();
res.json(routers);
} catch (error: unknown) {
if (error && typeof error === 'object' && 'message' in error) {
const e = error as { message: string }; // Type assertion here
res.status(500).json({ error: e.message });
} else {
res.status(500).json({ error: 'An unexpected error occurred' });
}
}
};
getRouterById = async (req: Request, res: Response) => {
try {
const id = parseInt(req.params.id);
const router = await this.service.getRouterById(id);
if (!router) {
return res.status(404).json({ error: 'Router not found' });
}
res.json(router);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch router' });
}
};
createRouter = async (req: Request, res: Response) => {
try {
const router = await this.service.createRouter(req.body);
res.status(201).json(router);
} catch (error) {
res.status(500).json({ error: 'Failed to create router' });
}
};
updateRouter = async (req: Request, res: Response) => {
try {
const id = parseInt(req.params.id);
const router = await this.service.updateRouter(id, req.body);
if (!router) {
return res.status(404).json({ error: 'Router not found' });
}
res.json(router);
} catch (error) {
res.status(500).json({ error: 'Failed to update router' });
}
};
deleteRouter = async (req: Request, res: Response) => {
try {
const id = parseInt(req.params.id);
const success = await this.service.deleteRouter(id);
if (!success) {
return res.status(404).json({ error: 'Router not found' });
}
res.status(204).send();
} catch (error) {
res.status(500).json({ error: 'Failed to delete router' });
}
};
getRoutersByFacility = async (req: Request, res: Response) => {
try {
const facility = req.params.facility;
const routers = await this.service.getRoutersByFacility(facility);
res.json(routers);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch routers by facility' });
}
};
}