Browse Source

Level 2.3: creating with POST, deleting with DELETE.

Frederic G. MARAND 6 years ago
parent
commit
7ee357079a
5 changed files with 54 additions and 5 deletions
  1. 1 0
      .idea/CodeSchool Backbone 1.iml
  2. 3 0
      package-lock.json
  3. 21 3
      src/Model.php
  4. 12 1
      src/controllers.php
  5. 17 1
      web/data/items.json

+ 1 - 0
.idea/CodeSchool Backbone 1.iml

@@ -3,6 +3,7 @@
   <component name="NewModuleRootManager">
     <content url="file://$MODULE_DIR$">
       <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+      <excludeFolder url="file://$MODULE_DIR$/var/logs" />
     </content>
     <orderEntry type="inheritedJdk" />
     <orderEntry type="sourceFolder" forTests="false" />

+ 3 - 0
package-lock.json

@@ -0,0 +1,3 @@
+{
+  "lockfileVersion": 1
+}

+ 21 - 3
src/Model.php

@@ -3,13 +3,21 @@
 use Symfony\Component\HttpFoundation\Response;
 
 class Model {
-  protected $data;
+  protected $data = [];
   protected $path;
 
   public function __construct(string $path) {
     $this->path = $path;
   }
 
+  /**
+   * Flush the in-memory storage to disk. Do not load it first.
+   */
+  protected function store() {
+    $raw = json_encode($this->data, JSON_PRETTY_PRINT);
+    file_put_contents($this->path, $raw);
+  }
+
   public function load() {
     $raw = file_get_contents($this->path);
     $this->data = json_decode($raw, true);
@@ -17,6 +25,7 @@ class Model {
   }
 
   public function save(array $doc) {
+    $this->load();
     if (empty($doc['id'])) {
       $id = (int) max(array_keys($this->data)) + 1;
       $doc['id'] = $id;
@@ -28,8 +37,17 @@ class Model {
     }
 
     $this->data[$id] = $doc;
-    $raw = json_encode($this->data);
-    file_put_contents($this->path, $raw);
+    $this->store();
     return $created;
   }
+
+  public function delete(int $id) {
+    $this->load();
+    if (!isset($this->data[$id])) {
+      return FALSE;
+    }
+    unset($this->data[$id]);
+    $this->store();
+    return TRUE;
+  }
 }

+ 12 - 1
src/controllers.php

@@ -3,6 +3,7 @@
 use Silex\Application;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\HttpKernelInterface;
 
 const BACKBONE = '/client/index.html';
 
@@ -20,12 +21,22 @@ $app->get('/todos/{id}', function (Application $app, int $id) {
   return $app->json($todo);
 });
 $app->put('/todos/{id}', function (Application $app, Request $request, int $id) {
+  $sub = Request::create('/todos', 'POST', [], [], [], [], $request->getContent());
+  return $app->handle($sub, HttpKernelInterface::SUB_REQUEST);
+});
+$app->post('/todos', function (Application $app, Request $request) {
   /** @var \Model $model */
   $model = $app['model'];
   $data = json_decode($request->getContent(), true);
   $created = $model->save($data);
   return new Response('Saved', $created ? Response::HTTP_CREATED : Response::HTTP_OK);
+})->value('id', NULL);
+$app->delete('/todos/{id}', function (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);
 });
 $app->match('{url}', function ($url) {
-  var_dump($url);
+  return new Response('Nothing to do', Response::HTTP_OK);
 });

+ 17 - 1
web/data/items.json

@@ -1 +1,17 @@
-{"1":{"id":1,"description":"Pick up milk","status":"incomplete"}}
+{
+    "1": {
+        "id": 1,
+        "description": "Remember the milk",
+        "status": "incomplete"
+    },
+    "2": {
+        "status": "unfinished",
+        "description": "second",
+        "id": 2
+    },
+    "3": {
+        "status": "unfinished",
+        "description": "third",
+        "id": 3
+    }
+}