Merge pull request #177 from WDI-Ideas/sayli

Search restaurant API
This commit is contained in:
Sayli Raut
2024-06-20 13:05:31 +05:30
committed by GitHub
3 changed files with 132 additions and 74 deletions

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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']);