Пример #1
0
 public function createRoutes()
 {
     $entityManager = $this->entityManager;
     $app = $this->app;
     // Get all Shows
     $app->get('/shows/', function () use($entityManager, $app) {
         try {
             $shows = $entityManager->getRepository('Show')->createQueryBuilder('e')->select('e')->getQuery()->getResult(Query::HYDRATE_ARRAY);
             if ($shows) {
                 $app->response->setStatus(200);
                 $app->response()->headers->set('Content-Type', 'application/json');
                 echo json_encode($shows);
             } else {
                 throw new PDOException('No records found.');
             }
         } catch (PDOException $e) {
             $app->response()->setStatus(404);
             echo json_encode(['error' => ['text' => $e->getMessage()]]);
         }
     });
     // Get Show by id
     $app->get('/shows/:id', function ($id) use($entityManager, $app) {
         try {
             $show = $entityManager->getRepository('Show')->createQueryBuilder('a')->select('a')->where('a =:id')->setParameter('id', $id)->getQuery()->getSingleResult(Query::HYDRATE_ARRAY);
             if ($show) {
                 $app->response->setStatus(200);
                 $app->response()->headers->set('Content-Type', 'application/json');
                 echo json_encode($show);
             } else {
                 throw new PDOException('No Show found.');
             }
         } catch (PDOException $e) {
             $app->response()->setStatus(404);
             echo json_encode(['error' => ['text' => $e->getMessage()]]);
         }
     });
     // Create Show
     $app->post('/shows/', function () use($entityManager, $app) {
         $allPostVars = json_decode($app->request->getBody());
         try {
             $show = new Show();
             $show->setName($allPostVars->name);
             $show->setSorder($allPostVars->sorder);
             $show->setCreationDate(date("Y-m-d H:i:s"));
             $show->setLastUpdate(date("Y-m-d H:i:s"));
             $entityManager->persist($show);
             $entityManager->flush();
             $app->response->setStatus(200);
             $app->response()->headers->set('Content-Type', 'application/json');
             echo json_encode(array("status" => "success", "code" => true, $show->getId()));
         } catch (PDOException $e) {
             $app->response()->setStatus(404);
             echo json_encode(['error' => ['text' => $e->getMessage()]]);
         }
     });
     // Update Show
     $app->put('/shows/:id', function ($id) use($entityManager, $app) {
         $allPostVars = json_decode($app->request->getBody());
         try {
             $show = $entityManager->find('Show', $id);
             $show->setName($allPostVars->name);
             $show->setSorder($allPostVars->sorder);
             $show->setLastUpdate(date("Y-m-d H:i:s"));
             $entityManager->flush();
             $app->response->setStatus(200);
             $app->response()->headers->set('Content-Type', 'application/json');
             echo json_encode(array("status" => "success", "code" => true));
         } catch (PDOException $e) {
             $app->response()->setStatus(404);
             echo json_encode(['error' => ['text' => $e->getMessage()]]);
         }
     });
     // Delete Show
     $app->delete('/shows/:id', function ($id) use($entityManager, $app) {
         try {
             $show = $entityManager->find('Show', $id);
             $entityManager->remove($show);
             $entityManager->flush();
             $app->response->setStatus(200);
             $app->response()->headers->set('Content-Type', 'application/json');
             echo json_encode(array("status" => "success", "code" => true));
         } catch (PDOException $e) {
             $app->response()->setStatus(404);
             echo json_encode(['error' => ['text' => $e->getMessage()]]);
         }
     });
 }