From 4103687c57c2c6d03e0aa74345ec012b1a5997e9 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Mon, 2 Nov 2020 16:45:49 +0100
Subject: [PATCH] Add a simple data persister

---
 src/DataPersister/PlaceDataPersister.php   | 34 ++++++++++++++++++++++
 src/DataProvider/PlaceItemDataProvider.php | 10 ++-----
 src/Entity/Place.php                       |  7 +++--
 3 files changed, 40 insertions(+), 11 deletions(-)
 create mode 100644 src/DataPersister/PlaceDataPersister.php

diff --git a/src/DataPersister/PlaceDataPersister.php b/src/DataPersister/PlaceDataPersister.php
new file mode 100644
index 0000000..c9b8631
--- /dev/null
+++ b/src/DataPersister/PlaceDataPersister.php
@@ -0,0 +1,34 @@
+<?php
+
+declare(strict_types=1);
+
+namespace DBP\API\StarterBundle\DataPersister;
+
+use ApiPlatform\Core\DataPersister\DataPersisterInterface;
+use DBP\API\StarterBundle\Entity\Place;
+use DBP\API\StarterBundle\Service\PlaceProviderInterface;
+
+class PlaceDataPersister implements DataPersisterInterface
+{
+    private $api;
+
+    public function __construct(PlaceProviderInterface $api)
+    {
+        $this->api = $api;
+    }
+
+    public function supports($data): bool
+    {
+        return $data instanceof Place;
+    }
+
+    public function persist($data)
+    {
+        // TODO
+    }
+
+    public function remove($data)
+    {
+        // TODO
+    }
+}
diff --git a/src/DataProvider/PlaceItemDataProvider.php b/src/DataProvider/PlaceItemDataProvider.php
index e69721f..51029e0 100644
--- a/src/DataProvider/PlaceItemDataProvider.php
+++ b/src/DataProvider/PlaceItemDataProvider.php
@@ -8,7 +8,6 @@ use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
 use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
 use DBP\API\StarterBundle\Entity\Place;
 use DBP\API\StarterBundle\Service\PlaceProviderInterface;
-use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 
 final class PlaceItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
 {
@@ -27,13 +26,8 @@ final class PlaceItemDataProvider implements ItemDataProviderInterface, Restrict
     /**
      * @param array|int|string $id
      */
-    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): Place
+    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?Place
     {
-        $place = $this->api->getPlaceById($id);
-        if ($place === null) {
-            throw new NotFoundHttpException();
-        }
-
-        return $place;
+        return $this->api->getPlaceById($id);
     }
 }
diff --git a/src/Entity/Place.php b/src/Entity/Place.php
index ef50736..95506c5 100644
--- a/src/Entity/Place.php
+++ b/src/Entity/Place.php
@@ -11,9 +11,10 @@ use Symfony\Component\Serializer\Annotation\Groups;
 /**
  * @ApiResource(
  *     collectionOperations={"get"},
- *     itemOperations={"get"},
+ *     itemOperations={"get", "put", "delete"},
  *     iri="https://schema.org/Place",
- *     normalizationContext={"groups"={"Place:output"}, "jsonld_embed_context"=true}
+ *     normalizationContext={"groups"={"Place:output"}, "jsonld_embed_context"=true},
+ *     denormalizationContext={"groups"={"Place:input"}, "jsonld_embed_context"=true}
  * )
  */
 class Place
@@ -25,7 +26,7 @@ class Place
 
     /**
      * @ApiProperty(iri="https://schema.org/name")
-     * @Groups({"Place:output"})
+     * @Groups({"Place:output", "Place:input"})
      *
      * @var string
      */
-- 
GitLab