| const { PrismaClient } = require('@prisma/client'); |
| const prisma = new PrismaClient(); |
|
|
| |
| exports.getAllVirtualHubs = async (req, res) => { |
| try { |
| const hubs = await prisma.virtualHub.findMany({ |
| orderBy: { |
| createdAt: 'desc' |
| } |
| }); |
| |
| res.json(hubs); |
| } catch (error) { |
| console.error('Error fetching virtual hubs:', error); |
| res.status(500).json({ error: 'Failed to fetch virtual hubs' }); |
| } |
| }; |
|
|
| |
| exports.createVirtualHub = async (req, res) => { |
| try { |
| const { name, address, latitude, longitude, type, radius } = req.body; |
| |
| |
| if (!name || !latitude || !longitude) { |
| return res.status(400).json({ |
| error: 'Name, latitude, and longitude are required' |
| }); |
| } |
| |
| const newHub = await prisma.virtualHub.create({ |
| data: { |
| name, |
| address: address || null, |
| latitude: parseFloat(latitude), |
| longitude: parseFloat(longitude), |
| type: type || null, |
| radius: radius ? parseFloat(radius) : null |
| } |
| }); |
| |
| res.status(201).json(newHub); |
| } catch (error) { |
| console.error('Error creating virtual hub:', error); |
| res.status(500).json({ error: 'Failed to create virtual hub' }); |
| } |
| }; |
|
|
| |
| exports.getVirtualHubById = async (req, res) => { |
| try { |
| const { id } = req.params; |
| |
| const hub = await prisma.virtualHub.findUnique({ |
| where: { id } |
| }); |
| |
| if (!hub) { |
| return res.status(404).json({ error: 'Virtual hub not found' }); |
| } |
| |
| res.json(hub); |
| } catch (error) { |
| console.error('Error fetching virtual hub:', error); |
| res.status(500).json({ error: 'Failed to fetch virtual hub' }); |
| } |
| }; |
|
|
| |
| exports.updateVirtualHub = async (req, res) => { |
| try { |
| const { id } = req.params; |
| const { name, address, latitude, longitude, type, radius } = req.body; |
| |
| const updatedHub = await prisma.virtualHub.update({ |
| where: { id }, |
| data: { |
| ...(name && { name }), |
| ...(address !== undefined && { address }), |
| ...(latitude && { latitude: parseFloat(latitude) }), |
| ...(longitude && { longitude: parseFloat(longitude) }), |
| ...(type !== undefined && { type }), |
| ...(radius !== undefined && { radius: radius ? parseFloat(radius) : null }) |
| } |
| }); |
| |
| res.json(updatedHub); |
| } catch (error) { |
| console.error('Error updating virtual hub:', error); |
| res.status(500).json({ error: 'Failed to update virtual hub' }); |
| } |
| }; |
|
|
| |
| exports.deleteVirtualHub = async (req, res) => { |
| try { |
| const { id } = req.params; |
| |
| await prisma.virtualHub.delete({ |
| where: { id } |
| }); |
| |
| res.json({ message: 'Virtual hub deleted successfully' }); |
| } catch (error) { |
| console.error('Error deleting virtual hub:', error); |
| res.status(500).json({ error: 'Failed to delete virtual hub' }); |
| } |
| }; |
|
|