Files
old-web/app/controllers/realEstateTypes.js
2019-09-27 18:20:16 +02:00

47 lines
1.3 KiB
JavaScript

const { currentSearchRequest } = require("../helpers/url");
const { createSearchRequest } = require("../helpers/db/searchRequest");
const { AD_CATEGORY } = require("../common/enums");
const getRealEstateTypes = (req, res) => {
const title = "Koju nekretninu tražite?";
const realEstateTypes = Object.keys(AD_CATEGORY).map(
category => AD_CATEGORY[category]
);
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
};