Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
taugk committed Dec 16, 2023
1 parent 0c6a27b commit 23b2e09
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions routes/postsRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ router.get("/posts", async (req, res) => {
} = req.query;
const offset = (page - 1) * limit;

if (!lat || !lon) {
return res
.status(400)
.json({ message: "Missing latitude or longitude parameters" });
const userLocation = {};

if (lat) {
userLocation.latitude = parseFloat(lat);
}

if (lon) {
userLocation.longitude = parseFloat(lon);
}
const userLocation = {
latitude: parseFloat(lat),
longitude: parseFloat(lon),
};

let whereCondition = {};

Expand All @@ -54,13 +54,19 @@ router.get("/posts", async (req, res) => {
if (price) {
whereCondition.price = { [Op.lte]: parseFloat(price) };
}

const posts = await Post.findAll({ where: whereCondition });

const WithDistanceAndDistance = posts.map((post) => {
const locationPosts = {
latitude: parseFloat(post.lat),
longitude: parseFloat(post.lon),
};
const distance = geolib.distanceGet(userLocation, locationPosts, 1);

const distance =
userLocation.latitude && userLocation.longitude
? geolib.getDistance(userLocation, locationPosts)
: null;

const pickupTimeUnix = moment(post.pickupTime).unix();
const createdAtUnix = moment(post.createdAt).unix();
Expand All @@ -78,7 +84,7 @@ router.get("/posts", async (req, res) => {
});

let sortPost;
if (search || category || radius || price) {
if (search || category || radius || price || (lat && lon)) {
sortPost = WithDistanceAndDistance;
} else {
sortPost = WithDistanceAndDistance.sort(
Expand Down

0 comments on commit 23b2e09

Please sign in to comment.