Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $app->get('/category', function (Request $request, Response $response, array $args) {
- $sth = $this->db->prepare("SELECT * FROM category");
- $sth->execute();
- $categories = $sth->fetchAll();
- return $this->response->withJson($categories);
- });
- $app->get('/category/[{category_id}]', function (Request $request, Response $response, array $args) {
- $sth = $this->db->prepare("SELECT category_id, name
- FROM category WHERE category_id=:category_id");
- $sth->bindParam("category_id", $args['category_id']);
- $sth->execute();
- $books = $sth->fetch();
- return $this->response->withJson($books);
- });
- $app->post('/category', function (Request $request, Response $response) {
- $input = $request->getParsedBody();
- if (!$input['name']) {
- return $this->response->withStatus(400)->withJson(array("message" => "Data incomplete."));
- }
- $sql = "INSERT INTO category (name) VALUES (:name)";
- $sth = $this->db->prepare($sql);
- $sth->bindParam("name", $input['name']);
- $sth->execute();
- return $this->response->withStatus(201)->withJson(array("message" => "Category was created."));
- });
- $app->delete('/category/[{category_id}]', function (Request $request, Response $response, array $args) {
- $sth = $this->db->prepare("DELETE FROM category WHERE category_id=:category_id");
- $sth->bindParam("category_id", $args['category_id']);
- $sth->execute();
- $count = $sth->rowCount();
- if ($count != 1) {
- return $this->response->withStatus(404)->withJson(array("message" => "Could not execute statement. Category not found"));
- }
- return $this->response->withJson(array("message" => "Category was deleted."));
- });
- $app->put('/category/[{category_id}]', function (Request $request, Response $response, array $args) {
- $input = $request->getParsedBody();
- if (!$input['name']) {
- return $this->response->withStatus(400)->withJson(array("message" => "Could not execute statement. Data incomplete."));
- }
- $sql = "UPDATE category SET name=:name WHERE category_id=:category_id";
- $sth = $this->db->prepare($sql);
- $sth->bindParam("category_id", $args['category_id']);
- $sth->bindParam("name", $input['name']);
- $sth->execute();
- $count = $sth->rowCount();
- if ($count != 1) {
- return $this->response->withStatus(404)->withJson(array("message" => "Could not execute statement. Category not found"));
- }
- return $this->response->withJson(array("message" => "Category was updated."));
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement