Example #1
0
 /**
  * @Route("/user/restaurants/list/")
  * 
  * List of a user's restaurants
  */
 public function restaurantlistAction()
 {
     $response = new JsonResponse();
     // Get POST
     $datas = file_get_contents('php://input');
     $requestDatas = json_decode($datas);
     if (!empty($requestDatas->userFacebookID)) {
         $manager = $this->getDoctrine()->getManager();
         // Get current user
         $userRepo = $manager->getRepository('TycoonApiBundle:User');
         $currentUser = $userRepo->findOneByFacebookId($requestDatas->userFacebookID);
         if (empty($currentUser)) {
             $currentUser = new User();
             $currentUser->setFacebookId($requestDatas->userFacebookID);
             $manager->persist($currentUser);
         }
         // Load restaurants
         $userRestaurants = $currentUser->getUserRestaurants();
         $restaurantsList = array();
         foreach ($userRestaurants as $userRestaurant) {
             // Load restaurant
             $currentRestaurant = $userRestaurant->getRestaurant();
             // Refresh restaurant
             $currentPostcode = null;
             foreach ($currentRestaurant->getPostcodes() as $postcode) {
                 $currentPostcode = $postcode;
                 break;
             }
             if (empty($currentPostcode->getRefreshedAt()) || $currentPostcode->getRefreshedAt()->format('Y-m-d') <= date('Y-m-d', time() - $currentRestaurant->getRefreshingTime())) {
                 // Last refreshed more than 1 day ago: call JustEat API to refresh datas
                 $this->_refreshPostcode($currentPostcode);
                 $manager->refresh($currentRestaurant);
             }
             $cuisines = array();
             foreach ($currentRestaurant->getCuisines() as $cuisine) {
                 $cuisines[] = array('cuisineID' => $cuisine->getId(), 'name' => $cuisine->getName());
             }
             $restaurantPrice = $currentRestaurant->getPrice();
             $discountedPrice = $this->_getDiscountedPrice($currentUser, $currentRestaurant);
             $restaurantsList[] = array('restaurantID' => $currentRestaurant->getId(), 'name' => $currentRestaurant->getName(), 'address' => $currentRestaurant->getAddress() . ' ' . $currentRestaurant->getCity(), 'logo' => $currentRestaurant->getLogo(), 'url' => $currentRestaurant->getUrl(), 'latitude' => $currentRestaurant->getLatitude(), 'longitude' => $currentRestaurant->getLongitude(), 'price' => $restaurantPrice, 'discountedPrice' => $discountedPrice, 'isOwner' => true, 'cuisines' => $cuisines);
             $manager->persist($currentRestaurant);
             $manager->persist($currentUser);
         }
         $manager->flush();
         $response->setData(array('restaurants' => $restaurantsList));
     } else {
         $response->setData(array('error' => 'Please send your Facebook ID.'));
     }
     return $response;
 }
 /**
  * @Route("/restaurants/details/")
  * 
  * Restaurant's details
  */
 public function detailsAction()
 {
     $response = new JsonResponse();
     // Get POST
     $datas = file_get_contents('php://input');
     $requestDatas = json_decode($datas);
     /**
             echo 'REMOVE THIS TEST'."\n";
             $requestDatas = array(
                 'restaurantID' => '343',
                 'userFacebookID' => '1100001103256836'
             );
             $requestDatas = (object)$requestDatas;
             /**/
     if (!empty($requestDatas->userFacebookID) && !empty($requestDatas->restaurantID)) {
         $manager = $this->getDoctrine()->getManager();
         // Get current user
         $userRepo = $manager->getRepository('TycoonApiBundle:User');
         $currentUser = $userRepo->findOneByFacebookId($requestDatas->userFacebookID);
         if (empty($currentUser)) {
             $currentUser = new User();
             $currentUser->setFacebookId($requestDatas->userFacebookID);
             $manager->persist($currentUser);
         }
         // Load restaurant
         $restaurantRepo = $manager->getRepository('TycoonApiBundle:Restaurant');
         $currentRestaurant = $restaurantRepo->find($requestDatas->restaurantID);
         if (!empty($currentRestaurant)) {
             // Refresh restaurant
             $currentPostcode = null;
             foreach ($currentRestaurant->getPostcodes() as $postcode) {
                 $currentPostcode = $postcode;
                 break;
             }
             if (empty($currentPostcode->getRefreshedAt()) || $currentPostcode->getRefreshedAt()->format('Y-m-d') <= date('Y-m-d', time() - $currentRestaurant->getRefreshingTime())) {
                 // Last refreshed more than 30 days ago: call JustEat API to refresh datas
                 $currentPostcode = $this->_refreshPostcode($currentPostcode);
                 $manager->refresh($currentRestaurant);
             }
             $cuisines = array();
             foreach ($currentRestaurant->getCuisines() as $cuisine) {
                 $cuisines[] = array('cuisineID' => $cuisine->getId(), 'name' => $cuisine->getName());
             }
             $isOwner = $currentUser->ownRestaurant($currentRestaurant);
             $restaurantPrice = $currentRestaurant->getPrice();
             $discountedPrice = $this->_getDiscountedPrice($currentUser, $currentRestaurant);
             $restaurant = array('restaurantID' => $currentRestaurant->getId(), 'name' => $currentRestaurant->getName(), 'address' => $currentRestaurant->getAddress() . ' ' . $currentRestaurant->getCity(), 'logo' => $currentRestaurant->getLogo(), 'url' => $currentRestaurant->getUrl(), 'latitude' => $currentRestaurant->getLatitude(), 'longitude' => $currentRestaurant->getLongitude(), 'price' => $restaurantPrice, 'discountedPrice' => $discountedPrice, 'isOwner' => $isOwner, 'cuisines' => $cuisines);
             if ($isOwner) {
                 // Load userRestaurant
                 $userRestaurantRepo = $manager->getRepository('TycoonApiBundle:UserRestaurant');
                 $userRestaurant = $userRestaurantRepo->getByUserAndRestaurant($currentUser, $currentRestaurant);
                 $restaurant['initialPrice'] = $userRestaurant->getInitialPrice();
                 $restaurant['lastLogin'] = $userRestaurant->getLastConnectionAt()->format('Y-m-d');
                 $restaurant['profitSinceLastLogin'] = $userRestaurant->getProfit();
                 $restaurant['costSinceLastLogin'] = $userRestaurant->getCost();
                 $restaurant['totalProfit'] = $userRestaurant->getTotalProfit() - $userRestaurant->getTotalCost();
                 $restaurant['money'] = $currentUser->getMoney();
                 $manager->persist($userRestaurant);
                 $manager->persist($currentUser);
             } else {
                 $restaurant['estimatedProfit'] = $currentRestaurant->getEstimatedProfit();
                 $restaurant['estimatedCost'] = $currentRestaurant->getEstimatedCost();
             }
             $manager->persist($currentRestaurant);
             $manager->flush();
             $response->setData(array('restaurant' => $restaurant));
         } else {
             $response->setData(array('error' => 'This restaurant does not exist.'));
         }
     } else {
         $response->setData(array('error' => 'Please send your Facebook ID.'));
     }
     return $response;
 }