-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBookmarkCreateController.php
109 lines (100 loc) · 3.41 KB
/
BookmarkCreateController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Rest\Server\Controller\Bookmark;
use ApiPlatform\Metadata\Post;
use ApiPlatform\OpenApi\Factory\OpenApiFactory;
use ApiPlatform\OpenApi\Model;
use Ibexa\Contracts\Core\Repository\BookmarkService;
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
use Ibexa\Contracts\Core\Repository\LocationService;
use Ibexa\Rest\Server\Controller as RestController;
use Ibexa\Rest\Server\Values;
use Ibexa\Rest\Value as RestValue;
use Symfony\Component\HttpFoundation\Response;
#[Post(
uriTemplate: '/bookmark/{locationId}',
extraProperties: [OpenApiFactory::OVERRIDE_OPENAPI_RESPONSES => false],
openapi: new Model\Operation(
summary: 'Create bookmark',
description: 'Add given Location to bookmarks of the current user.',
tags: [
'Bookmark',
],
parameters: [
new Model\Parameter(
name: 'locationId',
in: 'path',
required: true,
schema: [
'type' => 'string',
],
),
],
responses: [
Response::HTTP_CREATED => [
'description' => 'Created.',
],
Response::HTTP_UNAUTHORIZED => [
'description' => 'Error - the user is not authorized to given Location.',
],
Response::HTTP_NOT_FOUND => [
'description' => 'Error - the given Location does not exist.',
],
Response::HTTP_CONFLICT => [
'description' => 'Error - Location is already bookmarked.',
],
],
requestBody: new Model\RequestBody(
content: new \ArrayObject(),
),
),
)]
class BookmarkCreateController extends RestController
{
/**
* @var \Ibexa\Contracts\Core\Repository\BookmarkService
*/
protected $bookmarkService;
/**
* @var \Ibexa\Contracts\Core\Repository\LocationService
*/
protected $locationService;
/**
* Bookmark constructor.
*
* @param \Ibexa\Contracts\Core\Repository\BookmarkService $bookmarkService
* @param \Ibexa\Contracts\Core\Repository\LocationService $locationService
*/
public function __construct(BookmarkService $bookmarkService, LocationService $locationService)
{
$this->bookmarkService = $bookmarkService;
$this->locationService = $locationService;
}
/**
* Add given location to bookmarks.
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
*/
public function createBookmark(int $locationId): RestValue|Values\Conflict
{
$location = $this->locationService->loadLocation($locationId);
try {
$this->bookmarkService->createBookmark($location);
return new Values\ResourceCreated(
$this->router->generate(
'ibexa.rest.is_bookmarked',
[
'locationId' => $locationId,
]
)
);
} catch (InvalidArgumentException $e) {
return new Values\Conflict();
}
}
}