44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
const { currentSearchRequest } = require("../helpers/url");
|
|
const { createSearchRequest } = require("../helpers/db/searchRequest");
|
|
|
|
const { realEstateTypes, getRealEstateTypeEnum } = require("../helpers/enums");
|
|
|
|
const getRealEstateTypes = (req, res) => {
|
|
const title = "Koju nekretninu tražite?";
|
|
res.render("realEstateType", { realEstateTypes, title });
|
|
};
|
|
|
|
const postRealEstateTypes = async (req, res) => {
|
|
const searchRequest = await currentSearchRequest(req);
|
|
|
|
//TODO: check if selected real estate type is valid
|
|
const selectedRealEstateType = req.body.realEstateType || null;
|
|
|
|
const nextStepPage = req.query.nextStep || "lokacija";
|
|
|
|
let nextStepUrl = "";
|
|
if (searchRequest && searchRequest.id) {
|
|
nextStepUrl = `/${nextStepPage}/${searchRequest.id}`;
|
|
searchRequest.realEstateType = selectedRealEstateType;
|
|
|
|
await searchRequest.save();
|
|
} else {
|
|
try {
|
|
const newSearchRequest = await createSearchRequest({
|
|
realEstateType: selectedRealEstateType
|
|
});
|
|
|
|
nextStepUrl = `/${nextStepPage}/${newSearchRequest.id}`;
|
|
} catch (error) {
|
|
console.log(error);
|
|
nextStepUrl = `/`;
|
|
}
|
|
}
|
|
res.redirect(nextStepUrl);
|
|
};
|
|
|
|
module.exports = {
|
|
getRealEstateTypes,
|
|
postRealEstateTypes
|
|
};
|