Guest User

index.php

a guest
Mar 21st, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.20 KB | None | 0 0
  1. <?php
  2.  
  3. use Phalcon\Mvc\Micro;
  4. use Phalcon\Http\Response;
  5. use Phalcon\Loader;
  6. use Phalcon\Di\FactoryDefault;
  7. use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql;
  8.  
  9. $app = new Micro();
  10.  
  11. // Use Loader() to autoload our model
  12. $loader = new Loader();
  13.  
  14. $loader->registerDirs(
  15.     array(
  16.         __DIR__ . '/models/'
  17.     )
  18. )->register();
  19.  
  20. $di = new FactoryDefault();
  21.  
  22. // Set up the database service
  23. $di->set('db', function () {
  24.     return new PdoMysql(
  25.         array(
  26.             "host"     => "localhost",
  27.             "username" => "root",
  28.             "password" => "",
  29.             "dbname"   => "robotics"
  30.         )
  31.     );
  32. });
  33.  
  34. // Create and bind the DI to the application
  35. $app = new Micro($di);
  36.  
  37. $app->get('/',function(){
  38.     echo "Pagina non utilizzabile";
  39. });
  40. $app->get('/api',function(){
  41.     echo "Pagina non utilizzabile";
  42. });
  43.  
  44. // Retrieves all robots
  45. $app->get('/api/robots', function () use ($app) {
  46.  
  47.     $phql = "SELECT * FROM Robots ORDER BY name";
  48.     $robots = $app->modelsManager->executeQuery($phql);
  49.  
  50.     $data = array();
  51.     foreach ($robots as $robot) {
  52.         $data[] = array(
  53.             'id'   => $robot->id,
  54.             'name' => $robot->name
  55.         );
  56.     }
  57.  
  58.     echo json_encode($data);
  59. });
  60.  
  61. // Searches for robots with $name in their name
  62. $app->get('/api/robots/search/{name}', function ($name) use ($app) {
  63.  
  64.     $phql = "SELECT * FROM Robots WHERE name LIKE :name: ORDER BY name";
  65.     $robots = $app->modelsManager->executeQuery(
  66.         $phql,
  67.         array(
  68.             'name' => '%' . $name . '%'
  69.         )
  70.     );
  71.  
  72.     $data = array();
  73.     foreach ($robots as $robot) {
  74.         $data[] = array(
  75.             'id'   => $robot->id,
  76.             'name' => $robot->name
  77.         );
  78.     }
  79.  
  80.     echo json_encode($data);
  81. });
  82.  
  83. // Retrieves robots based on primary key
  84. $app->get('/api/robots/{id:[0-9]+}', function ($id) use ($app) {
  85.  
  86.     $phql = "SELECT * FROM Robots WHERE id = :id:";
  87.     $robot = $app->modelsManager->executeQuery($phql, array(
  88.         'id' => $id
  89.     ))->getFirst();
  90.  
  91.     // Create a response
  92.     $response = new Response();
  93.  
  94.     if ($robot == false) {
  95.         $response->setJsonContent(
  96.             array(
  97.                 'status' => 'NOT-FOUND'
  98.             )
  99.         );
  100.     } else {
  101.         $response->setJsonContent(
  102.             array(
  103.                 'status' => 'FOUND',
  104.                 'data'   => array(
  105.                     'id'   => $robot->id,
  106.                     'name' => $robot->name
  107.                 )
  108.             )
  109.         );
  110.     }
  111.  
  112.     return $response;
  113. });
  114.  
  115. $app->post('/api/contact', function () use ($app) {
  116.     echo "Success!";
  117. });
  118.  
  119. // ----------------------------------- //
  120. // ----- Here the problems!!!!! ------ //
  121. // ----------------------------------- //
  122.  
  123. // Adds a new robot
  124. $app->post('/api/robots', function () use ($app) {
  125.  
  126.     //$robot = $app->request->getJsonRawBody();
  127.    
  128.     $temp = file_get_contents('php://input');
  129.     $robot = json_decode($temp);
  130.    
  131.     echo array_values($robot);
  132.    
  133.  
  134.     $phql = "INSERT INTO Robots (name, type, year) VALUES (:name:, :type:, :year:)";
  135.  
  136.     $status = $app->modelsManager->executeQuery($phql, array(
  137.         'name' => $robot->name,
  138.         'type' => $robot->type,
  139.         'year' => $robot->year
  140.     ));
  141.  
  142.     // Create a response
  143.     $response = new Response();
  144.  
  145.     // Check if the insertion was successful
  146.     if ($status->success() == true) {
  147.  
  148.         // Change the HTTP status
  149.         $response->setStatusCode(201, "Created");
  150.  
  151.         $robot->id = $status->getModel()->id;
  152.  
  153.         $response->setJsonContent(
  154.             array(
  155.                 'status' => 'OK',
  156.                 'data'   => $robot
  157.             )
  158.         );
  159.  
  160.     } else {
  161.  
  162.         // Change the HTTP status
  163.         $response->setStatusCode(409, "Conflict");
  164.  
  165.         // Send errors to the client
  166.         $errors = array();
  167.         foreach ($status->getMessages() as $message) {
  168.             $errors[] = $message->getMessage();
  169.         }
  170.  
  171.         $response->setJsonContent(
  172.             array(
  173.                 'status'   => 'ERROR',
  174.                 'messages' => $errors
  175.             )
  176.         );
  177.     }
  178.  
  179.     return $response;
  180. });
  181.  
  182. // Updates robots based on primary key
  183. $app->put('/api/robots/{id:[0-9]+}', function ($id) use ($app) {
  184.  
  185.     $robot = $app->request->getJsonRawBody();
  186.  
  187.     $phql = "UPDATE Robots SET name = :name:, type = :type:, year = :year: WHERE id = :id:";
  188.     $status = $app->modelsManager->executeQuery($phql, array(
  189.         'id' => $id,
  190.         'name' => $robot->name,
  191.         'type' => $robot->type,
  192.         'year' => $robot->year
  193.     ));
  194.  
  195.     // Create a response
  196.     $response = new Response();
  197.  
  198.     // Check if the insertion was successful
  199.     if ($status->success() == true) {
  200.         $response->setJsonContent(
  201.             array(
  202.                 'status' => 'OK'
  203.             )
  204.         );
  205.     } else {
  206.  
  207.         // Change the HTTP status
  208.         $response->setStatusCode(409, "Conflict");
  209.  
  210.         $errors = array();
  211.         foreach ($status->getMessages() as $message) {
  212.             $errors[] = $message->getMessage();
  213.         }
  214.  
  215.         $response->setJsonContent(
  216.             array(
  217.                 'status'   => 'ERROR',
  218.                 'messages' => $errors
  219.             )
  220.         );
  221.     }
  222.  
  223.     return $response;
  224. });
  225.  
  226. // Deletes robots based on primary key
  227. $app->delete('/api/robots/{id:[0-9]+}', function ($id) use ($app) {
  228.  
  229.     $phql = "DELETE FROM Robots WHERE id = :id:";
  230.     $status = $app->modelsManager->executeQuery($phql, array(
  231.         'id' => $id
  232.     ));
  233.  
  234.     // Create a response
  235.     $response = new Response();
  236.  
  237.     if ($status->success() == true) {
  238.         $response->setJsonContent(
  239.             array(
  240.                 'status' => 'OK'
  241.             )
  242.         );
  243.     } else {
  244.  
  245.         // Change the HTTP status
  246.         $response->setStatusCode(409, "Conflict");
  247.  
  248.         $errors = array();
  249.         foreach ($status->getMessages() as $message) {
  250.             $errors[] = $message->getMessage();
  251.         }
  252.  
  253.         $response->setJsonContent(
  254.             array(
  255.                 'status'   => 'ERROR',
  256.                 'messages' => $errors
  257.             )
  258.         );
  259.     }
  260.  
  261.     return $response;
  262. });
  263.  
  264. $app->handle();
  265.  
  266. ?>
Add Comment
Please, Sign In to add comment