Skip to content

Commit

Permalink
route filter home
Browse files Browse the repository at this point in the history
  • Loading branch information
CorvusSharp committed Dec 9, 2024
1 parent bee6b9b commit 2ce39f8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
64 changes: 64 additions & 0 deletions citywalls2/controllers/streetController.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,67 @@ exports.houseDetails = asyncHandler(async (req, res) => {
});

});

exports.filterPage = asyncHandler(async (req, res) => {
const districtsCursor = await db.query('FOR house IN houses RETURN DISTINCT house.district');
const districts = await districtsCursor.all();

res.render('houses_filter', {
title: 'Дома Санкт-Петербурга',
districts,
});
});

exports.filteredHouses = asyncHandler(async (req, res) => {
const { year, district, floors, apartments, condition, management_company, street } = req.query;

let query = 'FOR house IN houses';
const bindVars = {};

if (year) {
const [minYear, maxYear] = year.split('-').map(Number);
query += ' FILTER house.construction_year >= @minYear AND house.construction_year <= @maxYear';
bindVars.minYear = minYear;
bindVars.maxYear = maxYear;
}

if (district) {
query += ' FILTER house.district == @district';
bindVars.district = district;
}

if (floors) {
query += ' FILTER house.floors == @floors';
bindVars.floors = Number(floors);
}

if (apartments) {
query += ' FILTER house.apartments == @apartments';
bindVars.apartments = Number(apartments);
}

if (condition) {
query += ' FILTER house.condition == @condition';
bindVars.condition = condition;
}

if (management_company) {
query += ' FILTER house.management_company == @management_company';
bindVars.management_company = management_company;
}

if (street) {
query += ' FILTER house.street == @street';
bindVars.street = street;
}

query += ' RETURN house';

const cursor = await db.query(query, bindVars);
const houses = await cursor.all();

res.render('houses_list', {
title: 'Результаты фильтрации домов',
houses,
});
});
3 changes: 3 additions & 0 deletions citywalls2/routes/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ router.get('/main/streets/:streetName/houses', street_controller.housesByStreet)
// Маршрут для страницы дома
router.get('/main/streets/:streetName/houses/:houseId', street_controller.houseDetails);

router.get('/main/houses', street_controller.filterPage);
router.get('/main/houses/filter', street_controller.filteredHouses);

module.exports = router;


Expand Down

0 comments on commit 2ce39f8

Please sign in to comment.