Пример #1
0
 protected function proceed()
 {
     $srv = new CustomersService();
     switch ($this->action) {
         case 'get':
             $this->succeed($srv->get($this->params['id']));
             break;
         case 'getAll':
             $this->succeed($srv->getAll());
             break;
         case 'addPrepaid':
             $this->succeed($srv->addPrepaid($this->params['id'], $this->params['amount']));
             break;
         case 'getTop':
             $this->succeed($srv->getTop($this->getParam("limit")));
             break;
         case 'save':
             // Convert single customer to array for consistency
             if ($this->isParamSet('customers')) {
                 $json = json_decode($this->params['customers']);
             } else {
                 $json = array(json_decode($this->params['customer']));
             }
             $srv = new CustomersService();
             $result = array();
             // Begin transaction
             $pdo = PDOBuilder::getPDO();
             if (!$pdo->beginTransaction()) {
                 $this->fail(APIError::$ERR_GENERIC);
                 break;
             }
             foreach ($json as $customer) {
                 if (isset($customer->id) && $customer->id !== null) {
                     // Edit
                     if (!$srv->update($customer)) {
                         // Error, rollback
                         $pdo->rollback();
                         $this->fail(APIError::$ERR_GENERIC);
                         return;
                     } else {
                         $result[] = $customer->id;
                     }
                 } else {
                     // Create
                     $id = $srv->create($customer);
                     if ($id !== false) {
                         $result[] = $id;
                     } else {
                         // Error, rollback
                         $pdo->rollback();
                         $this->fail(APIError::$ERR_GENERIC);
                         return;
                     }
                 }
             }
             // Success, commit
             if ($pdo->commit()) {
                 $this->succeed(array("saved" => $result));
             } else {
                 $this->fail(APIError::$ERR_GENERIC);
             }
             break;
     }
 }