Example #1
0
// $model = new ModelController(new JSONController(), new DataHelper(), new Validator());
$model = new ModelController(new MySQLController(), new DataHelper(), new Validator());
$app->group('/api', function () use($app, $model) {
    $app->group('/todos', function () use($app, $model) {
        $app->get('/', function ($req, $res, $arg) use($model) {
            $res->getBody()->write(json_encode($model->showNotes()));
            $res = $res->withHeader('Content-type', 'application/json');
            $res = $res->withStatus(200);
        });
        $app->get('/{id:[0-9]+}', function ($req, $res, $arg) use($model) {
            $res->getBody()->write(json_encode($model->showNote($arg['id'])));
            $res = $res->withHeader('Content-type', 'application/json');
            $res = $res->withStatus(200);
        });
        $app->post('/', function ($req, $res, $arg) use($model) {
            $status = $model->createNote($req->getParams());
            $res->getBody()->write(json_encode(array('status' => $status == 0 ? false : true)));
            $res = $res->withHeader('Content-type', 'application/json');
            $res = $res->withStatus(200);
        });
        $app->put('/{id:[0-9]+}', function ($req, $res, $arg) use($model) {
            $res->getBody()->write(json_encode(array('status' => $model->editNote(json_decode($req->getBody(), true)))));
            $res = $res->withHeader('Content-type', 'application/json');
            $res = $res->withStatus(200);
        });
        $app->delete('/{id:[0-9]+}', function ($req, $res, $arg) use($model) {
            $res->getBody()->write(json_encode(array('status' => $model->deleteNote($arg['id']))));
            $res = $res->withHeader('Content-type', 'application/json');
            $res = $res->withStatus(200);
        });
        $app->get('/{id:[0-9]+}/group/', function ($req, $res, $arg) use($model) {