Exemplo n.º 1
0
 public function indexAction()
 {
     /** @var \Zend\Http\Request $request */
     $request = $this->getRequest();
     $id = $this->params()->fromRoute('id', null);
     try {
         $bookEntity = $this->service->getById($id);
         $this->form->get('submit')->setValue('Update');
         if ($request->isPost()) {
             $this->form->setData($request->getPost()->toArray() ?: []);
             if ($this->form->isValid()) {
                 $this->service->update($bookEntity, $this->form->getInputFilter());
                 $this->flashMessenger()->addSuccessMessage('Books saved successfully!');
             } else {
                 $this->flashMessenger()->addErrorMessage('Please fill form correctly');
             }
         } else {
             $data = $this->service->extractEntity($bookEntity);
             $this->form->setData($data);
         }
         return ['form' => $this->form];
     } catch (\Exception $e) {
         $this->flashMessenger()->addErrorMessage($e->getMessage());
         return $this->redirect()->toRoute('library/books');
     }
 }
Exemplo n.º 2
0
 public function indexAction()
 {
     /**
      * @var Request  $request
      * @var Response $response
      */
     $request = $this->getRequest();
     $response = $this->getResponse();
     $id = $this->params()->fromRoute('id', null);
     try {
         $bookEntity = $this->service->getById($id);
         $this->filter->setData($request->getPost()->toArray());
         if ($this->filter->isValid()) {
             $bookEntity = $this->service->update($bookEntity, $this->filter);
             return new JsonModel($this->service->extractEntity($bookEntity));
         } else {
             $messages = $this->filter->getMessages();
             $response->setStatusCode(Response::STATUS_CODE_400);
             return new JsonModel(['error' => ['messages' => $messages]]);
         }
     } catch (EntityNotFoundException $e) {
         throw new Exception\NotFoundException();
     } catch (\PDOException $e) {
         throw new Exception\PDOServiceUnavailableException();
     }
 }
Exemplo n.º 3
0
 public function indexAction()
 {
     $id = $this->params()->fromRoute('id', null);
     try {
         $bookEntity = $this->service->getById($id);
         $data = $this->service->extractEntity($bookEntity);
         return new JsonModel($data);
     } catch (EntityNotFoundException $e) {
         throw new Exception\NotFoundException();
     } catch (\PDOException $e) {
         throw new Exception\PDOServiceUnavailableException();
     }
 }
Exemplo n.º 4
0
 public function indexAction()
 {
     $id = $this->params()->fromRoute('id', null);
     try {
         $bookEntity = $this->service->getById($id);
         $this->service->delete($bookEntity);
         return new JsonModel(['data' => "Books with id {$id} has been deleted"]);
     } catch (EntityNotFoundException $e) {
         throw new Exception\NotFoundException();
     } catch (\PDOException $e) {
         throw new Exception\PDOServiceUnavailableException();
     }
 }
Exemplo n.º 5
0
 public function testExtractEntity()
 {
     $bookEntity = $this->bookEntityProvider->getBookEntityWithRandomData();
     $data = $this->bookEntityProvider->getDataFromBookEntity($bookEntity);
     $this->bookRepositoryMock->expects($this->once())->method('extract')->with($bookEntity)->will($this->returnValue($data));
     $result = $this->testedObj->extractEntity($bookEntity);
     $this->assertSame($data, $result);
 }
Exemplo n.º 6
0
 public function indexAction()
 {
     /**
      * @var Request  $request
      * @var Response $response
      */
     $request = $this->getRequest();
     $response = $this->getResponse();
     $this->filter->setData($request->getPost()->toArray());
     if (!$this->filter->isValid()) {
         $messages = $this->filter->getMessages();
         $response->setStatusCode(Response::STATUS_CODE_400);
         return new JsonModel(['error' => ['messages' => $messages]]);
     }
     try {
         $bookEntity = $this->service->create($this->filter);
         $response->setStatusCode(Response::STATUS_CODE_201);
         return new JsonModel($this->service->extractEntity($bookEntity));
     } catch (\PDOException $e) {
         throw new Exception\PDOServiceUnavailableException();
     }
 }