require "database.php"; error_reporting(E_ALL); ini_set('display_errors', 'on'); set_time_limit(0); use Respect\Rest\Router; $router = new Router(); $router->get('/', function () { require "views/default.php"; }); $router->any('/devs/*', '\\Beeblebrox3\\DevShop\\Controllers\\Devs'); $router->any('/cart', '\\Beeblebrox3\\DevShop\\Controllers\\Cart'); $router->post('/cart/apply-cupom', function () { $controller = new \Beeblebrox3\DevShop\Controllers\Cart(); $controller->applyCupom(); }); $router->post('/cart/buy/', function () { $controller = new Beeblebrox3\DevShop\Controllers\Cart(); $controller->buy(); }); $router->delete('/cart/*', '\\Beeblebrox3\\DevShop\\Controllers\\Cart'); $router->post('/config', function () { $controller = new Beeblebrox3\DevShop\Controllers\Config(); $controller->import(); }); $router->exceptionRoute('InvalidArgumentException', function (InvalidArgumentException $e) { return 'Sorry, this error happened: ' . $e->getMessage(); }); $router->errorRoute(function ($a = null) { print_r($a); });
$book = $db->books()->where('id', $id); if ($data = $book->fetch()) { return array('id' => $data['id'], 'title' => $data['title'], 'author' => $data['author'], 'summary' => $data['summary']); } else { return array('status' => false, 'message' => sprintf('Book ID %s does not exist', $id)); } })->accept(array('application/json' => 'json_encode')); $r3->post('/book', function () use($db) { $book = array('title' => $_POST['title'], 'author' => $_POST['author'], 'summary' => $_POST['summary']); $result = $db->books->insert($book); return array('id' => $result['id']); })->accept(array('application/json' => 'json_encode')); $r3->put('/book/*', function ($id) use($db) { $book = $db->books()->where('id', $id); if ($book->fetch()) { parse_str(file_get_contents('php://input'), $put); $result = $book->update($put); return array('status' => (bool) $result, 'message' => 'Book updated successfully'); } else { return array('status' => false, 'message' => sprintf('Book id %s does not exist', $id)); } })->accept(array('application/json' => 'json_encode')); $r3->delete('/book/*', function ($id) use($db) { $book = $db->books()->where('id', $id); if ($book->fetch()) { $result = $book->delete(); return array('status' => true, 'message' => 'Book deleted successfully'); } else { return array('status' => false, 'message' => sprintf('Book id %s does not exist', $id)); } })->accept(array('application/json' => 'json_encode'));