2019-09-13 12:37:53 +02:00
|
|
|
const { currentSearchRequest } = require("../helpers/url");
|
2019-05-17 10:49:13 +02:00
|
|
|
|
2019-09-10 07:43:18 +02:00
|
|
|
const getLocation = async (req, res) => {
|
2019-10-18 03:29:15 -07:00
|
|
|
const title = "Odaberite lokaciju";
|
2019-09-05 11:14:54 +02:00
|
|
|
const nextStep = req.query.nextStep || "/";
|
|
|
|
|
|
2019-12-20 01:02:57 +01:00
|
|
|
//Check if location data already exists (active request)
|
2019-12-19 02:12:23 +01:00
|
|
|
//If it does then get location is called through edit field query
|
|
|
|
|
//and map should show already selected location not initial map
|
2019-12-20 01:02:57 +01:00
|
|
|
let selectedLatLngBounds = {};
|
|
|
|
|
let boundsSelected = false;
|
2019-12-19 02:12:23 +01:00
|
|
|
|
|
|
|
|
const searchRequest = await currentSearchRequest(req);
|
|
|
|
|
|
|
|
|
|
if (!searchRequest || !searchRequest.dataValues) {
|
|
|
|
|
res.render("notFound", { title: " " });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const selectedArea = searchRequest.areaToSearch;
|
|
|
|
|
const sw = selectedArea.coordinates[0][3];
|
|
|
|
|
const ne = selectedArea.coordinates[0][1];
|
2019-12-20 01:02:57 +01:00
|
|
|
|
2019-12-19 02:12:23 +01:00
|
|
|
if (sw[0] && ne[0]) {
|
2019-12-20 01:02:57 +01:00
|
|
|
selectedLatLngBounds = {
|
|
|
|
|
swLat: sw[1],
|
|
|
|
|
swLng: sw[0],
|
|
|
|
|
neLat: ne[1],
|
|
|
|
|
neLng: ne[0]
|
|
|
|
|
};
|
|
|
|
|
boundsSelected = true;
|
2019-12-19 02:12:23 +01:00
|
|
|
}
|
|
|
|
|
|
2019-09-10 07:43:18 +02:00
|
|
|
res.render("location", {
|
2019-09-05 11:14:54 +02:00
|
|
|
nextStep,
|
2019-12-19 02:12:23 +01:00
|
|
|
title,
|
2019-12-20 01:02:57 +01:00
|
|
|
boundsSelected,
|
2019-12-19 02:12:23 +01:00
|
|
|
selectedLatLngBounds
|
2019-09-05 11:14:54 +02:00
|
|
|
});
|
2019-05-17 10:49:13 +02:00
|
|
|
};
|
|
|
|
|
|
2019-09-10 07:43:18 +02:00
|
|
|
const postLocation = async (req, res) => {
|
2019-09-13 12:37:53 +02:00
|
|
|
let searchRequest = await currentSearchRequest(req);
|
2019-09-10 07:43:18 +02:00
|
|
|
|
2019-10-15 18:51:19 +02:00
|
|
|
if (!searchRequest || !searchRequest.dataValues) {
|
|
|
|
|
res.render("notFound", { title: " " });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 11:14:54 +02:00
|
|
|
const northWest = [req.body.west, req.body.north];
|
|
|
|
|
const northEast = [req.body.east, req.body.north];
|
|
|
|
|
const southEast = [req.body.east, req.body.south];
|
|
|
|
|
const southWest = [req.body.west, req.body.south];
|
|
|
|
|
|
2019-09-13 12:37:53 +02:00
|
|
|
const locationInputValue =
|
2019-09-10 07:43:18 +02:00
|
|
|
req.body.locationInput && req.body.locationInput.length > 0
|
|
|
|
|
? req.body.locationInput
|
|
|
|
|
: null;
|
|
|
|
|
|
2019-09-13 12:37:53 +02:00
|
|
|
searchRequest.areaToSearch = {
|
2019-09-05 11:14:54 +02:00
|
|
|
type: "Polygon",
|
2019-09-13 12:37:53 +02:00
|
|
|
coordinates: [[northWest, northEast, southEast, southWest, northWest]],
|
|
|
|
|
crs: { type: "name", properties: { name: "EPSG:4326" } }
|
2019-09-05 11:14:54 +02:00
|
|
|
};
|
2019-09-10 07:43:18 +02:00
|
|
|
|
|
|
|
|
let locationInputData;
|
|
|
|
|
if (req.body.locationInputData) {
|
|
|
|
|
try {
|
|
|
|
|
locationInputData = JSON.parse(req.body.locationInputData);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
locationInputData = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 12:37:53 +02:00
|
|
|
await searchRequest.save();
|
2019-09-05 11:14:54 +02:00
|
|
|
|
2019-10-11 15:37:47 +02:00
|
|
|
const nextStepPage = req.query.nextStep || "filteri";
|
2019-09-13 12:37:53 +02:00
|
|
|
const nextStepUrl = `/${nextStepPage}/${searchRequest.id}`;
|
2019-09-05 11:14:54 +02:00
|
|
|
|
|
|
|
|
res.redirect(nextStepUrl);
|
2019-05-17 10:49:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2019-09-10 07:43:18 +02:00
|
|
|
getLocation,
|
|
|
|
|
postLocation
|
2019-09-05 11:14:54 +02:00
|
|
|
};
|