Example #1
0
/* global $app */
// note-fields: title, content, trash, create_date, group_id, note_id
// group-fields: id, name, trash
// $model = new ModelController2(new NotePrototype, new GroupPrototype, new JSONController());
// $model = new ModelController2(new NotePrototype, new GroupPrototype, new MySQLController());
// $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) {