Software: Apache. PHP/8.1.30 uname -a: Linux server1.tuhinhossain.com 5.15.0-163-generic #173-Ubuntu SMP Tue Oct 14 17:51:00 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, Activity } from '../models/index.js';
import { auth, authorize } from '../middleware/auth.js';
import { floorValidators } from '../middleware/validators.js';
const router = express.Router();
// Create a floor
router.post('/', auth, authorize(['manage']), floorValidators.create, 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) {
return res.status(400).json({ error: `No more floors allowed.` });
}
const floor = await Floor.create(req.body);
const data = {
model_id: floor.id,
model_name: 'Floor',
description: `New floor #${floor.floor_number} has been created`,
};
const activity = await Activity.create(data);
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('/', async (req, res) => {
const { building_id } = req.query;
try {
const floors = await Floor.findAll({
where: building_id ? { building_id } : undefined,
include: [
{
model: Room,
as: 'rooms',
include: [
{
model: Bed,
as: 'beds',
include: [
{
model: Renter,
as: 'renter',
required: false
}
]
}
]
}, {
model: Building,
as: 'building',
attributes: ['id', 'name'],
},
],
order: [['created_at', 'DESC']]
});
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', 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']), floorValidators.update, 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);
const data = {
model_id: floor.id,
model_name: 'Floor',
description: `Floor #${floor.floor_number} has been updated`,
};
const activity = await Activity.create(data);
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' });
}
const data = {
model_id: floor.id,
model_name: 'Floor',
description: `Floor #${floor.floor_number} has been deleted`,
};
const activity = await Activity.create(data);
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.0034 ]-- |