| 1 | const { adminQueries } = require('../models/queries');
|
|---|
| 2 | const db = require('../config/database');
|
|---|
| 3 |
|
|---|
| 4 | const DEMO_ADMIN_ID = '';
|
|---|
| 5 |
|
|---|
| 6 | exports.dashboard = (req, res) => {
|
|---|
| 7 | res.render('admin/dashboard');
|
|---|
| 8 | };
|
|---|
| 9 |
|
|---|
| 10 | exports.selectBuilding = async (req, res) => {
|
|---|
| 11 | try {
|
|---|
| 12 | // Hardcoded admin for demo purposes
|
|---|
| 13 | const adminResult = await db.getPool().query(
|
|---|
| 14 | `SELECT admin_id FROM admin WHERE name = 'Nikola' LIMIT 1`
|
|---|
| 15 | );
|
|---|
| 16 | const admin_id = adminResult.rows[0]?.admin_id;
|
|---|
| 17 |
|
|---|
| 18 | if (!admin_id) {
|
|---|
| 19 | return res.status(404).send('Admin not found');
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | const buildings = await adminQueries.getBuildingsByAdminId(admin_id);
|
|---|
| 23 | res.render('admin/select-building', { buildings, admin_id: admin_id });
|
|---|
| 24 | } catch (error) {
|
|---|
| 25 | console.error('Error fetching buildings:', error);
|
|---|
| 26 | res.status(500).send('Database error');
|
|---|
| 27 | }
|
|---|
| 28 | };
|
|---|
| 29 |
|
|---|
| 30 | exports.selectFloor = async (req, res) => {
|
|---|
| 31 | try {
|
|---|
| 32 | const { building_id, admin_id } = req.query;
|
|---|
| 33 |
|
|---|
| 34 | const building = await adminQueries.getBuildingById(building_id);
|
|---|
| 35 | const floors = await adminQueries.getFloorsByBuildingId(building_id);
|
|---|
| 36 |
|
|---|
| 37 | res.render('admin/select-floor', {
|
|---|
| 38 | floors,
|
|---|
| 39 | building,
|
|---|
| 40 | admin_id: admin_id || building.admin_id
|
|---|
| 41 | });
|
|---|
| 42 | } catch (error) {
|
|---|
| 43 | console.error('Error fetching floors:', error);
|
|---|
| 44 | res.status(500).send('Database error');
|
|---|
| 45 | }
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | exports.createUnitForm = async (req, res) => {
|
|---|
| 49 | try {
|
|---|
| 50 | const { floor_id, admin_id } = req.query;
|
|---|
| 51 |
|
|---|
| 52 | const floor = await adminQueries.getFloorById(floor_id);
|
|---|
| 53 | const building = await adminQueries.getBuildingById(floor.building_id);
|
|---|
| 54 |
|
|---|
| 55 | res.render('admin/create-unit', {
|
|---|
| 56 | building,
|
|---|
| 57 | floor,
|
|---|
| 58 | admin_id: admin_id || building.admin_id
|
|---|
| 59 | });
|
|---|
| 60 | } catch (error) {
|
|---|
| 61 | console.error('Error loading form:', error);
|
|---|
| 62 | res.status(500).send('Database error');
|
|---|
| 63 | }
|
|---|
| 64 | };
|
|---|
| 65 |
|
|---|
| 66 | exports.createUnitSubmit = async (req, res) => {
|
|---|
| 67 | try {
|
|---|
| 68 | const { floor_id, unit_number, room_number, floor_area, status, price } = req.body;
|
|---|
| 69 |
|
|---|
| 70 | const isValid = await adminQueries.validateUnitNumber(floor_id, unit_number);
|
|---|
| 71 |
|
|---|
| 72 | if (!isValid) {
|
|---|
| 73 | return res.status(400).send(`Unit ${unit_number} already exists on this floor`);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | const unitData = {
|
|---|
| 77 | unit_number,
|
|---|
| 78 | room_number: parseInt(room_number),
|
|---|
| 79 | floor_area: parseFloat(floor_area),
|
|---|
| 80 | status,
|
|---|
| 81 | price: parseFloat(price),
|
|---|
| 82 | image: req.files?.image?.[0]?.filename || 'default_unit.jpg',
|
|---|
| 83 | floorplan: req.files?.floorplan?.[0]?.filename || 'default_plan.pdf',
|
|---|
| 84 | vector_image: req.files?.vector_image?.[0]?.filename || 'default_vector.svg',
|
|---|
| 85 | };
|
|---|
| 86 |
|
|---|
| 87 | const newUnit = await adminQueries.insertUnit(unitData, floor_id);
|
|---|
| 88 |
|
|---|
| 89 | const floor = await adminQueries.getFloorById(floor_id);
|
|---|
| 90 | const building = await adminQueries.getBuildingById(floor.building_id);
|
|---|
| 91 |
|
|---|
| 92 | res.render('admin/unit-created', {
|
|---|
| 93 | unit: newUnit,
|
|---|
| 94 | building,
|
|---|
| 95 | floor
|
|---|
| 96 | });
|
|---|
| 97 | } catch (error) {
|
|---|
| 98 | console.error('Insert unit failed:', error.message);
|
|---|
| 99 | res.status(500).send('Unit could not be created. Please try again.');
|
|---|
| 100 | }
|
|---|
| 101 | }; |
|---|