From d2144a1fed4a18daa7561059b3b37a7d8584fe4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Mon, 18 Sep 2023 13:24:33 +0200 Subject: [PATCH] Added possibility to add a change comment to changes via the API --- docs/api/intro.md | 9 ++- .../AddEditCommentRequestListener.php | 62 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/EventListener/AddEditCommentRequestListener.php diff --git a/docs/api/intro.md b/docs/api/intro.md index c308d7fb..2c9c1fa1 100644 --- a/docs/api/intro.md +++ b/docs/api/intro.md @@ -88,4 +88,11 @@ You can achieve this using the `properties[]` query parameter with the name of t For example if you only want to get the name and the description of a part, you can use `/api/parts/123?properties[]=name&properties[]=description`. It is also possible to use this filters on list endpoints (get collection), to only get a subset of the properties of all entities in the collection. -See [API Platform docs](https://api-platform.com/docs/core/filters/#property-filter) for more infos. \ No newline at end of file +See [API Platform docs](https://api-platform.com/docs/core/filters/#property-filter) for more infos. + +## Change comment + +Similar to the changes using Part-DB web interface, you can add a change comment to every change you make via the API, which will be +visible in the log of the entity. + +You can pass the text for this via the `_comment` query parameter (beware the proper encoding). For example `/api/parts/123?_comment=This%20is%20a%20change%20comment`. \ No newline at end of file diff --git a/src/EventListener/AddEditCommentRequestListener.php b/src/EventListener/AddEditCommentRequestListener.php new file mode 100644 index 00000000..3f2cc1b5 --- /dev/null +++ b/src/EventListener/AddEditCommentRequestListener.php @@ -0,0 +1,62 @@ +. + */ + +declare(strict_types=1); + + +namespace App\EventListener; + +use App\Services\LogSystem\EventCommentHelper; +use Symfony\Component\EventDispatcher\Attribute\AsEventListener; +use Symfony\Component\HttpKernel\Event\RequestEvent; + +#[AsEventListener()] +class AddEditCommentRequestListener +{ + public function __construct(private readonly EventCommentHelper $helper) + { + + } + + public function __invoke(RequestEvent $event) + { + if (!$event->isMainRequest()) { + return; + } + $request = $event->getRequest(); + + //Do not add comment if the request is a GET request + if ($request->isMethod('GET')) { + return; + } + + //Check if the user tries to access a /api/ endpoint, if not skip + if (!str_contains($request->getPathInfo(), '/api/')) { + return; + } + + //Extract the comment from the query parameter + $comment = $request->query->getString('_comment', ''); + + if ($comment !== '') { + $this->helper->setMessage($comment); + } + } +} \ No newline at end of file