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

Commit

Permalink
ref(#35): change restaurant structure
Browse files Browse the repository at this point in the history
  • Loading branch information
floriaaan committed Jan 10, 2024
1 parent 7164b99 commit daeed05
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 61 deletions.
29 changes: 21 additions & 8 deletions services/proto/restaurant.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,36 @@ message Restaurant {
string name = 2;
optional string description = 3;

repeated float location = 4;
optional string address = 5;
Address address = 4;

repeated string openingHours = 6;
optional string phone = 7;
repeated string openingHours = 5;
optional string phone = 6;

repeated string userIds = 10;
repeated string userIds = 7;

string createdAt = 8;
string updatedAt = 9;
}

message Address {
string id = 1;

float lat = 2;
float lng = 3;

optional string street = 4;
optional string city = 5;
optional string zipcode = 6;
optional string country = 7;

string restaurantId = 8;
}

message RestaurantCreateInput{
string name = 1;
optional string description = 2;

repeated float location = 3;
optional string address = 4;
Address address = 4;

repeated string openingHours = 5;
optional string phone = 6;
Expand All @@ -39,7 +51,8 @@ message RestaurantList {
}

message ByLocationInput {
repeated float location = 1;
float lat = 1;
float lng = 2;
}

message RestaurantId {
Expand Down
4 changes: 2 additions & 2 deletions services/restaurant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
},
"devDependencies": {
"@types/amqplib": "^0.10.1",
"prisma": "^5.1.1",
"prisma": "^5.8.0",
"typescript": "^5.1.6"
},
"dependencies": {
"@grpc/grpc-js": "^1.8.18",
"@grpc/proto-loader": "^0.7.8",
"@prisma/client": "5.1.1",
"@prisma/client": "5.8.0",
"amqplib": "^0.10.3",
"dotenv": "^16.3.1",
"esbuild": "^0.18.15",
Expand Down
50 changes: 34 additions & 16 deletions services/restaurant/pnpm-lock.yaml

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Warnings:
- You are about to drop the column `address` on the `Restaurant` table. All the data in the column will be lost.
- Added the required column `addressId` to the `Restaurant` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Restaurant" DROP COLUMN "address",
ADD COLUMN "addressId" TEXT NOT NULL;

-- CreateTable
CREATE TABLE "Address" (
"id" TEXT NOT NULL,
"lat" DOUBLE PRECISION NOT NULL,
"lng" DOUBLE PRECISION NOT NULL,
"street" TEXT,
"city" TEXT,
"zipcode" TEXT,
"country" TEXT,
"restaurantId" TEXT NOT NULL,

CONSTRAINT "Address_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "Address_restaurantId_key" ON "Address"("restaurantId");

-- AddForeignKey
ALTER TABLE "Address" ADD CONSTRAINT "Address_restaurantId_fkey" FOREIGN KEY ("restaurantId") REFERENCES "Restaurant"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:
- A unique constraint covering the columns `[addressId]` on the table `Restaurant` will be added. If there are existing duplicate values, this will fail.
*/
-- AlterTable
ALTER TABLE "Restaurant" ALTER COLUMN "addressId" DROP NOT NULL;

-- CreateIndex
CREATE UNIQUE INDEX "Restaurant_addressId_key" ON "Restaurant"("addressId");
20 changes: 18 additions & 2 deletions services/restaurant/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ model Restaurant {
name String
description String?
location Float[]
address String?
location Float[]
address Address?
addressId String? @unique
openingHours String[]
Expand All @@ -24,3 +25,18 @@ model Restaurant {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model Address {
id String @id @default(cuid())
lat Float
lng Float
street String?
city String?
zipcode String?
country String?
restaurant Restaurant @relation(fields: [restaurantId], references: [id])
restaurantId String @unique
}
15 changes: 4 additions & 11 deletions services/restaurant/src/handler/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,19 @@ export const CreateRestaurant = async (
callback: (err: any, response: Restaurant | null) => void
) => {
try {
const {
location,
name,
openingHours,
address,
description,
phone,
userIds,
} = request;
const { name, openingHours, address, description, phone, userIds } =
request;

const restaurant = (await prisma.restaurant.create({
data: {
location,
name,
openingHours,
address,
address: { create: address },
description,
phone,
userIds,
},
include: { address: true },
})) as unknown as Restaurant;

callback(null, restaurant);
Expand Down
1 change: 1 addition & 0 deletions services/restaurant/src/handler/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const GetRestaurant = async (

const restaurant = (await prisma.restaurant.findFirstOrThrow({
where: { id },
include: { address: true },
})) as unknown as Restaurant;

callback(null, restaurant);
Expand Down
5 changes: 3 additions & 2 deletions services/restaurant/src/handler/list-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export const GetRestaurants = async (
callback: (err: any, response: RestaurantList | null) => void
) => {
try {
const restaurants =
(await prisma.restaurant.findMany()) as unknown as Restaurant[];
const restaurants = (await prisma.restaurant.findMany({
include: { address: true },
})) as unknown as Restaurant[];

callback(null, { restaurants });
} catch (error) {
Expand Down
15 changes: 10 additions & 5 deletions services/restaurant/src/handler/list-by-location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Restaurant,
RestaurantList,
} from "@restaurant/types/restaurant";
import { getDistance } from 'geolib';
import { getDistance } from "geolib";

interface RestaurantWithDistance extends Restaurant {
distance: number;
Expand All @@ -19,7 +19,10 @@ export const sortRestaurantsByDistance = (
return restaurants
.map((restaurant) => ({
...restaurant,
distance: getDistance(location, restaurant.location),
distance: getDistance(location, [
restaurant.address.lat,
restaurant.address.lng,
]),
}))
.sort((a, b) => a.distance - b.distance);
};
Expand All @@ -29,11 +32,13 @@ export const GetRestaurantsByLocation = async (
callback: (err: any, response: RestaurantList | null) => void
) => {
try {
const { location } = request;
const { lat, lng } = request;

const all = (await prisma.restaurant.findMany()) as unknown as Restaurant[];
const all = (await prisma.restaurant.findMany({
include: { address: true },
})) as unknown as Restaurant[];

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

Expand Down
16 changes: 4 additions & 12 deletions services/restaurant/src/handler/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,20 @@ export const UpdateRestaurant = async (
callback: (err: any, response: Restaurant | null) => void
) => {
try {
const {
id,
location,
name,
openingHours,
address,
description,
phone,
userIds,
} = request;
const { id, name, openingHours, address, description, phone, userIds } =
request;

const restaurant = (await prisma.restaurant.update({
where: { id },
data: {
location,
name,
openingHours,
address,
address: { update: address },
description,
phone,
userIds,
},
include: { address: true },
})) as unknown as Restaurant;

callback(null, restaurant);
Expand Down
20 changes: 17 additions & 3 deletions services/restaurant/src/types/restaurant.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ export type Restaurant = {
name: string;
description?: string;

location: [number, number];
address?: string;
address: Address;

openingHours: string[];
phone?: string;
Expand All @@ -16,6 +15,20 @@ export type Restaurant = {
updatedAt: Date | string;
};

export type Address = {
id: string;

lat: number;
lng: number;

street?: string;
city?: string;
zipcode?: string;
country?: string;

restaurantId: string;
};

export type RestaurantCreateInput = Omit<
Restaurant,
"id" | "createdAt" | "updatedAt"
Expand All @@ -26,7 +39,8 @@ export type RestaurantId = {
};

export type ByLocationInput = {
location: Restaurant["location"];
lat: number;
lng: number;
};

export type RestaurantList = {
Expand Down

0 comments on commit daeed05

Please sign in to comment.