90 lines
3.1 KiB
JavaScript
90 lines
3.1 KiB
JavaScript
const router = require("express").Router();
|
|
const _ = require('lodash')
|
|
const getSearchPresets = require("../models/getSearchPresets.js");
|
|
const base64 = require("../util/base64.js")
|
|
const sas = require("../util/scoreAndSearch.js");
|
|
const { filter } = require("lodash");
|
|
|
|
|
|
module.exports = dbConn => {
|
|
router.get("/api/v1/search", searchHandler(dbConn));
|
|
router.get("/api/v1/search/presets", presetHandler(dbConn));
|
|
|
|
return router;
|
|
};
|
|
|
|
function presetHandler(dbConn) {
|
|
return function (req, res) {
|
|
getSearchPresets(dbConn).then(presets => {
|
|
res.json(presets)
|
|
}).catch(error => {
|
|
// TODO error handling
|
|
})
|
|
}
|
|
}
|
|
|
|
function searchHandler(dbConn) {
|
|
const scoreAndSearch = sas(dbConn)
|
|
return function (req, res) {
|
|
let response = {}
|
|
|
|
response.meta = {
|
|
params: req.params,
|
|
query: req.query,
|
|
headers: req.headers
|
|
}
|
|
|
|
let q = req.query.q ? base64.base64ToObj(req.query.q) : req.query
|
|
console.log('Q:', q)
|
|
|
|
let scoreQueryObj = {}
|
|
if (q.temperature) scoreQueryObj['temperature_mean_max'] = q.temperature
|
|
if (q.temperature_mean_max) scoreQueryObj['temperature_mean_max'] = q.temperature_mean_max
|
|
if (q.precipitation) scoreQueryObj['precipitation'] = q.precipitation
|
|
if (q.rain_days) scoreQueryObj['rain_days'] = q.rain_days
|
|
if (q.sun_hours) scoreQueryObj['sun_hours'] = q.sun_hours
|
|
if (q.accommodation_costs) scoreQueryObj['accommodation_costs'] = q.accommodation_costs
|
|
if (q.food_costs) scoreQueryObj['food_costs'] = q.food_costs
|
|
if (q.alcohol_costs) scoreQueryObj['alcohol_costs'] = q.alcohol_costs
|
|
if (q.water_costs) scoreQueryObj['water_costs'] = q.water_costs
|
|
if (q.public_transportation_costs) scoreQueryObj['public_transportation_costs'] = q.public_transportation_costs
|
|
if (q.entertainment_costs) scoreQueryObj['entertainment_costs'] = q.entertainment_costs
|
|
if (q.average_per_day_costs) scoreQueryObj['average_per_day_costs'] = q.average_per_day_costs
|
|
|
|
//console.log(scoreQueryObj)
|
|
|
|
if (_.isEmpty(scoreQueryObj)) {
|
|
res.status(400).send('provide at least one search parameter.');
|
|
}
|
|
scoreAndSearch(q.from, q.to, scoreQueryObj).then(searchResults => {
|
|
|
|
//response.data = searchResults
|
|
// FILTER if query contains filterString
|
|
if (q.textfilter) {
|
|
response = filterByString(searchResults, q.textfilter, q.fulltext)
|
|
} else {
|
|
response = searchResults
|
|
}
|
|
res.json(response)
|
|
}).catch(e => {
|
|
// TODO error handling
|
|
console.log(e)
|
|
res.json(e.message)
|
|
})
|
|
}
|
|
}
|
|
|
|
function filterByString(searchResults, filterString, boolFulltext) {
|
|
return _.filter(searchResults, region => {
|
|
console.log("filtering: filterString, boolFulltext");
|
|
console.log(filterString, boolFulltext);
|
|
filterString = filterString.toLowerCase()
|
|
let name = region.name.toLowerCase()
|
|
let country = region.country.toLowerCase()
|
|
if (boolFulltext) {
|
|
let desc = region.description.toLowerCase()
|
|
return name.includes(filterString) || country.includes(filterString) || desc.includes(filterString)
|
|
}
|
|
return name.includes(filterString) || country.includes(filterString)
|
|
})
|
|
} |