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 { Bed, Room, Floor, Building, Renter, Activity } from '../models/index.js';
import { auth, authorize } from '../middleware/auth.js';
import { Op } from 'sequelize';
import { bedValidators } from '../middleware/validators.js';
const router = express.Router();
// ✅ Create a bed
router.post('/', auth, authorize(['manage']), bedValidators.create, async (req, res) => {
try {
const { building_id, room_id } = req.body;
const building = await Building.findOne({ where: { id: building_id } });
const room = await Room.findOne({ where: { id: room_id } });
if (!building) {
return res.status(404).json({ error: 'Building not found' });
}
if (!room) {
return res.status(404).json({ error: 'Room not found' });
}
// Count beds in the building
const buildingBedCount = await Bed.count({ where: { building_id } });
if (buildingBedCount >= building.total_beds) {
return res.status(400).json({
error: `No more beds allowed in Building.`
});
}
// Count beds in the specific room
const roomBedCount = await Bed.count({ where: { room_id } });
if (roomBedCount >= room.total_beds) {
const remainingRoom = room.total_beds - roomBedCount;
return res.status(400).json({
error: `No more beds allowed in Room. Remaining: ${remainingRoom}`
});
}
// Add floor_id from room into the bed data
const bedData = {
...req.body,
floor_id: room.floor_id
};
const bed = await Bed.create(bedData);
const data = {
model_id: bed.id,
model_name: 'Bed',
description: `New bed #${bed.bed_number} has been created`,
};
const activity = await Activity.create(data);
res.status(201).json(bed);
} catch (err) {
console.error('Create bed error:', err);
res.status(500).json({ error: 'Failed to create bed' });
}
});
// ✅ Get all beds (optionally by room_id)
router.get('/', async (req, res) => {
const { room_id } = req.query;
try {
const whereClause = room_id ? { room_id } : {};
const beds = await Bed.findAll({
where: whereClause,
include: [
{
model: Renter,
as: 'renter',
required: false
},
{
model: Room,
as: 'room',
include: [
{
model: Floor,
as: 'floor',
include: [{ model: Building, as: 'building' }]
}
]
}
]
});
res.json(beds);
} catch (err) {
console.error('Fetch beds error:', err);
res.status(500).json({ error: 'Failed to fetch beds' });
}
});
// ✅ Get a single bed by ID
router.get('/:id', async (req, res) => {
try {
const bed = await Bed.findByPk(req.params.id, {
include: [
{
model: Renter,
as: 'renter',
required: false
},
{
model: Room,
as: 'room',
include: [{ model: Floor, as: 'floor' }]
}
]
});
if (!bed) {
return res.status(404).json({ message: 'Bed not found' });
}
res.json(bed);
} catch (err) {
console.error('Get bed error:', err);
res.status(500).json({ message: 'Server error' });
}
});
// ✅ Update a bed
router.put('/:id', auth, authorize(['manage']), bedValidators.update, async (req, res) => {
try {
const bed = await Bed.findByPk(req.params.id);
if (!bed) {
return res.status(404).json({ message: 'Bed not found' });
}
const { building_id, room_id } = req.body;
const building = await Building.findByPk(building_id);
const room = await Room.findByPk(room_id);
if (!building) {
return res.status(404).json({ error: 'Building not found' });
}
if (!room) {
return res.status(404).json({ error: 'Room not found' });
}
// Check building bed limit (excluding current bed)
const buildingBedCount = await Bed.count({
where: {
building_id,
id: { [Op.ne]: bed.id }, // exclude this bed
}
});
if (buildingBedCount >= building.total_beds) {
return res.status(400).json({
error: `No more beds allowed in Building. Remaining: ${building.total_beds - buildingBedCount}`
});
}
// Check room bed limit (excluding current bed)
const roomBedCount = await Bed.count({
where: {
room_id,
id: { [Op.ne]: bed.id }, // exclude this bed
}
});
if (roomBedCount >= room.total_beds) {
return res.status(400).json({
error: `No more beds allowed in Room. Remaining: ${room.total_beds - roomBedCount}`
});
}
// Merge floor_id into update data
const updatedData = {
...req.body,
floor_id: room.floor_id
};
await bed.update(updatedData);
const data = {
model_id: bed.id,
model_name: 'Bed',
description: `Bed #${bed.bed_number} has been updated`,
};
const activity = await Activity.create(data);
res.json(bed);
} catch (err) {
console.error('Update bed error:', err);
res.status(500).json({ message: 'Failed to update bed' });
}
});
// ✅ Delete a bed
router.delete('/:id', auth, authorize(['delete']), async (req, res) => {
try {
const bed = await Bed.findByPk(req.params.id);
if (!bed) {
return res.status(404).json({ message: 'Bed not found' });
}
const data = {
model_id: bed.id,
model_name: 'Bed',
description: `Bed #${bed.bed_number} has been deleted`,
};
const activity = await Activity.create(data);
await bed.destroy();
res.json({ message: 'Bed deleted successfully' });
} catch (err) {
console.error('Delete bed error:', err);
res.status(500).json({ message: 'Failed to delete bed' });
}
});
export default router;
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0032 ]-- |