Exemple #1
0
 public static function createNewLike($input)
 {
     $like = new Like();
     $error_code = ApiResponse::OK;
     $like->user_id = Session::get('user_id');
     if (!empty($input['rating_id'])) {
         $like->rating_id = $input['rating_id'];
         $check_rating = Rating::check_rating($like->rating_id);
         if ($check_rating !== false) {
             if (Like::where('rating_id', $like->rating_id)->where('user_id', '=', $like->user_id)->first()) {
                 $error_code = ApiResponse::DUPLICATED_LIKE;
                 $data = ApiResponse::getErrorContent(ApiResponse::DUPLICATED_LIKE);
             } else {
                 //update like_count on rating
                 $like_rating = Rating::where('id', $like->rating_id)->first();
                 if ($like_rating != null) {
                     $like_rating->like_count = $like_rating->like_count + 1;
                     $like_rating->save();
                 }
                 $like->save();
                 $data = $like->toArray();
             }
         } else {
             $error_code = ApiResponse::UNAVAILABLE_RATING;
             $data = ApiResponse::getErrorContent(ApiResponse::UNAVAILABLE_RATING);
         }
     }
     return array("code" => $error_code, "data" => $data);
 }
Exemple #2
0
 public static function createNewComment($rating_id, $input)
 {
     $comment = new Comment();
     $error_code = ApiResponse::OK;
     $comment->user_id = Session::get('user_id');
     if (!empty($input['content'])) {
         $comment->content = $input['content'];
         $comment->rating_id = $rating_id;
         $check_rating = Rating::check_rating($comment->rating_id);
         if ($check_rating !== false) {
             //update comment_count on rating
             $comment_rating = Rating::where('id', $comment->rating_id)->first();
             if ($comment_rating != null) {
                 $comment_rating->comment_count = $comment_rating->comment_count + 1;
                 $comment_rating->save();
                 $comment->delete();
             }
             $comment_profile = Profile::where('user_id', $comment->user_id)->first();
             if ($comment_profile != null) {
                 $comment_profile->comment_count = $comment_profile->comment_count + 1;
                 $comment_profile->save();
             }
             $comment->save();
             $profile = Profile::where('user_id', $comment->user_id)->first();
             if ($profile->image != null) {
                 $comment->avatar_user = URL::asset($profile->image);
             } else {
                 $comment->avatar_user = $profile->image;
             }
             $comment->first_name = $profile->first_name;
             $comment->last_name = $profile->last_name;
             $data = $comment;
         } else {
             $error_code = ApiResponse::UNAVAILABLE_RATING;
             $data = ApiResponse::getErrorContent(ApiResponse::UNAVAILABLE_RATING);
         }
     } else {
         $error_code = ApiResponse::MISSING_PARAMS;
         $data = $input;
     }
     return array("code" => $error_code, "data" => $data);
 }