Advertisement
bartek27210

da

Jan 27th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1.     $app->get('/category', function (Request $request, Response $response, array $args) {
  2.         $sth = $this->db->prepare("SELECT * FROM category");
  3.         $sth->execute();
  4.         $categories = $sth->fetchAll();
  5.         return $this->response->withJson($categories);
  6.     });
  7.  
  8.     $app->get('/category/[{category_id}]', function (Request $request, Response $response, array $args) {
  9.         $sth = $this->db->prepare("SELECT category_id, name
  10.            FROM category WHERE category_id=:category_id");
  11.         $sth->bindParam("category_id", $args['category_id']);
  12.         $sth->execute();
  13.         $books = $sth->fetch();
  14.  
  15.         return $this->response->withJson($books);
  16.     });
  17.  
  18.  
  19.     $app->post('/category', function (Request $request, Response $response) {
  20.         $input = $request->getParsedBody();
  21.         if (!$input['name']) {
  22.             return $this->response->withStatus(400)->withJson(array("message" => "Data incomplete."));
  23.         }
  24.  
  25.         $sql = "INSERT INTO category (name) VALUES (:name)";
  26.         $sth = $this->db->prepare($sql);
  27.         $sth->bindParam("name", $input['name']);
  28.         $sth->execute();
  29.         return $this->response->withStatus(201)->withJson(array("message" => "Category was created."));
  30.     });
  31.  
  32.     $app->delete('/category/[{category_id}]', function (Request $request, Response $response, array $args) {
  33.         $sth = $this->db->prepare("DELETE FROM category WHERE category_id=:category_id");
  34.         $sth->bindParam("category_id", $args['category_id']);
  35.         $sth->execute();
  36.  
  37.         $count = $sth->rowCount();
  38.         if ($count != 1) {
  39.             return $this->response->withStatus(404)->withJson(array("message" => "Could not execute statement. Category not found"));
  40.         }
  41.         return $this->response->withJson(array("message" => "Category was deleted."));
  42.     });
  43.  
  44.     $app->put('/category/[{category_id}]', function (Request $request, Response $response, array $args) {
  45.         $input = $request->getParsedBody();
  46.         if (!$input['name']) {
  47.             return $this->response->withStatus(400)->withJson(array("message" => "Could not execute statement. Data incomplete."));
  48.         }
  49.  
  50.         $sql = "UPDATE category SET name=:name WHERE category_id=:category_id";
  51.         $sth = $this->db->prepare($sql);
  52.         $sth->bindParam("category_id", $args['category_id']);
  53.         $sth->bindParam("name", $input['name']);
  54.         $sth->execute();
  55.  
  56.         $count = $sth->rowCount();
  57.         if ($count != 1) {
  58.             return $this->response->withStatus(404)->withJson(array("message" => "Could not execute statement. Category not found"));
  59.         }
  60.         return $this->response->withJson(array("message" => "Category was updated."));
  61.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement