public static function getWineDetail($wine_id) { $user_id = Session::get('user_id'); $wine = Wine::where('wine_id', $wine_id)->with('winery')->first(); $error_code = ApiResponse::OK; if ($wine) { if ($wine->wine_type != null) { $wine->wine_type = Wine::getWineType($wine->wine_type); } $wine->image_url = Wine::getImageWineFromServer($user_id, $wine->wine_unique_id, $wine->image_url); if ($wine->wine_flag != null) { $wine->wine_flag = URL::asset($wine->wine_flag); } $country = Country::where('id', $wine->winery->country_id)->first(); if ($country) { $wine->winery->country_id = $country->country_name; } else { $wine->winery->country_id = null; } $wine_note = Winenote::where('wine_unique_id', $wine->wine_unique_id)->where('user_id', $user_id)->first(); if ($wine_note) { $wine->winenote = $wine_note->note; } else { $wine->winenote = null; } $wishlist = Wishlist::where('user_id', $user_id)->where('wine_unique_id', $wine->wine_unique_id)->first(); if ($wishlist) { $wine->is_wishlist = true; } else { $wine->is_wishlist = false; } $all_wines_winery = Wine::where('winery_id', $wine->winery_id)->whereNotIn('wine_id', [$wine_id])->where('year', '>', 0)->where('average_rate', '>', 0)->orderBy('year', 'desc')->take(10)->get(); $wine->winery->count_wine = count($all_wines_winery) + 1; $rate_winery = $wine->rate_count; if (count($all_wines_winery) !== 0) { $sum_rate_winery = $wine->average_rate; foreach ($all_wines_winery as $wine_winery) { $wine_on_winery = Wine::where('wine_id', $wine_winery->wine_id)->first(); $wine_on_winery->image_url = Wine::getImageWineFromServer($user_id, $wine_on_winery->wine_unique_id, $wine_on_winery->image_url); $rate_count = $wine_on_winery->rate_count; $rate_winery = $rate_winery + $rate_count; $average_rate = $wine_on_winery->average_rate; $sum_rate_winery = $sum_rate_winery + $average_rate; } $wine->winery->total_rate = $rate_winery; $wine->winery->average_rate_winery = $sum_rate_winery / count($all_wines_winery); } else { $wine->winery->total_rate = $rate_winery; $wine->winery->average_rate_winery = $wine->average_rate; } $wine->total_like = 0; $rating_user = Rating::where('wine_unique_id', $wine->wine_unique_id)->where('user_id', $user_id)->with('profile')->first(); if (count($rating_user) == 0) { $rating_user = null; } else { if ($rating_user->profile->image != null) { $rating_user->profile->image = URL::asset($rating_user->profile->image); } $wine->total_like = $wine->total_like + $rating_user->like_count; } $ratings = Rating::where('wine_unique_id', $wine->wine_unique_id)->whereNotIn('user_id', [$user_id])->with('profile')->get(); if (count($ratings) == 0) { $ratings = array(); } else { foreach ($ratings as $rating) { if ($rating->profile->image != null) { $rating->profile->image = URL::asset($rating->profile->image); } $follow = Follow::where('from_id', $user_id)->where('to_id', $rating->user_id)->first(); if ($follow) { $rating->is_follow = true; } else { $rating->is_follow = false; } $wine->total_like = $wine->total_like + $rating->like_count; } } $data = array('wine' => $wine, 'rate_user' => $rating_user, 'rate' => $ratings, 'wine_related' => $all_wines_winery); } else { $error_code = ApiResponse::UNAVAILABLE_WINE; $data = ApiResponse::getErrorContent(ApiResponse::UNAVAILABLE_WINE); } return array("code" => $error_code, "data" => $data); }
public function testGetWineDetailNoWineNote() { $this->setUpRating(); $this->setUpCountry(); $this->setUpProfile(); $wine_note = Winenote::destroy(1); $response = $this->call('GET', 'api/wine/1'); $wine_infor = Wine::where('wine_id', 1)->with('winery')->first(); $wine_infor->total_like = 0; $country_name = Country::where('id', $wine_infor->winery->country_id)->first()->country_name; $wine_infor->winenote = null; $wine_infor->winery->country_id = $country_name; if ($wine_infor->wine_type != null) { $wine_infor->wine_type = Wine::getWineType($wine_infor->wine_type); } $wishlist = Wishlist::where('user_id', $this->_user_id)->where('wine_unique_id', $wine_infor->wine_unique_id)->first(); if ($wishlist) { $wine_infor->is_wishlist = true; } else { $wine_infor->is_wishlist = false; } $all_wines_winery = Wine::where('winery_id', $wine_infor->winery_id)->whereNotIn('wine_id', [1])->take(10)->get(); $wine_infor->winery->count_wine = count($all_wines_winery) + 1; $rate_winery = $wine_infor->rate_count; if (count($all_wines_winery) !== 0) { $sum_rate_winery = $wine_infor->average_rate; foreach ($all_wines_winery as $wine_winery) { $wine_on_winery = Wine::where('wine_id', $wine_winery->wine_id)->first(); $rate_count = $wine_on_winery->rate_count; $rate_winery = $rate_winery + $rate_count; $average_rate = $wine_on_winery->average_rate; $sum_rate_winery = $sum_rate_winery + $average_rate; } $wine_infor->winery->total_rate = $rate_winery; $wine_inforine->winery->average_rate_winery = $sum_rate_winery / count($all_wines_winery); } else { $wine_infor->winery->total_rate = $rate_winery; $wine_infor->winery->average_rate_winery = $wine_infor->average_rate; } $rating_user = Rating::where('wine_unique_id', $wine_infor->wine_unique_id)->where('user_id', $this->_user_id)->with('profile')->first(); if ($rating_user->profile->image != null) { $rating_user->profile->image = URL::asset($rating_user->profile->image); } $wine_infor->total_like = $wine_infor->total_like + $rating_user->like_count; $rating = Rating::where('wine_unique_id', $wine_infor->wine_unique_id)->whereNotIn('user_id', [$this->_user_id])->with('profile')->get(); if (count($rating) == 0) { $rating = array(); } else { foreach ($rating as $ratings) { if ($ratings->profile->image != null) { $ratings->profile->image = URL::asset($ratings->profile->image); } $follow = Follow::where('from_id', $this->_user_id)->where('to_id', $ratings->user_id)->first(); if ($follow) { $ratings->is_follow = true; } else { $ratings->is_follow = false; } $wine_infor->total_like = $wine_infor->total_like + $ratings->like_count; } } if ($wine_infor->image_url != null) { $wine_infor->image_url = URL::asset($wine_infor->image_url); } if ($wine_infor->wine_flag != null) { $wine_infor->wine_flag = URL::asset($wine_infor->wine_flag); } $data = array('wine' => $wine_infor->toArray(), 'rate_user' => $rating_user->toArray(), 'rate' => $rating->toArray(), 'wine_related' => $all_wines_winery->toArray()); $this->assertEquals(array("code" => ApiResponse::OK, "data" => $data), json_decode($response->getContent(), true)); }