예제 #1
0
        $comments[] = ['name' => $properties['name']->getStringValue(), 'body' => $properties['body']->getStringValue(), 'created' => $properties['created']->getDateTimeValue()];
    }
    return $twig->render('datastore.html.twig', ['project' => $projectId, 'comments' => $comments]);
})->bind('home');
$app->post('/store', function (Request $request) use($app) {
    /** @var Google_Client $client */
    $client = $app['google_client'];
    /** @var Symfony\Component\Routing\Generator\UrlGenerator $urlgen */
    $urlgen = $app['url_generator'];
    // pull the comment from the post body
    $name = $request->get('name');
    $body = $request->get('body');
    if (empty($name) || empty($body)) {
        $error = 'Invalid Request: "name" and "body" are required';
        return new Response($error, 400);
    }
    $util = new DatastoreHelper();
    // use our project ID for our dataset ID
    $datasetId = $app['project_id'];
    // create a datastore service object to call the APIs
    $datastore = new Google_Service_Datastore($client);
    // generate a unique key to store this item using the APIs
    $keyRequest = $util->createUniqueKeyRequest($datasetId);
    $uniqueId = $datastore->datasets->allocateIds($datasetId, $keyRequest);
    $key = $uniqueId->getKeys()[0];
    // submit the changes to datastore
    $request = $util->createCommentRequest($key, $name, $body);
    $result = $datastore->datasets->commit($datasetId, $request);
    return new Response('', 301, ['Location' => $urlgen->generate('home')]);
})->bind('store');
return $app;