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 1ddf973 commit ac59c72
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions routes/postsRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,38 @@ router.get("/posts", async (req, res) => {

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

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

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

const pickupTimeUnix = moment(post.pickupTime).unix();
const createdAtUnix = moment(post.createdAt).unix();
const updateAtUnix = moment(post.updatedAt).unix();
const pickupTimeUnix = moment(post.pickupTime).unix();
const createdAtUnix = moment(post.createdAt).unix();
const updateAtUnix = moment(post.updatedAt).unix();

const postResponses = {
...post.dataValues,
pickupTime: pickupTimeUnix,
createdAt: createdAtUnix,
updatedAt: updateAtUnix,
distance,
};
const postResponses = {
...post.dataValues,
pickupTime: pickupTimeUnix,
createdAt: createdAtUnix,
updatedAt: updateAtUnix,
distance,
};

return postResponses;
});
return postResponses;
})
.filter((post) => {
// Filter posts based on the specified radius
return (
!radius || (post.distance && post.distance <= parseFloat(radius))
);
});

let sortPost;

Expand Down Expand Up @@ -340,7 +347,7 @@ router.get("/recommendation/rawIngredients", async (req, res) => {
};
const rawIngredientsPosts = await Post.findAll({
where: {
categoryId: 3,
categoryId: 3,
},
});
const withDistance = rawIngredientsPosts.map((post) => {
Expand All @@ -354,7 +361,6 @@ router.get("/recommendation/rawIngredients", async (req, res) => {
const createdAtUnix = moment(post.createdAt).unix();
const updateAtUnix = moment(post.updatedAt).unix();


const responseRawIngredientsPost = {
...post.dataValues,
pickupTime: pickupTimeUnix,
Expand All @@ -366,7 +372,6 @@ router.get("/recommendation/rawIngredients", async (req, res) => {
return responseRawIngredientsPost;
});


const sort = withDistance.sort((a, b) => a.distance - b.distance);

const rawIngredientsPost = sort.slice(0, 5);
Expand Down

0 comments on commit ac59c72

Please sign in to comment.