public static function getListRatingMyWine() { $user_id = Session::get('user_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::where('user_id', $user_id)->where('is_my_wine', 1)->with('wine')->orderBy('updated_at', 'desc')->forPage($page, $limit)->get(); $error_code = ApiResponse::OK; if (count($ratings) == 0) { $data = array(); } else { foreach ($ratings as $rating) { $rating->winery = Winery::where('id', $rating->wine->winery_id)->first(); $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); } } $data = $ratings->toArray(); } } return array("code" => $error_code, "data" => $data); }
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); }