Beispiel #1
0
 /**
  * Review Edit
  * route: reviews/edit
  *
  * @param Request $request
  * @param $id
  * @return Response
  * @throws Exception
  */
 public function editAction(Request $request, $id)
 {
     /* Multipart Procedure
        $data = $request->only('json');
        $data_json = json_decode(file_get_contents($data['json']), true);
        */
     $data_json['Review'] = $request->json()->get('Review');
     if (!isset($data_json['Review']) || !isset($data_json['Review']['user_id']) || !isset($data_json['Review']['restaurant_id']) || !isset($data_json['Review']['title']) || !isset($data_json['Review']['text']) || !isset($data_json['Review']['rating'])) {
         $message = "Format {'Review': {'user_id': <int>, 'title': <string>, 'text': <string>, 'price': <int>, 'date_visited': <string>, 'rating': <double>, 'restaurant_id': <int>}}";
         return showErrorResponse($message);
     }
     // check fo valid data
     if (!Restaurants::isExists($data_json['Review']['restaurant_id'])) {
         return showErrorResponse("Restaurant data not found", HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL);
     }
     if (!in_array($data_json['Review']['rating'], array('0.5', '1.0', '1', '1.5', '2.0', '2', '2.5', '3.0', '3', '3.5', '4.0', '4', '4.5', '5.0', '5'))) {
         $message = "Rating must not be 0. Any of 0.5, 1.0, 1.5, ..., 5.0";
         return showErrorResponse($message);
     }
     // check for rating value
     // Check Ng Words
     $ng_words = NgWord::ngword_filter($data_json['Review']['text']);
     if ($ng_words) {
         $message = "Bad word(s) found: " . implode(',', $ng_words);
         return showErrorResponse($message, HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_BADWORDS_FOUND);
     }
     // check of Ng Words
     try {
         // Edit Review Data
         $review = new Reviews();
         $edit_review = $review->editReview($id, $data_json['Review']);
         $photos = Photos::getByType(CONSTANTS::REVIEW, $edit_review->id);
         $photos_array = Photos::convertPhotosToArray($photos);
         $restaurant = Restaurants::find($edit_review->restaurant_id);
         $user = Users::find($edit_review->user_id);
         $json_return[KeyParser::data] = array(KeyParser::review => ModelFormatter::reviewFormat($edit_review), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::photos => $photos_array);
         return response()->json($json_return);
     } catch (\Exception $e) {
         return showErrorResponse($e->getMessage());
     }
     // end catch
 }