Пример #1
0
 /**
  * Review Add
  * route: checkins/add
  *
  * @param Request $request
  * @return Response
  * @throws Exception
  */
 public function addAction(Request $request)
 {
     $data = $request->only('json', 'fileField');
     $data_photos = $data['fileField'];
     $data_json = json_decode(file_get_contents($data['json']), true);
     if (!isset($data_json['CheckIn']) || !isset($data_json['CheckIn']['user_id']) || !isset($data_json['CheckIn']['restaurant_id']) || !isset($data_json['CheckIn']['message'])) {
         $message = "Format should be: {'CheckIn': {'user_id': <int>, 'restaurant_id': <int>, 'message': <string>}, 'Photos': []}";
         return showErrorResponse($message, HTTP_UNPROCESSABLE_ENTITY);
     }
     // check for valid data
     //Check if restaurant ID is existing
     $restaurant = Restaurants::find($data_json['CheckIn']['restaurant_id']);
     if (!$restaurant) {
         return showErrorResponse('Restaurant data not found', HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_GENERAL);
     }
     // Check Ng Words
     $ng_words = NgWord::ngword_filter($data_json['CheckIn']['message']);
     if ($ng_words) {
         $message = "Bad words found: " . implode(', ', $ng_words);
         return showErrorResponse($message, HTTP_ACCEPTED, CONSTANTS::ERROR_CODE_BADWORDS_FOUND);
     }
     // check of Ng Words
     try {
         DB::beginTransaction();
         // Save Checkin Data
         $checkin = new CheckIns();
         $new_checkin = $checkin->addCheckin($data_json['CheckIn']);
         // Save and Upload Photo
         $photos_upload = new Photos();
         $photos_upload->saveUploadedPhotos($data_photos, $data_json['CheckIn'], CONSTANTS::CHECKIN, $new_checkin->id);
         // Send Notification to Followers about the new Checkin
         $followers = Follow::getFollowerUsersAll($new_checkin->user_id);
         $notification = new Notification();
         foreach ($followers as $follower) {
             $notification->addNotificationNewCheckin($new_checkin->user_id, $follower->follower_user_id, $new_checkin->id, $new_checkin->restaurant_id);
         }
         //Add Activity
         $activity = new Activities();
         $new_activity = $activity->addActivity(CONSTANTS::CHECKIN, $new_checkin->id, $new_checkin->user_id, $new_checkin->restaurant_id);
         $photos = Photos::getByType(CONSTANTS::CHECKIN, $new_checkin->id);
         $photos_array = Photos::convertPhotosToArray($photos);
         $user = Users::find($new_checkin->user_id);
         $json_return[KeyParser::data] = array(KeyParser::activity => ModelFormatter::activityFormat($new_activity), KeyParser::user => ModelFormatter::userLongFormat($user), KeyParser::restaurant => ModelFormatter::restaurantLongFormat($restaurant), KeyParser::checkin => ModelFormatter::checkinFormat($new_checkin), KeyParser::photos => $photos_array);
         //DB Commit
         DB::commit();
         return response()->json($json_return);
     } catch (\Exception $e) {
         DB::rollback();
         return showErrorResponse($e->getMessage());
     }
     // end catch
 }