File size: 2,990 Bytes
fcf8749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

// Get all virtual hubs
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' });
  }
};

// Create new virtual hub
exports.createVirtualHub = async (req, res) => {
  try {
    const { name, address, latitude, longitude, type, radius } = req.body;
    
    // Validation
    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' });
  }
};

// Get single virtual hub by ID
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' });
  }
};

// Update 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' });
  }
};

// Delete 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' });
  }
};