Skip to content

Commit

Permalink
route fix
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensusas committed Nov 19, 2024
1 parent 992fa12 commit 0b1a0b4
Showing 1 changed file with 16 additions and 70 deletions.
86 changes: 16 additions & 70 deletions backend/routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ router.post('/login-user', async (req, res) => {

const token = jwt.sign(
{
id: user.userid,
userId: user.userid,
role: user.role
},
'CompGrid_SuperSecretKey_2024',
Expand Down Expand Up @@ -119,7 +119,10 @@ router.post('/register-user', async (req, res) => {
}
});

router.use(authenticateToken);
router.use(authenticateToken, (req, res, next) => {
console.log('Authenticated user:', req.user);
next();
});

router.get('/instances', async (req, res) => {
try {
Expand Down Expand Up @@ -453,75 +456,18 @@ router.get('/instances/:userId', authenticateToken, async (req, res) => {
});

// Main endpoint handler
router.post('/request-instance', authenticateToken, authenticateUser, async (req, res) => {
const client = await pool.connect();

router.post('/request-instance', async (req, res) => {
try {
const userId = req.user.userid;
const { instancetype } = req.body;

console.log('Request received:', { userId, instancetype });

await client.query('BEGIN');

// First, find an available instance of the requested type
const instanceTypeResult = await client.query(`
SELECT i.instanceid, it.instancetypeid
FROM public.instancetype it
JOIN public.instance i ON it.instancetypeid = i.instancetypeid
WHERE it.instancetype = $1
AND i.allocateduserid IS NULL
AND i.booted = false
LIMIT 1
FOR UPDATE SKIP LOCKED
`, [instancetype]);

if (instanceTypeResult.rows.length === 0) {
await client.query('ROLLBACK');
return res.status(404).json({ message: 'No available instances of this type' });
}

const { instanceid, instancetypeid } = instanceTypeResult.rows[0];

// Allocate the instance to the user
const allocationResult = await client.query(`
UPDATE public.instance i
SET allocateduserid = $1
WHERE instanceid = $2
RETURNING
i.instanceid,
i.instancename,
i.ipaddress,
i.username,
i.password,
i.booted as status,
(SELECT instancetype FROM public.instancetype WHERE instancetypeid = i.instancetypeid) as type,
(SELECT systemtype FROM public.instancetype WHERE instancetypeid = i.instancetypeid) as systemtype,
(SELECT cpucorecount FROM public.instancetype WHERE instancetypeid = i.instancetypeid) as cpucorecount,
(SELECT memory FROM public.instancetype WHERE instancetypeid = i.instancetypeid) as memory,
(SELECT storage FROM public.instancetype WHERE instancetypeid = i.instancetypeid) as storage,
(SELECT priceperhour FROM public.pricetier pt
JOIN public.instancetype it ON pt.pricetierId = it.pricetierId
WHERE it.instancetypeid = i.instancetypeid) as priceperhour
`, [userId, instanceid]);

if (allocationResult.rowCount === 0) {
throw new Error('Failed to allocate instance');
}

// Update the free count
await client.query(`
UPDATE public.instancetype
SET free_count = free_count - 1
WHERE instancetypeid = $1
`, [instancetypeid]);

await client.query('COMMIT');

res.json({
message: 'Instance allocated successfully',
instance: allocationResult.rows[0],
});
const userId = req.user.userId; // Changed from req.user.id
const {
cpucorecount,
memory,
storage,
systemtype,
maxPricePerHour
} = req.body;

// Rest of the implementation...
} catch (err) {
console.error('Transaction error:', err);
await client.query('ROLLBACK');
Expand Down

0 comments on commit 0b1a0b4

Please sign in to comment.