Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved issue #14 search garages by name #39

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 62 additions & 46 deletions controllers/garage.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,51 +109,51 @@ const renderfindgarage = async (req, res) => {
//TODO: IN PROGRESS
//rendergaragebyIp... renders the garages in progress
const rendergaragebyip = async (req, res) => {
var config = {
method: 'get',
url: 'http://ip-api.com/json/',
};
axios(config)
.then(async function(response) {
const geoData = await geocoder.forwardGeocode({
query: response.data.city,
limit: 1,
})
.send();
var geometry = geoData.body.features[0].geometry;
var garages = await garageService.AllGarages();
var min_distance = 10000000.0;
var dist = {};
for (let garage of garages) {
// getting the distance between the garage and the user
var distance = garageService.DistanceCal(
response.data.lat,
response.data.lon,
garage.geometry.coordinates[1],
garage.geometry.coordinates[0]
);
if (distance <= min_distance) {
dist = garage;
min_distance = distance;
}
}
if (min_distance > 1000.0) {
req.flash(
'err',
'Sorry! No garages found within 1000.0 km radius.'
);
res.redirect('/garage/find');
} else {
res.render('garages/foundgarage', {
body: req.body,
by: 'IP',
geometry: geometry,
maptoken: mapBoxToken,
garage: dist,
min_distance: min_distance,
});
var config = {
method: 'get',
url: 'http://ip-api.com/json/',
};
axios(config).then(async function (response) {
const geoData = await geocoder
.forwardGeocode({
query: response.data.city,
limit: 1,
})
.send();
var geometry = geoData.body.features[0].geometry;
var garages = await garageService.AllGarages();
var min_distance = 10000000.0;
var dist = {};
for (let garage of garages) {
// getting the distance between the garage and the user
var distance = garageService.DistanceCal(
response.data.lat,
response.data.lon,
garage.geometry.coordinates[1],
garage.geometry.coordinates[0]
);
if (distance <= min_distance) {
dist = garage;
min_distance = distance;
}
})
}
if (min_distance > 1000.0) {
req.flash(
'err',
'Sorry! No garages found within 1000.0 km radius.'
);
res.redirect('/garage/find');
} else {
res.render('garages/foundgarage', {
body: req.body,
by: 'IP',
geometry: geometry,
maptoken: mapBoxToken,
garage: dist,
min_distance: min_distance,
});
}
});
};

//rendergaragebyloc... renders the garages in progress
Expand All @@ -170,7 +170,7 @@ const rendergaragebyloc = async (req, res) => {
limit: 1,
})
.send();
var geometry = geoData.body.features[0].geometry;;
var geometry = geoData.body.features[0].geometry;
var garages = await garageService.AllGarages();
var min_distance = 10000000.0;
var dist = {};
Expand Down Expand Up @@ -210,6 +210,21 @@ const rendergaragebyloc = async (req, res) => {
}
};

const renderSearchGarage = async (req, res) => {
var name = req.body.name;
const garages = await garageService.FindSearchByNameGarage(name);
if (!garages) {
req.flash('err', 'Error :Garage Not Found!');
res.redirect('/garage/');
} else {
res.render('garages/searchgarage', {
garages: garages,
maptoken: mapBoxToken,
name:name
});
}
};

module.exports = {
renderAddGarage,
addGarage,
Expand All @@ -220,4 +235,5 @@ module.exports = {
renderfindgarage,
rendergaragebyip,
rendergaragebyloc,
};
renderSearchGarage,
};
Loading