126 lines
4.8 KiB
PHP
126 lines
4.8 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Services\APIs\CustomerAPIs;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Models\CustomerFavouriteRestaurant;
|
|
use App\Models\ManageRestaurant;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
class RestaurantApiServices
|
|
{
|
|
public function getCoordinates($customerIamId)
|
|
{
|
|
try {
|
|
$restaurants = ManageRestaurant::with('operatingHours')->select(
|
|
'id',
|
|
'name',
|
|
'image',
|
|
'address',
|
|
'short_id',
|
|
'latitude',
|
|
'longtitude'
|
|
)
|
|
->where('is_active', '1')
|
|
->get()
|
|
->toArray();
|
|
|
|
foreach ($restaurants as &$restaurant) {
|
|
$restaurant['image'] = ListingImageUrl('restaurant_images', $restaurant['image']);
|
|
|
|
$isFavourite = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
|
|
->where('restaurant_xid', $restaurant['id'])
|
|
->exists();
|
|
$restaurant['is_favourite'] = $isFavourite;
|
|
}
|
|
|
|
return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $restaurants, 200);
|
|
} catch (Exception $ex) {
|
|
Log::error('Restaurant Get service failed : ' . $ex->getMessage());
|
|
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
public function addToFavourite($customerIamId, $request)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$restaurant = ManageRestaurant::where('short_id', $request->id)->where('is_active', '1')->first();
|
|
if (!$restaurant) {
|
|
return jsonResponseWithErrorMessage(__('auth.restaurant_data_not_found'), 404);
|
|
}
|
|
|
|
$existingFavourite = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
|
|
->where('restaurant_xid', $restaurant->id)
|
|
->first();
|
|
|
|
if ($existingFavourite) {
|
|
return jsonResponseWithErrorMessage(__('auth.restaurant_already_favourite'), 409);
|
|
}
|
|
|
|
$favRestaurant = new CustomerFavouriteRestaurant();
|
|
$favRestaurant->principal_xid = $customerIamId;
|
|
$favRestaurant->restaurant_xid = $restaurant->id;
|
|
$favRestaurant->save();
|
|
|
|
DB::commit();
|
|
|
|
return jsonResponseWithSuccessMessage(__('auth.data_updated_successfully'), 200);
|
|
} catch (\Exception $ex) {
|
|
DB::rollBack();
|
|
Log::error('Favourite Restaurant service failed: ' . $ex->getMessage());
|
|
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function listFavRestaurant($customerIamId)
|
|
{
|
|
try {
|
|
$list = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)->get()->toArray();
|
|
$customerFavouriteRestaurants = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
|
|
->pluck('restaurant_xid')
|
|
->toArray();
|
|
|
|
$restaurants = ManageRestaurant::with('operatingHours')->where('is_active', '1')->whereIn('id', $customerFavouriteRestaurants)->get();
|
|
|
|
foreach ($restaurants as &$res) {
|
|
$res['image'] = ListingImageUrl('restaurant_images', $res['image']);
|
|
}
|
|
|
|
return jsonResponseWithSuccessMessage(__('auth.data_updated_successfully'), $restaurants, 200);
|
|
} catch (Exception $ex) {
|
|
Log::error('List of Favourite Restaurant service failed : ' . $ex->getMessage());
|
|
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
|
|
public function DetailRestaurant($customerIamId, $id)
|
|
{
|
|
try {
|
|
$rest = ManageRestaurant::with('operatingHours')->select('id', 'short_id', 'name', 'description', 'restaurant_id', 'address', 'image', 'bio', 'try_on_1', 'try_on_2', 'try_on_3', 'try_on_4', 'exclusion', 'latitude', 'longtitude')->where('short_id', $id)->where('is_active', '1')->first();
|
|
if ($rest) {
|
|
$rest->image = ListingImageUrl('restaurant_images', $rest->image);
|
|
$isFavourite = CustomerFavouriteRestaurant::where('principal_xid', $customerIamId)
|
|
->where('restaurant_xid', $rest->id)
|
|
->exists();
|
|
$rest->is_favourite = $isFavourite;
|
|
}
|
|
if (!$rest) {
|
|
return jsonResponseWithErrorMessage(__('auth.restaurant_data_not_found'), 404);
|
|
}
|
|
return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $rest, 200);
|
|
} catch (\Exception $ex) {
|
|
Log::error('DetailRestaurant service failed: ' . $ex->getMessage());
|
|
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
}
|