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 Visitor from '../models/Visitor.js';
import { Floor, Building, Room, Bed, Renter } from '../models/index.js';
import { visitorValidators } from '../middleware/validators.js';
const router = express.Router();
// CREATE a new visitor
router.post('/', visitorValidators.create, async (req, res) => {
try {
const { visiting_renter_id, building_id } = req.body;
let user_id = '';
if (visiting_renter_id) {
const renter = await Renter.findOne({ where: { id: visiting_renter_id } });
if (!renter) {
return res.status(404).json({ error: 'Renter not found' });
}
user_id = renter.user_id;
}
const building = await Building.findOne({ where: { id: building_id } });
if (!building) {
return res.status(404).json({ error: 'Building not found' });
}
// const visitor = await Visitor.create(req.body);
const visitor = await Visitor.create({
...req.body,
user_id: user_id,
});
res.status(201).json(visitor);
} catch (err) {
console.error('Create visitor error:', err);
res.status(400).json({ error: 'Failed to create visitor', details: err.message });
}
});
// READ all visitors
router.get('/', async (req, res) => {
try {
const visitors = await Visitor.findAll({
include: [
{
model: Building,
as: 'building',
},
],
order: [['created_at', 'DESC']]
});
res.json(visitors);
} catch (err) {
console.error('Fetch visitors error:', err);
res.status(500).json({ error: 'Failed to fetch visitors' });
}
});
// READ a single visitor by ID
router.get('/:id', async (req, res) => {
try {
const visitor = await Visitor.findByPk(req.params.id);
if (!visitor) return res.status(404).json({ error: 'Visitor not found' });
res.json(visitor);
} catch (err) {
res.status(500).json({ error: 'Failed to fetch visitor' });
}
});
// UPDATE a visitor
router.put('/:id', visitorValidators.update, async (req, res) => {
try {
const { visiting_renter_id } = req.body;
const visitor = await Visitor.findByPk(req.params.id);
if (!visitor) return res.status(404).json({ error: 'Visitor not found' });
let user_id = '';
if (visiting_renter_id) {
const renter = await Renter.findOne({ where: { id: visiting_renter_id } });
if (!renter) {
return res.status(404).json({ error: 'Renter not found' });
}
user_id = renter.user_id;
}
await visitor.update({
...req.body,
user_id: user_id,
});
res.json(visitor);
} catch (err) {
console.error('Update visitor error:', err);
res.status(400).json({ error: 'Failed to update visitor', details: err.message });
}
});
// DELETE a visitor
router.delete('/:id', async (req, res) => {
try {
const visitor = await Visitor.findByPk(req.params.id);
if (!visitor) return res.status(404).json({ error: 'Visitor not found' });
await visitor.destroy();
res.json({ message: 'Visitor deleted' });
} catch (err) {
console.error('Delete visitor error:', err);
res.status(500).json({ error: 'Failed to delete visitor' });
}
});
router.post('/checkout/:id', async (req, res) => {
try {
const visitor = await Visitor.findByPk(req.params.id);
if (!visitor) return res.status(404).json({ error: 'Visitor not found' });
await visitor.update({
check_out_time: new Date()
});
res.json({ message: 'Visitor checkout' });
} catch (err) {
console.error('checkout visitor error:', err);
res.status(500).json({ error: 'Failed to checkout visitor' });
}
});
export default router;
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0035 ]-- |