Esempio n. 1
0
 public static function createNewRating($input)
 {
     $rating = new Rating();
     $error_code = ApiResponse::OK;
     $rating->user_id = Session::get('user_id');
     if (!empty($input['wine_unique_id'])) {
         if (Wine::where('wine_unique_id', $input['wine_unique_id'])->first()) {
             $rating_old = Rating::where('wine_unique_id', $input['wine_unique_id'])->where('user_id', $rating->user_id)->first();
             if ($rating_old) {
                 $result = Rating::updateRatingDetail($rating_old->id, $input);
                 return $result;
             } else {
                 $rating->wine_unique_id = $input['wine_unique_id'];
                 if (!empty($input['rate'])) {
                     $rating->rate = $input['rate'];
                 }
                 if (!empty($input['content'])) {
                     $rating->content = $input['content'];
                 }
                 if (!empty($input['like_count'])) {
                     $rating->like_count = $input['like_count'];
                 }
                 if (!empty($input['comment_count'])) {
                     $rating->comment_count = $input['comment_count'];
                 }
                 if (!empty($input['is_my_wine'])) {
                     $rating->is_my_wine = $input['is_my_wine'];
                 }
                 // Validation and Filtering is sorely needed!!
                 // Seriously, I'm a bad person for leaving that out.
                 $check = Rating::check_validator($input);
                 if ($check !== false) {
                     $rating_profile = Profile::where('user_id', $rating->user_id)->first();
                     if ($rating_profile != null) {
                         $rating_profile->rate_count = $rating_profile->rate_count + 1;
                         $rating_profile->save();
                     }
                     $rating_wine = Wine::where('wine_unique_id', $rating->wine_unique_id)->first();
                     if ($rating_wine != null) {
                         $rating_rate = $rating_wine->average_rate * $rating_wine->rate_count;
                         $rating_wine->rate_count = $rating_wine->rate_count + 1;
                         $rating_wine->average_rate = ($rating_rate + $rating->rate) / $rating_wine->rate_count;
                         $rating_wine->save();
                     }
                     $rating->save();
                     $data = $rating->toArray();
                 } else {
                     $error_code = ApiResponse::UNAVAILABLE_RATING;
                     $data = ApiResponse::getErrorContent(ApiResponse::UNAVAILABLE_RATING);
                 }
             }
         } else {
             $error_code = ApiResponse::UNAVAILABLE_WINE;
             $data = ApiResponse::getErrorContent(ApiResponse::UNAVAILABLE_WINE);
         }
     } else {
         $error_code = ApiResponse::MISSING_PARAMS;
         $data = $input;
     }
     return array("code" => $error_code, "data" => $data);
 }