From 2358f561bf2f0ddf05c387c280d367bf2ddff52d Mon Sep 17 00:00:00 2001 From: sayliraut Date: Thu, 20 Jun 2024 13:05:03 +0530 Subject: [PATCH] Search restaurant API --- .../Customer_API/RestaurantControllerApi.php | 24 ++- .../CustomerAPIs/RestaurantApiServices.php | 180 +++++++++++------- routes/customer_api.php | 2 + 3 files changed, 132 insertions(+), 74 deletions(-) diff --git a/app/Http/Controllers/Admin/APIs/Customer_API/RestaurantControllerApi.php b/app/Http/Controllers/Admin/APIs/Customer_API/RestaurantControllerApi.php index c2d08c2..31e8e2d 100644 --- a/app/Http/Controllers/Admin/APIs/Customer_API/RestaurantControllerApi.php +++ b/app/Http/Controllers/Admin/APIs/Customer_API/RestaurantControllerApi.php @@ -171,7 +171,7 @@ class RestaurantControllerApi extends Controller } } - /** + /** * Created By : Sayli Raut * Created at : 04 June 2024 * Use : To redeem restaurant. @@ -198,4 +198,26 @@ class RestaurantControllerApi extends Controller return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500); } } + + + /** + * Created By : Sayli Raut + * Created at : 20 June 2024 + * Use : To search from restaurant. + */ + public function searchRestaurant(Request $request) + { + try { + $token = readHeaderToken(); + if ($token) { + $customerIamId = $token['sub']; + return $this->RestaurantApiServices->searchRestaurant($customerIamId, $request); + } else { + return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409); + } + } catch (\Exception $e) { + Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage(), ['exception' => $e]); + return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500); + } + } } diff --git a/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php b/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php index 669397b..c20afdb 100644 --- a/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php +++ b/app/Services/APIs/CustomerAPIs/RestaurantApiServices.php @@ -212,99 +212,133 @@ class RestaurantApiServices public function redeemRestaurant($customerIamId, $request) -{ - try { - $restaurantId = $request->id; + { + try { + $restaurantId = $request->id; - $restaurant = ManageRestaurant::with('operatingHours')->where('short_id', $restaurantId)->first(); + $restaurant = ManageRestaurant::with('operatingHours')->where('short_id', $restaurantId)->first(); - if (!$restaurant) { - return jsonResponseWithErrorMessageApi(__('auth.restaurant_not_found'), 404); - } + if (!$restaurant) { + return jsonResponseWithErrorMessageApi(__('auth.restaurant_not_found'), 404); + } - // Check if the restaurant has already been redeemed - $restaurantExist = RedeemRestaurant::where('manage_restaurants_xid', $restaurant->id)->where('iam_principal_xid', $customerIamId) - ->where('is_redeem', 1) - ->first(); + // Check if the restaurant has already been redeemed + $restaurantExist = RedeemRestaurant::where('manage_restaurants_xid', $restaurant->id)->where('iam_principal_xid', $customerIamId) + ->where('is_redeem', 1) + ->first(); - if ($restaurantExist) { - return jsonResponseWithErrorMessageApi(__('auth.restaurant_already_redeemed'), 400); - } + if ($restaurantExist) { + return jsonResponseWithErrorMessageApi(__('auth.restaurant_already_redeemed'), 400); + } - // Check if there's an existing entry for the restaurant and update it - $restexist = RedeemRestaurant::where('manage_restaurants_xid', $restaurant->id)->where('iam_principal_xid', $customerIamId)->first(); - if ($restexist) { - $restexist->is_redeem = 1; - $restexist->redeem_date = now(); - $restexist->is_redeemption_undone = 0; - $restexist->redeemption_undone_date = null; - $restexist->save(); - return jsonResponseWithSuccessMessageApi(__('success.restaurant_redeem'), $restexist, 200); - } + // Check if there's an existing entry for the restaurant and update it + $restexist = RedeemRestaurant::where('manage_restaurants_xid', $restaurant->id)->where('iam_principal_xid', $customerIamId)->first(); + if ($restexist) { + $restexist->is_redeem = 1; + $restexist->redeem_date = now(); + $restexist->is_redeemption_undone = 0; + $restexist->redeemption_undone_date = null; + $restexist->save(); + return jsonResponseWithSuccessMessageApi(__('success.restaurant_redeem'), $restexist, 200); + } - // Create a new redeem entry if it doesn't exist - $redeemRestaurant = new RedeemRestaurant(); - $redeemRestaurant->iam_principal_xid = $customerIamId; - $redeemRestaurant->manage_restaurants_xid = $restaurant->id; - $redeemRestaurant->is_redeem = 1; // Redeem restaurant - $redeemRestaurant->redeem_date = now(); - $redeemRestaurant->is_redeemption_undone = 0; - $redeemRestaurant->redeemption_undone_date = null; - $redeemRestaurant->save(); + // Create a new redeem entry if it doesn't exist + $redeemRestaurant = new RedeemRestaurant(); + $redeemRestaurant->iam_principal_xid = $customerIamId; + $redeemRestaurant->manage_restaurants_xid = $restaurant->id; + $redeemRestaurant->is_redeem = 1; // Redeem restaurant + $redeemRestaurant->redeem_date = now(); + $redeemRestaurant->is_redeemption_undone = 0; + $redeemRestaurant->redeemption_undone_date = null; + $redeemRestaurant->save(); - $imagePath = ListingImageUrl('restaurant_images', $restaurant->image); + $imagePath = ListingImageUrl('restaurant_images', $restaurant->image); - $customerTitle = "Your Redemption was successful for " . $restaurant->name; - $customerMessage = $restaurant->name . " Voucher Redeemed Successfully"; - $customerContentType = 'Voucher_Redemption'; - $customerImageUrl = $imagePath; + $customerTitle = "Your Redemption was successful for " . $restaurant->name; + $customerMessage = $restaurant->name . " Voucher Redeemed Successfully"; + $customerContentType = 'Voucher_Redemption'; + $customerImageUrl = $imagePath; - $customerData = IamPrincipal::where('id', $customerIamId)->where('notification_status', 1)->where('principal_type_xid', 3)->first(); - $restaurants = IamPrincipalRestaurantRole::select('id', 'principal_xid')->where('restaurant_xid', $restaurant->id)->get(); + $customerData = IamPrincipal::where('id', $customerIamId)->where('notification_status', 1)->where('principal_type_xid', 3)->first(); + $restaurants = IamPrincipalRestaurantRole::select('id', 'principal_xid')->where('restaurant_xid', $restaurant->id)->get(); - foreach ($restaurants as $restaurant) { - $restUser = IamPrincipal::where('id', $restaurant->principal_xid)->where('notification_status', 1)->where('principal_type_xid', 4)->first(); + foreach ($restaurants as $restaurant) { + $restUser = IamPrincipal::where('id', $restaurant->principal_xid)->where('notification_status', 1)->where('principal_type_xid', 4)->first(); - if ($restUser) { - $restImagePath = ListingImageUrl('restaurant_images', $restaurant->image); - $restTitle = "New redemption for " . $restaurant->name; - $restMessage = $restaurant->name . " new voucher Redeemed Successfully"; - $restContent_type = 'Voucher_Redemption'; - $restImageUrl = $restImagePath; + if ($restUser) { + $restImagePath = ListingImageUrl('restaurant_images', $restaurant->image); + $restTitle = "New redemption for " . $restaurant->name; + $restMessage = $restaurant->name . " new voucher Redeemed Successfully"; + $restContent_type = 'Voucher_Redemption'; + $restImageUrl = $restImagePath; - // Sending notification to restaurant - $pushNotificationToRestaurant = onesignalhelper::restSendNotificationApi( - $restUser->one_signal_player_id, - $restTitle, - $restMessage, - $restContent_type, - $restImageUrl, + // Sending notification to restaurant + $pushNotificationToRestaurant = onesignalhelper::restSendNotificationApi( + $restUser->one_signal_player_id, + $restTitle, + $restMessage, + $restContent_type, + $restImageUrl, + $id = null + ); + onesignalhelper::StoreNotificationDetails($restUser->id, $restContent_type, $restTitle, $restImageUrl); + } + } + + if ($customerData) { + // Sending notification to customer + $pushNotificationToCustomer = onesignalhelper::sendNotificationApi( + $customerData->one_signal_player_id, + $customerTitle, + $customerMessage, + $customerContentType, + $customerImageUrl, $id = null ); - onesignalhelper::StoreNotificationDetails($restUser->id, $restContent_type, $restTitle, $restImageUrl); + + onesignalhelper::StoreNotificationDetails($customerData->id, $customerContentType, $customerTitle, $customerImageUrl); } + return jsonResponseWithSuccessMessageApi(__('success.restaurant_redeem'), $redeemRestaurant, 200); + } catch (Exception $ex) { + Log::error('Restaurant Redeem service failed: ' . $ex->getMessage()); + return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500); } + } - if ($customerData) { - // Sending notification to customer - $pushNotificationToCustomer = onesignalhelper::sendNotificationApi( - $customerData->one_signal_player_id, - $customerTitle, - $customerMessage, - $customerContentType, - $customerImageUrl, - $id = null - ); - onesignalhelper::StoreNotificationDetails($customerData->id, $customerContentType, $customerTitle, $customerImageUrl); + public function searchRestaurant($customerIamId, $request) + { + try { + $restaurantsQuery = ManageRestaurant::with(['operatingHours', 'state']) + ->where('is_active', '1'); + + $searchData = $request->input('search_data'); + + if (!empty($searchData)) { + $restaurantsQuery->where(function ($query) use ($searchData) { + $query->where('name', 'like', "%$searchData%") + ->orWhere('description', 'like', "%$searchData%") + ->orWhere('address', 'like', "%$searchData%") + ->orWhereHas('state', function ($stateQuery) use ($searchData) { + $stateQuery->where('name', 'like', "%$searchData%"); + }); + }); + } + + $restaurants = $restaurantsQuery->get(); + + foreach ($restaurants as &$res) { + $res['image'] = ListingImageUrl('restaurant_images', $res['image']); + } + + return jsonResponseWithSuccessMessageApi(__('auth.restaurant_search'), $restaurants, 200); + } catch (Exception $ex) { + Log::error('Search from restaurant service failed: ' . $ex->getMessage()); + return response()->json([ + 'message' => __('auth.something_went_wrong') + ], 500); } - return jsonResponseWithSuccessMessageApi(__('success.restaurant_redeem'), $redeemRestaurant, 200); - } catch (Exception $ex) { - Log::error('Restaurant Redeem service failed: ' . $ex->getMessage()); - return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500); } } - -} diff --git a/routes/customer_api.php b/routes/customer_api.php index 8133a90..f4c7c57 100644 --- a/routes/customer_api.php +++ b/routes/customer_api.php @@ -58,6 +58,8 @@ Route::middleware(['customerApiBasicAuth'])->group(function () { Route::post('/v1/remove-from-favourite', [RestaurantControllerApi::class, 'removeFromFavourite']); Route::post('/v1/search-from-favourite', [RestaurantControllerApi::class, 'searchFromFavourite']); Route::post('/v1/redeem-restaurant', [RestaurantControllerApi::class, 'redeemRestaurant']); + Route::post('/v1/search-restaurant', [RestaurantControllerApi::class, 'searchRestaurant']); +