Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

Commit

Permalink
feat(location): implement list by location
Browse files Browse the repository at this point in the history
  • Loading branch information
floriaaan committed Aug 19, 2023
1 parent 20f4e3a commit f69b814
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions services/restaurant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"amqplib": "^0.10.3",
"dotenv": "^16.3.1",
"esbuild": "^0.18.15",
"geolib": "^3.3.4",
"nodemon": "^3.0.1",
"path": "^0.12.7",
"ts-node": "^10.9.1",
Expand Down
7 changes: 7 additions & 0 deletions services/restaurant/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 22 additions & 7 deletions services/restaurant/src/handler/list-by-location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ import {
Restaurant,
RestaurantList,
} from "@restaurant/types/restaurant";
import geolib from "geolib";

interface RestaurantWithDistance extends Restaurant {
distance: number;
}

export const sortRestaurantsByDistance = (
restaurants: Restaurant[],
location: [number, number]
): RestaurantWithDistance[] => {
return restaurants
.map((restaurant) => ({
...restaurant,
distance: geolib.getDistance(location, restaurant.location),
}))
.sort((a, b) => a.distance - b.distance);
};

export const GetRestaurantsByLocation = async (
{ request }: Data<ByLocationInput>,
Expand All @@ -14,13 +31,11 @@ export const GetRestaurantsByLocation = async (
try {
const { location } = request;

const restaurants = (await prisma.restaurant.findMany({
where: {
location: {
equals: location,
},
},
})) as unknown as Restaurant[];
const all = (await prisma.restaurant.findMany()) as unknown as Restaurant[];

const restaurants = sortRestaurantsByDistance(all, location)
.map(({ distance, ...restaurant }) => restaurant as Restaurant)
.filter((_, i) => i < 10);

callback(null, { restaurants });
} catch (error) {
Expand Down

0 comments on commit f69b814

Please sign in to comment.