/** * Add like on review, checkin or photo * * @param $request * @return mixed */ public function addAction(Request $request) { $data = $request->json()->get('Like'); $type = $data['type']; $type_id = $data['type_id']; $user_id = $data['user_id']; $user_data = Users::find($user_id); if (is_null($user_data)) { return showErrorResponse('Invalid user'); } $is_liked = Like::isLiked($user_id, $type, $type_id); if ($is_liked) { $like_count = Like::getCount($type, $type_id); $json_return[KeyParser::data] = array(KeyParser::type => $type, KeyParser::type_id => $type_id, KeyParser::user_id => $user_id, KeyParser::is_existing => CONSTANTS::LIKE_IS_EXISTING, KeyParser::like_count => $like_count); return response()->json($json_return); } switch ($type) { case CONSTANTS::REVIEW: $like_object = Reviews::find($type_id); $like_type = CONSTANTS::NOTIFICATION_TYPE_LIKE_REVIEW; if (!$like_object) { $status_code = CONSTANTS::ERROR_CODE_REVIEW_MISSING; } break; case CONSTANTS::CHECKIN: $like_object = CheckIns::find($type_id); $like_type = CONSTANTS::NOTIFICATION_TYPE_LIKE_CHECKIN; if (!$like_object) { $status_code = CONSTANTS::ERROR_CODE_CHECKIN_MISSING; } break; case CONSTANTS::PHOTO: $like_object = Photos::find($type_id); $like_type = CONSTANTS::NOTIFICATION_TYPE_LIKE_PHOTO; if (!$like_object) { $status_code = CONSTANTS::ERROR_CODE_PHOTO_MISSING; } break; default: return showErrorResponse('Invalid type', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_INVALID_TYPE); } if (!isset($like_object)) { return showErrorResponse('Invalid type id', HTTP_ACCEPTED, $status_code); } if (!Restaurants::isExists($like_object->restaurant_id)) { return showErrorResponse('Restaurant data not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL); } try { $like_data = new Like(); $query_response = $like_data->addLike($type_id, $type, $user_id, $like_object, $like_type); } catch (\Exception $e) { return showErrorResponse($e->getMessage()); } $json_return[KeyParser::data] = array(KeyParser::type => $query_response->type, KeyParser::type_id => $query_response->type_id, KeyParser::user_id => $query_response->user_id, KeyParser::is_existing => CONSTANTS::LIKE_IS_NOT_EXISTING, KeyParser::like_count => Like::getCount($type, $type_id)); return response()->json($json_return); }
/** * Output JSON output of comments in a review * * @param $id * @return Response */ public function viewByReviewIdAction($id) { $review = Reviews::find($id); if (!$review) { return showErrorResponse('Review not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_REVIEW_MISSING); } if (!Restaurants::isExists($review->restaurant_id)) { return showErrorResponse('Restaurant data not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL); } $json_return = array(KeyParser::data => array(), KeyParser::comment_count => 0, KeyParser::page => array()); $comments = Comments::getByTypePaginate(CONSTANTS::REVIEW, $id); if ($comments->count()) { foreach ($comments as $comment) { $json_return[KeyParser::data][] = array(KeyParser::comment => ModelFormatter::commentFormat($comment), KeyParser::user => Users::find($comment->user_id)); } $json_return[KeyParser::comment_count] = Comments::getCountByType(CONSTANTS::REVIEW, $id); $json_return[KeyParser::page] = array(KeyParser::current => $comments->currentPage(), KeyParser::number => $comments->lastPage()); } return response()->json($json_return); }
function checkTypeId($type, $type_id) { switch ($type) { case CONSTANTS::REVIEW: $object = Reviews::find($type_id); $message = "Review does not exist"; $error_code = CONSTANTS::ERROR_CODE_REVIEW_MISSING; break; case CONSTANTS::CHECKIN: $object = CheckIns::find($type_id); $message = "Checkin does not exist"; $error_code = CONSTANTS::ERROR_CODE_CHECKIN_MISSING; break; case CONSTANTS::BOOKMARK: $object = Bookmarks::find($type_id); $message = "Bookmark does not exist"; $error_code = CONSTANTS::ERROR_CODE_GENERAL; break; case CONSTANTS::COMMENT: $object = Comments::find($type_id); $message = "Comment does not exist"; $error_code = CONSTANTS::ERROR_CODE_GENERAL; break; case CONSTANTS::PHOTO: $object = Photos::find($type_id); $message = "Photo does not exist"; $error_code = CONSTANTS::ERROR_CODE_PHOTO_MISSING; break; case CONSTANTS::RESTAURANT: $object = Restaurants::find($type_id); $message = "Restaurant does not exist"; $error_code = CONSTANTS::ERROR_CODE_GENERAL; break; case 'user': $object = Users::find($type_id); $message = "User does not exist"; $error_code = CONSTANTS::ERROR_CODE_GENERAL; break; default: return showErrorResponse('Invalid type', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_INVALID_TYPE); } if (!$object) { return showErrorResponse($message, HTTP_ACCEPTED, $error_code); } return false; }
/** * Delete Review * @param $id * @throws $e */ public function deleteReview($id) { $connection = $this->getConnection(); try { $connection->beginTransaction(); $review = Reviews::find($id); if ($review) { //delete review $review->delete(); //delete activities $activities = new Activities(); $activities->deleteActivity(CONSTANTS::REVIEW, $id); //delete comments $comments = new Comments(); $comments->deleteCommentByType(CONSTANTS::REVIEW, $id); //delete likes $like = new Like(); $like->deleteLikes(CONSTANTS::REVIEW, $id); } else { throw new \Exception('No review found'); } $connection->commit(); } catch (\Exception $e) { $connection->rollBack(); throw $e; } }
/** * Add new comment to table * * @param $text * @param $type * @param $type_id * @param $user_id * @throws \Exception */ public function addComment($text, $type, $type_id, $user_id) { $connection = $this->getConnection(); try { $connection->beginTransaction(); $type_object = null; switch ($type) { case CONSTANTS::REVIEW: $type_object = Reviews::find($type_id); if (!$type_object) { //Review not Found throw new \Exception(CONSTANTS::ERROR_CODE_REVIEW_MISSING); } $this->type = CONSTANTS::REVIEW; $comment_type = CONSTANTS::NOTIFICATION_TYPE_COMMENT_ON_REVIEW; break; case CONSTANTS::CHECKIN: $type_object = CheckIns::find($type_id); if (!$type_object) { throw new \Exception(CONSTANTS::ERROR_CODE_CHECKIN_MISSING); } $this->type = CONSTANTS::CHECKIN; $comment_type = CONSTANTS::NOTIFICATION_TYPE_COMMENT_ON_CHECKIN; break; case CONSTANTS::PHOTO: $type_object = Photos::find($type_id); if (!$type_object) { throw new \Exception(CONSTANTS::ERROR_CODE_PHOTO_MISSING); } $this->type = CONSTANTS::PHOTO; $comment_type = CONSTANTS::NOTIFICATION_TYPE_COMMENT_ON_PHOTO; break; default: throw new \Exception(CONSTANTS::ERROR_CODE_INVALID_TYPE); } $this->type_id = $type_id; $this->comment = $text; $this->status = CONSTANTS::STATUS_ENABLED; $this->user_id = $user_id; $this->date_created = date('Y-m-d H:i:s'); $this->save(); $restaurant_id = $type_object['restaurant_id']; $owner_id = $type_object['user_id']; if ($user_id != $owner_id) { $notification_data = new Notification(); $notification_data->addCommentNotification($user_id, $owner_id, $comment_type, $type_id, $restaurant_id); } $connection->commit(); } catch (\Exception $e) { $connection->rollBack(); throw $e; } }
/** * Returns either review, checkin, or bookmark activity based on $type parameter. * * @param $type - activity type. Either 'checkin', 'review', or 'bookmark' * @param $type_id - ID for checkin/review/bookmark activity * @return mixed */ public static function getActivityType($type, $type_id) { $arr = array(); switch ($type) { case CONSTANTS::CHECKIN: $check_in = CheckIns::find($type_id); if ($check_in) { $arr[KeyParser::checkin] = ModelFormatter::checkinFormat($check_in); } else { $arr[KeyParser::checkin] = array(); } $photos = Photos::getByType(CONSTANTS::CHECKIN, $type_id); $arr[KeyParser::photos] = Photos::convertPhotosToArray($photos); break; case CONSTANTS::REVIEW: $review = Reviews::find($type_id); if ($review) { $arr[KeyParser::review] = ModelFormatter::reviewFormat($review); } else { $arr[KeyParser::review] = array(); } $photos = Photos::getByType(CONSTANTS::REVIEW, $type_id); $arr[KeyParser::photos] = Photos::convertPhotosToArray($photos); break; case CONSTANTS::PHOTO_UPLOAD_RESTAURANT: $photos = Photos::where('id', $type_id)->get(); $arr[KeyParser::photos] = Photos::convertPhotosToArray($photos); break; } unset($photos_arr); return $arr; }
/** * Review View * route: reviews/view{id} * * @param $id * @optional ?viewer_id * @return Response */ public function viewAction($id) { $json_return = array(); $review = Reviews::find($id); if (!$review) { return showErrorResponse('Review not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_REVIEW_MISSING); } $restaurant = Restaurants::find($review->restaurant_id); if (!$restaurant) { return showErrorResponse('Restaurant data not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL); } $user = Users::find($review->user_id); $photos = Photos::getByType(CONSTANTS::REVIEW, $review->id); $photos_array = Photos::convertPhotosToArray($photos); $comments = Comments::getByType(CONSTANTS::REVIEW, $review->id); $comments_array = array(); if ($comments) { foreach ($comments as $comment) { $comments_array[] = ModelFormatter::commentFormat($comment); } } $json_return[KeyParser::data] = array(KeyParser::review => ModelFormatter::reviewFormat($review), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::photos => $photos_array, KeyParser::comments => $comments_array); return response()->json($json_return); }