Exemple #1
0
 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);
 }
Exemple #2
0
 public function testGetWineDetailNoRatingOtherUser()
 {
     $this->setUpRating();
     $this->setUpCountry();
     $this->setUpWineNote();
     $this->setUpProfile();
     $rate_user1 = Rating::destroy(2);
     $rate_user2 = Rating::destroy(3);
     $response = $this->call('GET', 'api/wine/1');
     $wine_infor = Wine::where('wine_id', 1)->with('winery')->first();
     $country_name = Country::where('id', $wine_infor->winery->country_id)->first()->country_name;
     $wine_note = Winenote::where('wine_unique_id', $wine_infor->wine_unique_id)->where('user_id', $this->_user_id)->first();
     if ($wine_infor->wine_type != null) {
         $wine_infor->wine_type = Wine::getWineType($wine_infor->wine_type);
     }
     $wine_infor->winenote = $wine_note->note;
     $wine_infor->winery->country_id = $country_name;
     $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_infor->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();
     $wine_infor->total_like = $rating_user->like_count;
     if ($rating_user->profile->image != null) {
         $rating_user->profile->image = URL::asset($rating_user->profile->image);
     }
     $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();
     }
     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, 'wine_related' => $all_wines_winery->toArray());
     $this->assertEquals(array("code" => ApiResponse::OK, "data" => $data), json_decode($response->getContent(), true));
 }
Exemple #3
0
 public static function deleteWinenote($wine_unique_id)
 {
     $user_id = Session::get('user_id');
     $winenote = Winenote::where('user_id', $user_id)->where('wine_unique_id', $wine_unique_id)->first();
     $error_code = ApiResponse::OK;
     if (Wine::where('wine_unique_id', $wine_unique_id)->first()) {
         if ($winenote) {
             $winenote->delete();
             $data = 'Wine note is deleted';
         } else {
             $error_code = ApiResponse::UNAVAILABLE_WINE_NOTE;
             $data = ApiResponse::getErrorContent(ApiResponse::UNAVAILABLE_WINE_NOTE);
         }
     } else {
         $error_code = ApiResponse::UNAVAILABLE_WINE;
         $data = ApiResponse::getErrorContent(ApiResponse::UNAVAILABLE_WINE);
     }
     return array("code" => $error_code, "data" => $data);
 }
 public function testUpdateWinenoteNoNote()
 {
     $_params = $this->_params;
     $_params['user_id'] = $this->_user_id;
     $_params['note'] = "";
     $response = $this->action('POST', 'WinenoteController@update', array('wine_unique_id' => "1_2009"), array('data' => json_encode($_params), '_method' => 'PUT'));
     //get created login information
     $winenote_infor = Winenote::where('user_id', $this->_user_id)->where('wine_unique_id', "1_2009")->first();
     $this->assertEquals(array("code" => ApiResponse::OK, "data" => $winenote_infor->toArray()), json_decode($response->getContent(), true));
 }