diff --git a/app/controllers/realEstateTypes.js b/app/controllers/realEstateTypes.js index 853b693..c02d2b9 100644 --- a/app/controllers/realEstateTypes.js +++ b/app/controllers/realEstateTypes.js @@ -1,6 +1,6 @@ -const db = require("../models/index"); +const { currentSearchRequest } = require("../helpers/url"); +const { createSearchRequest } = require("../helpers/db/searchRequest"); -const { currentRERequest } = require("../helpers/url"); const { realEstateTypes, getRealEstateTypeEnum } = require("../helpers/enums"); const getRealEstateTypes = (req, res) => { @@ -9,31 +9,35 @@ const getRealEstateTypes = (req, res) => { }; const postRealEstateTypes = async (req, res) => { - const request = await currentRERequest(req); + 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"; - if (request && request.uniqueId) { - const nextStepUrl = `/${nextStepPage}/${request.uniqueId}`; - request.realEstateType = req.body.realestatetype; - if (!getRealEstateTypeEnum(request.realEstateType).hasGardenSize) { - request.gardenSize = null; + let nextStepUrl = ""; + if (searchRequest && searchRequest.id) { + nextStepUrl = `/${nextStepPage}/${searchRequest.id}`; + searchRequest.realEstateType = selectedRealEstateType; + if (!getRealEstateTypeEnum(selectedRealEstateType).hasGardenSize) { + searchRequest.gardenSizeMin = null; + searchRequest.gardenSizeMax = null; } await request.save(); - - res.redirect(nextStepUrl); } else { - db.RealEstateRequest.create({ - realEstateType: req.body.realestatetype - }) - .then(result => { - const nextStepUrl = `/${nextStepPage}/${result.uniqueId}`; - res.redirect(nextStepUrl); - }) - .catch(e => { - res.send(e); + try { + const newSearchRequest = await createSearchRequest({ + realEstateType: selectedRealEstateType }); + + nextStepUrl = `/${nextStepPage}/${newSearchRequest.id}`; + } catch (error) { + console.log(error); + nextStepUrl = `/`; + } } + res.redirect(nextStepUrl); }; module.exports = { diff --git a/app/routes/index.js b/app/routes/index.js index 9872951..bba7973 100644 --- a/app/routes/index.js +++ b/app/routes/index.js @@ -27,10 +27,10 @@ const { redirect } = require("../controllers/redirect"); const router = express.Router(); router.get("/", welcome); -router.get("/vrstanekretnine/:request_id", getRealEstateTypes); -router.get("/vrstanekretnine", getRealEstateTypes); -router.post("/vrstanekretnine/:request_id", postRealEstateTypes); +router.get("/vrstanekretnine/:searchRequestId", getRealEstateTypes); +router.get("/vrstanekretnine", getRealEstateTypes); +router.post("/vrstanekretnine/:searchRequestId", postRealEstateTypes); router.post("/vrstanekretnine", postRealEstateTypes); router.get("/lokacija/:request_id", getLocation); diff --git a/app/views/realEstateType.ejs b/app/views/realEstateType.ejs index eac9357..cf14b03 100644 --- a/app/views/realEstateType.ejs +++ b/app/views/realEstateType.ejs @@ -1,5 +1,5 @@ -<% include partials/navBar %> +<% include partials/navBar %>
@@ -14,13 +14,13 @@ <% } %> - +