/** * Add new log_recently_viewed data * * @param $user_id * @param $restaurant_id * @throws \Exception */ public function addNewLog($user_id, $restaurant_id) { $connection = $this->getConnection(); try { $connection->beginTransaction(); $where = array('user_id' => $user_id, 'restaurant_id' => $restaurant_id); $log = LogRecentlyViewed::where($where)->get()->first(); if ($log) { return; } /*$logs = LogRecentlyViewed::getByUserId($user_id, CONSTANTS::ORDER_ASC); if ($logs->count() == CONSTANTS::LOG_RECENTLY_VIEWED_COUNT) { $logs[0]->delete(); }*/ $log = new LogRecentlyViewed(); $log->user_id = $user_id; $log->restaurant_id = $restaurant_id; $log->save(); $logs = LogRecentlyViewed::getByUserId($user_id); $connection->commit(); } catch (\Exception $e) { $connection->rollBack(); throw $e; } }
/** * Returns restaurants recently viewed by a user * * @param $user_id * @return \Symfony\Component\HttpFoundation\Response */ public function getAction($user_id) { $json_return = array(); if (!$user_id) { return showErrorResponse('Invalid user_id'); } $recently_viewed_data = LogRecentlyViewed::getByUserId($user_id); $recently_viewed_data = $recently_viewed_data->toArray(); $page_data = $recently_viewed_data; unset($page_data['data']); $recently_viewed_data = $recently_viewed_data['data']; $recently_viewed = array(); $restaurant = null; foreach ($recently_viewed_data as $rvd) { $restaurant = Restaurants::find($rvd['restaurant_id']); if ($restaurant) { $recently_viewed[] = ModelFormatter::restaurantFormat($restaurant); } $restaurant = null; } $json_return = array(KeyParser::data => $recently_viewed, KeyParser::page => array(KeyParser::current => $page_data['current_page'], KeyParser::number => $page_data['last_page'])); return response()->json($json_return); }