public static function timeline() { $user_id = Session::get('user_id'); $error_code = ApiResponse::OK; $user_timeline = array(); $user_timeline[] = $user_id; $user_follow = Follow::where('from_id', $user_id)->orderBy('updated_at', 'asc')->get(); if (isset($user_follow)) { foreach ($user_follow as $user) { $user_timeline[] = $user->to_id; } } $pagination = ApiResponse::pagination(); if ($pagination == false) { $error_code = ApiResponse::URL_NOT_EXIST; $data = ApiResponse::getErrorContent(ApiResponse::URL_NOT_EXIST); } else { $page = $pagination['page']; $limit = $pagination['limit']; $ratings = Rating::whereIn('user_id', $user_timeline)->whereNotNull('wine_unique_id')->with('profile')->with('wine')->orderBy('updated_at', 'desc')->forPage($page, $limit)->get(); if (count($ratings) == 0) { $data = array(); } else { foreach ($ratings as $rating) { $winery = Winery::where('id', $rating->wine->winery_id)->first(); $rating->winery = $winery; $country = Country::where('id', $rating->winery->country_id)->first(); if ($country) { $rating->winery->country_name = $country->country_name; } else { $rating->winery->country_name = null; } $like = Like::where('user_id', $user_id)->where('rating_id', $rating->id)->first(); if ($like) { $rating->liked = true; } else { $rating->liked = false; } $wishlist = Wishlist::where('user_id', $user_id)->where('wine_unique_id', $rating->wine_unique_id)->first(); if ($wishlist) { $rating->wishlist = true; } else { $rating->wishlist = false; } $rating->wine->image_url = Wine::getImageWineFromServer($user_id, $rating->wine->wine_unique_id, $rating->wine->image_url); if ($rating->wine->wine_flag != null) { $rating->wine->wine_flag = URL::asset($rating->wine->wine_flag); } if ($rating->profile->image != null) { $rating->profile->image = URL::asset($rating->profile->image); } } $data = $ratings->toArray(); } } return array("code" => $error_code, "data" => $data); }
public function testGetTimelineSuccess() { $this->setUpRating(); $this->setUpCountry(); $this->setUpWineNote(); $this->setUpProfile(); $_params = $this->_params; $_params['user_id'] = "user_id"; $response = $this->_getAuth($_params); $error_code = ApiResponse::OK; $user_timeline = array(); $user_timeline[] = $this->_user_id; $user_follow = Follow::where('from_id', $this->_user_id)->orderBy('updated_at', 'asc')->get(); if (isset($user_follow)) { foreach ($user_follow as $user) { $user_timeline[] = $user->to_id; } } $pagination = ApiResponse::pagination(); $page = $pagination['page']; $limit = $pagination['limit']; $wine = Wine::with('winery')->forPage($page, $limit)->get(); $ratings = Rating::whereIn('user_id', $user_timeline)->whereNotNull('wine_unique_id')->with('profile')->with('wine')->forPage($page, $limit)->get(); foreach ($ratings as $rating) { $winery = Winery::where('id', $rating->wine->winery_id)->first(); $rating->winery = $winery; $country = Country::where('id', $rating->winery->country_id)->first(); $rating->winery->country_name = $country->country_name; $like = Like::where('user_id', $this->_user_id)->where('rating_id', $rating->id)->first(); if ($like) { $rating->liked = true; } else { $rating->liked = false; } $wishlist = Wishlist::where('user_id', $this->_user_id)->where('wine_unique_id', $rating->wine_unique_id)->first(); if ($wishlist) { $rating->wishlist = true; } else { $rating->wishlist = false; } if ($rating->wine->image_url != null) { $rating->wine->image_url = URL::asset($rating->wine->image_url); } if ($rating->wine->wine_flag != null) { $rating->wine->wine_flag = URL::asset($rating->wine->wine_flag); } if ($rating->profile->image != null) { $rating->profile->image = URL::asset($rating->profile->image); } $rating->winery = $rating->winery->toArray(); } $data = $ratings; $this->assertEquals(array("code" => ApiResponse::OK, "data" => $ratings->toArray()), json_decode($response->getContent(), true)); }