From c67f2eb131fa0689a79e2006320a7ce92a9c5907 Mon Sep 17 00:00:00 2001 From: Florian LEROUX Date: Wed, 10 Jan 2024 12:17:20 +0100 Subject: [PATCH] ref(#35): change restaurant controller and update bruno requests --- requests/environments/local.bru | 3 + requests/restaurant/create restaurant.bru | 43 ++++++++++++ requests/restaurant/get restaurant by id.bru | 22 ++++++ requests/restaurant/list restaurants.bru | 22 ++++++ .../post restaurant by location.bru | 6 +- requests/restaurant/post restaurant.bru | 36 ---------- requests/restaurant/update restaurant.bru | 43 ++++++++++++ .../restaurant/restaurant.controller.ts | 67 +++++++++++++------ services/proto/restaurant.proto | 4 +- services/restaurant/src/handler/create.ts | 12 +++- services/restaurant/src/handler/update.ts | 12 +++- services/restaurant/src/types/restaurant.d.ts | 2 +- 12 files changed, 207 insertions(+), 65 deletions(-) create mode 100644 requests/restaurant/create restaurant.bru create mode 100644 requests/restaurant/get restaurant by id.bru create mode 100644 requests/restaurant/list restaurants.bru delete mode 100644 requests/restaurant/post restaurant.bru create mode 100644 requests/restaurant/update restaurant.bru diff --git a/requests/environments/local.bru b/requests/environments/local.bru index a6911cf6..616d4b20 100644 --- a/requests/environments/local.bru +++ b/requests/environments/local.bru @@ -1,3 +1,6 @@ vars { baseUrl: http://localhost:50000 } +vars:secret [ + token +] diff --git a/requests/restaurant/create restaurant.bru b/requests/restaurant/create restaurant.bru new file mode 100644 index 00000000..57e53ac1 --- /dev/null +++ b/requests/restaurant/create restaurant.bru @@ -0,0 +1,43 @@ +meta { + name: create restaurant + type: http + seq: 2 +} + +post { + url: {{baseUrl}}/api/restaurant + body: json + auth: bearer +} + +headers { + Content-Type: application/json +} + +auth:bearer { + token: {{token}} +} + +body:json { + { + "name": "restaurant-name", + "description": "restaurant-desc", + "address": { + "street": "restaurant-street", + "city": "restaurant-city", + "zipCode": "restaurant-postalCode", + "country": "restaurant-country", + "lat": 49.443, + "lng": 1.099 + }, + "openingHoursList": [ + "12h-14h", + "19h-22h" + ], + "phone": "restaurant-phone", + "userIds": [ + "user-id-1", + "user-id-2" + ] + } +} diff --git a/requests/restaurant/get restaurant by id.bru b/requests/restaurant/get restaurant by id.bru new file mode 100644 index 00000000..f6549237 --- /dev/null +++ b/requests/restaurant/get restaurant by id.bru @@ -0,0 +1,22 @@ +meta { + name: get restaurant by id + type: http + seq: 6 +} + +get { + url: {{baseUrl}}/api/restaurant/clr7ocdea00006tc1c5pc1gcn + body: none + auth: none +} + +headers { + Content-Type: application/json +} + +body:json { + { + "lat": 49.443, + "lng": 1.099 + } +} diff --git a/requests/restaurant/list restaurants.bru b/requests/restaurant/list restaurants.bru new file mode 100644 index 00000000..c153feb4 --- /dev/null +++ b/requests/restaurant/list restaurants.bru @@ -0,0 +1,22 @@ +meta { + name: list restaurants + type: http + seq: 5 +} + +get { + url: {{baseUrl}}/api/restaurant/ + body: none + auth: none +} + +headers { + Content-Type: application/json +} + +body:json { + { + "lat": 49.443, + "lng": 1.099 + } +} diff --git a/requests/restaurant/post restaurant by location.bru b/requests/restaurant/post restaurant by location.bru index 2b5dc888..e676a99d 100644 --- a/requests/restaurant/post restaurant by location.bru +++ b/requests/restaurant/post restaurant by location.bru @@ -16,9 +16,7 @@ headers { body:json { { - "locationList": [ - "", - "" - ] + "lat": 49.443, + "lng": 1.099 } } diff --git a/requests/restaurant/post restaurant.bru b/requests/restaurant/post restaurant.bru deleted file mode 100644 index ad13597a..00000000 --- a/requests/restaurant/post restaurant.bru +++ /dev/null @@ -1,36 +0,0 @@ -meta { - name: crate restaurant - type: http - seq: 2 -} - -post { - url: {{baseUrl}}/api/restaurant - body: json - auth: none -} - -headers { - Content-Type: application/json -} - -body:json { - { - "name": "", - "description": "", - "locationList": [ - "", - "" - ], - "address": "", - "openingHoursList": [ - "", - "" - ], - "phone": "", - "userIds": [ - "", - "" - ] - } -} diff --git a/requests/restaurant/update restaurant.bru b/requests/restaurant/update restaurant.bru new file mode 100644 index 00000000..1976365b --- /dev/null +++ b/requests/restaurant/update restaurant.bru @@ -0,0 +1,43 @@ +meta { + name: update restaurant + type: http + seq: 4 +} + +put { + url: {{baseUrl}}/api/restaurant/clr7ocdea00006tc1c5pc1gcn + body: json + auth: bearer +} + +headers { + Content-Type: application/json +} + +auth:bearer { + token: {{token}} +} + +body:json { + { + "name": "restaurant-name-updated", + "description": "restaurant-desc", + "address": { + "street": "restaurant-street-updated", + "city": "restaurant-city", + "zipCode": "restaurant-postalCode", + "country": "restaurant-country", + "lat": 59.445, + "lng": 2.099 + }, + "openingHoursList": [ + "12h-14h", + "19h-22h" + ], + "phone": "restaurant-phone", + "userIds": [ + "user-id-1", + "user-id-2" + ] + } +} diff --git a/services/gateway/src/controller/restaurant/restaurant.controller.ts b/services/gateway/src/controller/restaurant/restaurant.controller.ts index 0f0e015d..8f300176 100644 --- a/services/gateway/src/controller/restaurant/restaurant.controller.ts +++ b/services/gateway/src/controller/restaurant/restaurant.controller.ts @@ -1,12 +1,14 @@ import { Request, Response, Router } from "express"; import { restaurantServiceClient } from "@gateway/services/clients/restaurant.client"; -import { RestaurantId } from "@gateway/proto/product_pb"; import { Empty } from "google-protobuf/google/protobuf/empty_pb"; + import { ByLocationInput, Restaurant, + RestaurantId, RestaurantCreateInput, RestaurantDeleteInput, + Address, } from "@gateway/proto/restaurant_pb"; import { withCheck } from "@gateway/middleware/auth"; @@ -38,19 +40,17 @@ restaurantRoutes.post("/api/restaurant/by-location", async (req: Request, res: R in: 'body', required: true, schema: { - locationList: [1.099, 49.443] + lat: 49.443, + lng: 1.099 } } */ - const { locationList } = req.body; - restaurantServiceClient.getRestaurantsByLocation( - new ByLocationInput().setLocationList(locationList), - (error, response) => { - if (error) return res.status(500).send({ error }); - else return res.status(200).json(response.toObject()); - }, - ); + const { lat, lng } = req.body; + restaurantServiceClient.getRestaurantsByLocation(new ByLocationInput().setLat(lat).setLng(lng), (error, response) => { + if (error) return res.status(500).send({ error }); + else return res.status(200).json(response.toObject()); + }); }); restaurantRoutes.post("/api/restaurant", withCheck({ role: "ADMIN" }), (req: Request, res: Response) => { @@ -60,8 +60,14 @@ restaurantRoutes.post("/api/restaurant", withCheck({ role: "ADMIN" }), (req: Req schema: { name: "restaurant-name", description: "restaurant-desc", - locationList: [1.099, 49.443], - address: "restaurant-address", + address: { + street: "restaurant-street", + city: "restaurant-city", + zipCode: "restaurant-postalCode", + country: "restaurant-country", + lat: 49.443, + lng: 1.099 + }, openingHoursList: ["12h-14h", "19h-22h"], phone: "restaurant-phone", userIds: ["user-id-1", "user-id-2"] @@ -72,12 +78,20 @@ restaurantRoutes.post("/api/restaurant", withCheck({ role: "ADMIN" }), (req: Req required: true, type: 'string' } */ - const { name, description, locationList, address, openingHoursList, phone, userIds } = req.body; + const { name, description, address, openingHoursList, phone, userIds } = req.body; + const restaurantCreateInput = new RestaurantCreateInput() .setName(name) .setDescription(description) - .setLocationList(locationList) - .setAddress(address) + .setAddress( + new Address() + .setStreet(address.street) + .setCity(address.city) + .setZipcode(address.zipCode) + .setCountry(address.country) + .setLat(address.lat) + .setLng(address.lng), + ) .setOpeninghoursList(openingHoursList) .setPhone(phone) .setUseridsList(userIds); @@ -103,8 +117,14 @@ restaurantRoutes.put( schema: { name: "restaurant-name", description: "restaurant-desc", - locationList: [1.099, 49.443], - address: "restaurant-address", + address: { + street: "restaurant-street", + city: "restaurant-city", + zipCode: "restaurant-postalCode", + country: "restaurant-country", + lat: 49.443, + lng: 1.099 + }, openingHoursList: ["12h-14h", "19h-22h"], phone: "restaurant-phone", userIds: ["user-id-1", "user-id-2"] @@ -116,13 +136,20 @@ restaurantRoutes.put( type: 'string' } */ const { id } = req.params; - const { name, description, locationList, address, openingHoursList, phone, userIds } = req.body; + const { name, description, address, openingHoursList, phone, userIds } = req.body; const restaurantInput = new Restaurant() .setId(id) .setName(name) .setDescription(description) - .setLocationList(locationList) - .setAddress(address) + .setAddress( + new Address() + .setStreet(address.street) + .setCity(address.city) + .setZipcode(address.zipCode) + .setCountry(address.country) + .setLat(address.lat) + .setLng(address.lng), + ) .setOpeninghoursList(openingHoursList) .setPhone(phone) .setUseridsList(userIds); diff --git a/services/proto/restaurant.proto b/services/proto/restaurant.proto index 5a4d120d..ebd04bc0 100644 --- a/services/proto/restaurant.proto +++ b/services/proto/restaurant.proto @@ -21,7 +21,7 @@ message Restaurant { } message Address { - string id = 1; + optional string id = 1; float lat = 2; float lng = 3; @@ -31,7 +31,7 @@ message Address { optional string zipcode = 6; optional string country = 7; - string restaurantId = 8; + optional string restaurantId = 8; } message RestaurantCreateInput{ diff --git a/services/restaurant/src/handler/create.ts b/services/restaurant/src/handler/create.ts index 5b0f704e..b0b3a34b 100644 --- a/services/restaurant/src/handler/create.ts +++ b/services/restaurant/src/handler/create.ts @@ -18,7 +18,17 @@ export const CreateRestaurant = async ( data: { name, openingHours, - address: { create: address }, + address: { + create: { + lat: address.lat, + lng: address.lng, + + street: address.street, + city: address.city, + zipcode: address.zipcode, + country: address.country, + }, + }, description, phone, userIds, diff --git a/services/restaurant/src/handler/update.ts b/services/restaurant/src/handler/update.ts index 5533e707..e385243d 100644 --- a/services/restaurant/src/handler/update.ts +++ b/services/restaurant/src/handler/update.ts @@ -16,7 +16,17 @@ export const UpdateRestaurant = async ( data: { name, openingHours, - address: { update: address }, + address: { + update: { + lat: address.lat, + lng: address.lng, + + street: address.street, + city: address.city, + zipcode: address.zipcode, + country: address.country, + }, + }, description, phone, userIds, diff --git a/services/restaurant/src/types/restaurant.d.ts b/services/restaurant/src/types/restaurant.d.ts index 731e8b6f..0892f397 100644 --- a/services/restaurant/src/types/restaurant.d.ts +++ b/services/restaurant/src/types/restaurant.d.ts @@ -26,7 +26,7 @@ export type Address = { zipcode?: string; country?: string; - restaurantId: string; + restaurantId?: string; }; export type RestaurantCreateInput = Omit<