| 1 | const { clientQueries, adminQueries } = require('../models/queries');
|
|---|
| 2 |
|
|---|
| 3 | exports.listBuildings = async (req, res) => {
|
|---|
| 4 | try {
|
|---|
| 5 | const buildings = await clientQueries.getAllBuildings();
|
|---|
| 6 | res.render('client/buildings', { buildings });
|
|---|
| 7 | } catch (error) {
|
|---|
| 8 | console.error('Error fetching buildings:', error);
|
|---|
| 9 | res.status(500).send('Database error');
|
|---|
| 10 | }
|
|---|
| 11 | };
|
|---|
| 12 |
|
|---|
| 13 | exports.buildingDetail = async (req, res) => {
|
|---|
| 14 | try {
|
|---|
| 15 | const { building_id } = req.params;
|
|---|
| 16 | const data = await clientQueries.getBuildingDetailById(building_id);
|
|---|
| 17 |
|
|---|
| 18 | if (data.length === 0) {
|
|---|
| 19 | return res.status(404).send('Building not found');
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | // Group data
|
|---|
| 23 | const buildingInfo = {
|
|---|
| 24 | id: data[0].building_id,
|
|---|
| 25 | name: data[0].building_name,
|
|---|
| 26 | address: data[0].address,
|
|---|
| 27 | description: data[0].description,
|
|---|
| 28 | architect: data[0].architect_name,
|
|---|
| 29 | };
|
|---|
| 30 |
|
|---|
| 31 | const floors = data.map(row => ({
|
|---|
| 32 | floor_id: row.floor_id,
|
|---|
| 33 | floor_number: row.floor_number,
|
|---|
| 34 | layout_image: row.layout_image,
|
|---|
| 35 | }));
|
|---|
| 36 |
|
|---|
| 37 | res.render('client/building-detail', { building: buildingInfo, floors });
|
|---|
| 38 | } catch (error) {
|
|---|
| 39 | console.error('buildingDetail error:', error.message);
|
|---|
| 40 | return res.status(500).send('Could not load building information');
|
|---|
| 41 | }
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | exports.floorUnits = async (req, res) => {
|
|---|
| 45 | try {
|
|---|
| 46 | const { floor_id } = req.params;
|
|---|
| 47 | const units = await clientQueries.getUnitsByFloorId(floor_id);
|
|---|
| 48 |
|
|---|
| 49 | const floor = await adminQueries.getFloorById(floor_id);
|
|---|
| 50 | const building = await adminQueries.getBuildingById(floor.building_id);
|
|---|
| 51 |
|
|---|
| 52 | res.render('client/floor-units', {
|
|---|
| 53 | units,
|
|---|
| 54 | building,
|
|---|
| 55 | floor
|
|---|
| 56 | });
|
|---|
| 57 | } catch (error) {
|
|---|
| 58 | console.error('Error fetching units:', error);
|
|---|
| 59 | res.status(500).send('Database error');
|
|---|
| 60 | }
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | exports.unitDetail = async (req, res) => {
|
|---|
| 64 | try {
|
|---|
| 65 | const { unit_id } = req.params;
|
|---|
| 66 | const unitData = await clientQueries.getUnitById(unit_id);
|
|---|
| 67 |
|
|---|
| 68 | if (!unitData) {
|
|---|
| 69 | return res.status(404).send('Unit not found');
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | res.render('client/unit-detail', { unit: unitData });
|
|---|
| 73 | } catch (error) {
|
|---|
| 74 | console.error('unitDetail problem:', error.message);
|
|---|
| 75 | return res.status(404).send('Unit not available');
|
|---|
| 76 | }
|
|---|
| 77 | }; |
|---|