forked from deblan/gist
API: create
This commit is contained in:
parent
5fb853f3d5
commit
0d0403e242
|
@ -29,3 +29,7 @@ download:
|
||||||
revisions:
|
revisions:
|
||||||
path: /revs/{gist}
|
path: /revs/{gist}
|
||||||
defaults: {_controller: Gist\Controller\ViewController::revisionsAction, _locale: en}
|
defaults: {_controller: Gist\Controller\ViewController::revisionsAction, _locale: en}
|
||||||
|
|
||||||
|
api_create:
|
||||||
|
path: /api/create
|
||||||
|
defaults: {_controller: Gist\Controller\ApiController::createAction, _locale: en}
|
||||||
|
|
74
src/Gist/Controller/ApiController.php
Normal file
74
src/Gist/Controller/ApiController.php
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Gist\Controller;
|
||||||
|
|
||||||
|
use Silex\Application;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Gist\Model\Gist;
|
||||||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
use Gist\Form\ApiCreateGistForm;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ApiController
|
||||||
|
* @author Simon Vieille <simon@deblan.fr>
|
||||||
|
*/
|
||||||
|
class ApiController extends Controller
|
||||||
|
{
|
||||||
|
public function createAction(Request $request, Application $app)
|
||||||
|
{
|
||||||
|
if (false === $request->isMethod('post')) {
|
||||||
|
return $this->invalidMethodResponse('POST method is required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$form = new ApiCreateGistForm(
|
||||||
|
$app['form.factory'],
|
||||||
|
$app['translator'],
|
||||||
|
[],
|
||||||
|
['csrf_protection' => false]
|
||||||
|
);
|
||||||
|
|
||||||
|
$form = $form->build()->getForm();
|
||||||
|
|
||||||
|
$form->submit($request);
|
||||||
|
|
||||||
|
if ($form->isValid()) {
|
||||||
|
$gist = $app['gist']->create(new Gist(), $form->getData());
|
||||||
|
$gist->setCipher(false)->save();
|
||||||
|
|
||||||
|
$history = $app['gist']->getHistory($gist);
|
||||||
|
|
||||||
|
return new JsonResponse(array(
|
||||||
|
'url' => $request->getSchemeAndHttpHost().$app['url_generator']->generate(
|
||||||
|
'view',
|
||||||
|
array(
|
||||||
|
'gist' => $gist->getFile(),
|
||||||
|
'commit' => array_pop($history)['commit'],
|
||||||
|
)
|
||||||
|
),
|
||||||
|
'gist' => $gist->toArray(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->invalidRequestResponse('Invalid field(s)');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function invalidMethodResponse($message = null)
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'error' => 'Method Not Allowed',
|
||||||
|
'message' => $message,
|
||||||
|
];
|
||||||
|
|
||||||
|
return new JsonResponse($data, 405);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function invalidRequestResponse($message = null)
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'error' => 'Bad request',
|
||||||
|
'message' => $message,
|
||||||
|
];
|
||||||
|
|
||||||
|
return new JsonResponse($data, 400);
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,11 +15,11 @@ abstract class AbstractForm
|
||||||
|
|
||||||
protected $translator;
|
protected $translator;
|
||||||
|
|
||||||
public function __construct(FormFactory $formFactory, Translator $translator, array $data = array())
|
public function __construct(FormFactory $formFactory, Translator $translator, array $data = array(), $formFactoryOptions = array())
|
||||||
{
|
{
|
||||||
$this->translator = $translator;
|
$this->translator = $translator;
|
||||||
|
|
||||||
$this->builder = $formFactory->createBuilder('form', $data);
|
$this->builder = $formFactory->createBuilder('form', $data, $formFactoryOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getForm()
|
public function getForm()
|
||||||
|
|
19
src/Gist/Form/ApiCreateGistForm.php
Normal file
19
src/Gist/Form/ApiCreateGistForm.php
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Gist\Form;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class CreateGistForm
|
||||||
|
* @author Simon Vieille <simon@deblan.fr>
|
||||||
|
*/
|
||||||
|
class ApiCreateGistForm extends CreateGistForm
|
||||||
|
{
|
||||||
|
public function build(array $options = array())
|
||||||
|
{
|
||||||
|
parent::build($options);
|
||||||
|
|
||||||
|
$this->builder->remove('cipher');
|
||||||
|
|
||||||
|
return $this->builder;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue