123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- namespace Controllers;
- use Silex\Application;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\HttpKernelInterface;
- class TodosController {
- public function index(Application $app) {
- /** @var \Model $model */
- $model = $app['model'];
- $todos = $model->load();
- return $app->json(array_values($todos));
- }
- public function getLevel1(Application $app, $id) {
- /** @var \Model $model */
- $model = $app['model'];
- $todos = $model->load();
- $todo = $todos[$id] ?? NULL;
- return $app->json($todo);
- }
- public function getLevel2(Application $app, $id) {
- /** @var \Model $model */
- $model = $app['model'];
- $todos = $model->load();
- $todo = $todos[$id]
- ? ['todo' => $todos[$id]]
- : NULL;
- if ($todo) {
- $todo['todo']['desc'] = $todo['todo']['description'];
- unset($todo['todo']['description']);
- }
- return $app->json($todo);
- }
- public function putLevel1(Application $app, Request $request, int $id) {
- $sub = Request::create('/todos', 'POST', [], [], [], [], $request->getContent());
- return $app->handle($sub, HttpKernelInterface::SUB_REQUEST);
- }
- public function putLevel2(Application $app, Request $request, int $id) {
- $sub = Request::create('/todos', 'POST', [], [], [], [], $request->getContent());
- return $app->handle($sub, HttpKernelInterface::SUB_REQUEST);
- }
- public function post(Application $app, Request $request) {
- /** @var \Model $model */
- $model = $app['model'];
- $data = json_decode($request->getContent(), true);
- $data = $data['todo'];
- $created = $model->save($data);
- return new Response('Saved', $created ? Response::HTTP_CREATED : Response::HTTP_OK);
- }
- public function delete(Application $app, int $id) {
- /** @var \Model $model */
- $model = $app['model'];
- $deleted = $model->delete($id);
- return new Response(
- $deleted ? 'Deleted' : 'It was not there anyway',
- Response::HTTP_OK
- );
- }
- }
|