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 floor router.post('/', auth, authorize(['manage']), async (req, res) => { try { const { building_id } = req.body; const building = await Building.findOne({ where: { id: building_id } }); if (!building) { return res.status(404).json({ error: 'Building not found' }); } const existingFloors = await Floor.count({ where: { building_id } }); if (existingFloors >= building.floors) { const remaining = building.floors - existingFloors; return res.status(400).json({ error: `No more floors allowed. Remaining: ${remaining}` }); } const floor = await Floor.create(req.body); res.status(201).json(floor); } catch (err) { console.error('Create floor error:', err); res.status(500).json({ error: 'Failed to create floor' }); } }); // Get all floors by building ID router.get('/', auth, async (req, res) => { const { building_id } = req.query; try { // if (!building_id) { // return res.status(400).json({ error: 'building_id is required' }); // } const floors = await Floor.findAll({ // where: { building_id }, where: building_id ? { building_id } : undefined, include: [ { model: Room, as: 'rooms', include: [ { model: Bed, as: 'beds', include: [ { model: Renter, as: 'renter', required: false } ] } ] } ] }); res.json(floors); } catch (err) { console.error('Fetch floors error:', err); res.status(500).json({ error: 'Failed to fetch floors' }); } }); // Get a single floor by ID router.get('/:id', auth, async (req, res) => { try { const floor = await Floor.findByPk(req.params.id, { include: [ { model: Room, as: 'rooms', include: [ { model: Bed, as: 'beds', include: [{ model: Renter, as: 'renter', required: false }] } ] } ] }); if (!floor) { return res.status(404).json({ message: 'Floor not found' }); } res.json(floor); } catch (err) { console.error('Get floor error:', err); res.status(500).json({ message: 'Server error' }); } }); // Update a floor // router.put('/:id', auth, authorize(['manage']), async (req, res) => { // try { // const floor = await Floor.findByPk(req.params.id); // if (!floor) { // return res.status(404).json({ message: 'Floor not found' }); // } // await floor.update(req.body); // res.json(floor); // } catch (err) { // console.error('Update floor error:', err); // res.status(500).json({ message: 'Failed to update floor' }); // } // }); router.put('/:id', auth, authorize(['manage']), async (req, res) => { try { const floorId = req.params.id; const updateData = req.body; const floor = await Floor.findByPk(floorId); if (!floor) { return res.status(404).json({ message: 'Floor not found' }); } // Optional: Prevent updating to a building that exceeds allowed floors if (updateData.building_id) { const building = await Building.findByPk(updateData.building_id); if (!building) { return res.status(400).json({ message: 'Invalid building_id' }); } const floorCount = await Floor.count({ where: { building_id: updateData.building_id } }); // If changing to another building, and it would exceed its limit if (updateData.building_id !== floor.building_id && floorCount >= building.floors) { return res.status(400).json({ message: `Building already has the maximum of ${building.floors} floors.` }); } } await floor.update(updateData); res.json(floor); } catch (err) { console.error('Update floor error:', err); res.status(500).json({ message: 'Failed to update floor' }); } }); // Delete a floor router.delete('/:id', auth, authorize(['delete']), async (req, res) => { try { const floor = await Floor.findByPk(req.params.id); if (!floor) { return res.status(404).json({ message: 'Floor not found' }); } await floor.destroy(); res.json({ message: 'Floor deleted successfully' }); } catch (err) { console.error('Delete floor error:', err); res.status(500).json({ message: 'Failed to delete floor' }); } }); export default router; |
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0075 ]-- |