Software: Apache. PHP/8.1.30 uname -a: Linux server1.tuhinhossain.com 5.15.0-151-generic #161-Ubuntu SMP Tue Jul 22 14:25:40 UTC uid=1002(picotech) gid=1003(picotech) groups=1003(picotech),0(root) Safe-mode: OFF (not secure) /home/picotech/domains/rentals.picotech.app/public_html/server/routes/ drwxr-xr-x |
Viewing file: Select action/file-type: import express from 'express'; import { Floor,Building,Room, Bed, Renter } from '../models/index.js'; import { auth, authorize } from '../middleware/auth.js'; const router = express.Router(); // Create a room router.post('/', auth, authorize(['manage']), async (req, res) => { try { const { building_id,floor_id } = req.body; const building = await Building.findOne({ where: { id: building_id } }); const floor = await Floor.findOne({ where: { id: floor_id } }); if (!building) { return res.status(404).json({ error: 'Building not found' }); } if (!floor) { return res.status(404).json({ error: 'Floor not found' }); } const existingRooms = await Room.count({ where: { building_id } }); if (existingRooms >= building.total_rooms) { const remainingBuilding = building.total_rooms - existingRooms; return res.status(400).json({ error: `No more rooms allowed in Building. Remaining: ${remainingBuilding}` }); } if (existingRooms >= floor.total_rooms) { const remaining = floor.total_rooms - existingRooms; return res.status(400).json({ error: `No more rooms allowed in Floor. Remaining: ${remaining}` }); } const room = await Room.create(req.body); res.status(201).json(room); } catch (err) { console.error('Create room error:', err); res.status(500).json({ error: 'Failed to create room' }); } }); // Get all rooms by floor ID router.get('/', auth, async (req, res) => { try { const rooms = await Room.findAll({ include: [ { model: Building, as: 'building', }, { model: Floor, as: 'floor', }, { model: Bed, as: 'beds', include: [ { model: Renter, as: 'renter', required: false } ] } ] }); res.json(rooms); } catch (err) { console.error('Fetch rooms error:', err); res.status(500).json({ error: 'Failed to fetch rooms' }); } }); // Get a single room by ID router.get('/:id', auth, async (req, res) => { try { const room = await Room.findByPk(req.params.id, { include: [ { model: Bed, as: 'beds', include: [{ model: Renter, as: 'renter', required: false }] } ] }); if (!room) { return res.status(404).json({ message: 'Room not found' }); } res.json(room); } catch (err) { console.error('Get room error:', err); res.status(500).json({ message: 'Server error' }); } }); // Update a room router.put('/:id', auth, authorize(['manage']), async (req, res) => { try { const room = await Room.findByPk(req.params.id); if (!room) { return res.status(404).json({ message: 'Room not found' }); } await room.update(req.body); res.json(room); } catch (err) { console.error('Update room error:', err); res.status(500).json({ message: 'Failed to update room' }); } }); // Delete a room router.delete('/:id', auth, authorize(['delete']), async (req, res) => { try { const room = await Room.findByPk(req.params.id); if (!room) { return res.status(404).json({ message: 'Room not found' }); } await room.destroy(); res.json({ message: 'Room deleted successfully' }); } catch (err) { console.error('Delete room error:', err); res.status(500).json({ message: 'Failed to delete room' }); } }); export default router; |
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.004 ]-- |