public static function restaurantSearch($params) { $longitude = isset($params['long']) ? $params['long'] : 0; $latitude = isset($params['lat']) ? $params['lat'] : 0; $review_count_query = "(SELECT COUNT(id) FROM reviews WHERE reviews.restaurant_id = restaurants.id)"; $checkin_count_query = "(SELECT COUNT(id) FROM check_ins WHERE check_ins.restaurant_id = restaurants.id)"; $restaurants = Restaurants::select('restaurants.*', DB::raw("{$review_count_query} AS review_count"), DB::raw("{$checkin_count_query} AS checkin_count")); if (isset($params['name'])) { $restaurants->where('restaurants.name', 'LIKE', '%' . substr($params['name'], 0, 30) . '%'); } if (isset($params['address'])) { $restaurants->where('restaurants.address', 'LIKE', '%' . substr($params['address'], 0, 30) . '%'); } if (isset($params['tag']) && $params['tag'] != '') { $tag = $params['tag']; $restaurants->leftJoin('restaurants_category', 'restaurants.id', '=', 'restaurants_category.restaurant_id')->where('restaurants_category.category_id', $tag); } $search_results = $restaurants->paginate(CONSTANTS::RESTAURANTS_PARTIAL_SEARCH_PAGINATION_LIMIT); $distances = array(); $data = array(); foreach ($search_results as $count => $restaurant) { if ($restaurant['id'] == NULL) { continue; } if ($longitude > 0 && $latitude > 0) { $restaurant->distance = 3956 * 2 * asin(sqrt(pow(sin(($latitude - $restaurant->latitude) * pi() / 180 / 2), 2) + cos($latitude * pi() / 180) * cos($restaurant->latitude * pi() / 180) * pow(sin(($longitude - $restaurant->longitude) * pi() / 180 / 2), 2))); } else { $restaurant->distance = 0; } $data[$count]['restaurant'] = ModelFormatter::restaurantSearchFormat($restaurant); $distances[$count] = $restaurant->distance; $data[$count][KeyParser::categories] = Categories::getFormattedRestaurantCategories($restaurant->id); } array_multisort($distances, SORT_ASC, $data); $page = array(KeyParser::current => $search_results->currentPage(), KeyParser::number => $search_results->lastPage()); return array(KeyParser::data => $data, KeyParser::page => $page); }
/** Displays list of restaurants within user's recent activity list * route: /restaurants/recent-activity/{user_id}/{search_key?} * * @param $user_id * @param $search_key (optional) for restaurant name searches * @return Response */ public function recentActivitySearchAction($user_id, $search_key = null) { $restaurants = Restaurants::getRecentActivityRestaurants($user_id, $search_key); $data = array(); foreach ($restaurants as $restaurant) { $categories = Categories::getFormattedRestaurantCategories($restaurant->id); $data[] = array(KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::categories => $categories); } $page = array(KeyParser::current => $restaurants->currentPage(), KeyParser::number => $restaurants->lastPage()); $json_return = array(KeyParser::data => $data, KeyParser::page => $page); return response()->json($json_return); }
/** * get all of the bookmarks of a user * /bookmarks/user/{user_id} * * @param $user_id * @return Response */ public function userBookmarkListAction($user_id) { if (!is_numeric($user_id)) { return showErrorResponse('Incorrect User ID format'); } $bookmark_list = Bookmarks::getBookmarkByUserId($user_id); $user_data = Users::find($user_id); $json_return[KeyParser::data] = array(); if ($bookmark_list) { foreach ($bookmark_list as $bookmark) { $restaurant_data = Restaurants::find($bookmark->restaurant_id); if ($restaurant_data) { $json_return[KeyParser::data][] = array(KeyParser::bookmark => ModelFormatter::bookmarkFormat($bookmark), KeyParser::user => ModelFormatter::userFormat($user_data), KeyParser::restaurant => ModelFormatter::restaurantBookmarkListViewFormat($restaurant_data), KeyParser::categories => Categories::getFormattedRestaurantCategories($bookmark->restaurant_id)); } } } $json_return[KeyParser::page] = array(KeyParser::current => $bookmark_list->currentPage(), KeyParser::number => $bookmark_list->lastPage()); return response()->json($json_return); }