Skip to content

Commit 0b678a4

Browse files
authored
feat(marketplace): add new get drivers endpoint (#518)
GET /api/public/v1/marketplace/drivers
1 parent a6e80f9 commit 0b678a4

11 files changed

Lines changed: 1071 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php namespace App\Http\Controllers;
2+
/**
3+
* Copyright 2017 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
use App\Models\Foundation\Marketplace\IDriverRepository;
15+
use models\oauth2\IResourceServerContext;
16+
use models\utils\IBaseRepository;
17+
use ModelSerializers\SerializerRegistry;
18+
19+
/**
20+
* Class DriversApiController
21+
* @package App\Http\Controllers
22+
*/
23+
final class DriversApiController extends JsonController
24+
{
25+
use ParametrizedGetAll;
26+
27+
/**
28+
* @var IDriverRepository
29+
*/
30+
private $repository;
31+
32+
/**
33+
* @var IResourceServerContext
34+
*/
35+
private $resource_server_context;
36+
37+
/**
38+
* DriversApiController constructor.
39+
* @param IDriverRepository $repository
40+
* @param IResourceServerContext $resource_server_context
41+
*/
42+
public function __construct(IDriverRepository $repository, IResourceServerContext $resource_server_context)
43+
{
44+
parent::__construct();
45+
$this->repository = $repository;
46+
$this->resource_server_context = $resource_server_context;
47+
}
48+
49+
protected function getResourceServerContext(): IResourceServerContext
50+
{
51+
return $this->resource_server_context;
52+
}
53+
54+
protected function getRepository(): IBaseRepository
55+
{
56+
return $this->repository;
57+
}
58+
59+
/**
60+
* @return mixed
61+
*/
62+
public function getAll()
63+
{
64+
return $this->_getAll(
65+
function () {
66+
return [
67+
'name' => ['=@', '==', '@@'],
68+
'project' => ['=@', '==', '@@'],
69+
'vendor' => ['=@', '==', '@@'],
70+
'release' => ['=@', '==', '@@'],
71+
];
72+
},
73+
function () {
74+
return [
75+
'name' => 'sometimes|string',
76+
'project' => 'sometimes|string',
77+
'vendor' => 'sometimes|string',
78+
'release' => 'sometimes|string',
79+
];
80+
},
81+
function () {
82+
return [
83+
'id',
84+
'name',
85+
'project',
86+
'vendor',
87+
];
88+
},
89+
function ($filter) {
90+
return $filter;
91+
},
92+
function () {
93+
return SerializerRegistry::SerializerType_Public;
94+
}
95+
);
96+
}
97+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php namespace App\ModelSerializers\Marketplace;
2+
/**
3+
* Copyright 2017 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
use ModelSerializers\SilverStripeSerializer;
15+
16+
/**
17+
* Class DriverReleaseSerializer
18+
* @package App\ModelSerializers\Marketplace
19+
*/
20+
class DriverReleaseSerializer extends SilverStripeSerializer
21+
{
22+
protected static $array_mappings = [
23+
'Name' => 'name:json_string',
24+
'Url' => 'url:json_string',
25+
'Active' => 'active:json_boolean',
26+
];
27+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php namespace App\ModelSerializers\Marketplace;
2+
/**
3+
* Copyright 2017 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
15+
use App\Models\Foundation\Marketplace\Driver;
16+
use Libs\ModelSerializers\Many2OneExpandSerializer;
17+
use ModelSerializers\SilverStripeSerializer;
18+
19+
/**
20+
* Class DriverSerializer
21+
* @package App\ModelSerializers\Marketplace
22+
*/
23+
class DriverSerializer extends SilverStripeSerializer
24+
{
25+
protected static $array_mappings = [
26+
'Name' => 'name:json_string',
27+
'Description' => 'description:json_string',
28+
'Project' => 'project:json_string',
29+
'Vendor' => 'vendor:json_string',
30+
'Url' => 'url:json_string',
31+
'Tested' => 'tested:json_boolean',
32+
'Active' => 'active:json_boolean',
33+
];
34+
35+
/**
36+
* @param $expand
37+
* @param array $fields
38+
* @param array $relations
39+
* @param array $params
40+
* @return array
41+
*/
42+
public function serialize($expand = null, array $fields = [], array $relations = [], array $params = [])
43+
{
44+
$driver = $this->object;
45+
if (!$driver instanceof Driver) return [];
46+
$values = parent::serialize($expand, $fields, $relations, $params);
47+
48+
if (in_array('releases', $relations) && !isset($values['releases'])) {
49+
$releases = [];
50+
foreach ($driver->getReleases() as $r) {
51+
$releases[] = $r->getId();
52+
}
53+
$values['releases'] = $releases;
54+
}
55+
return $values;
56+
}
57+
58+
protected static $allowed_relations = [
59+
'releases',
60+
];
61+
62+
protected static $expand_mappings = [
63+
'releases' => [
64+
'type' => Many2OneExpandSerializer::class,
65+
'getter' => 'getReleases',
66+
],
67+
];
68+
}

app/ModelSerializers/SerializerRegistry.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
use App\ModelSerializers\Marketplace\TrainingCourseSerializer;
7070
use App\ModelSerializers\Marketplace\TrainingCourseTypeSerializer;
7171
use App\ModelSerializers\Marketplace\TrainingServiceSerializer;
72+
use App\ModelSerializers\Marketplace\DriverSerializer;
73+
use App\ModelSerializers\Marketplace\DriverReleaseSerializer;
7274
use App\ModelSerializers\PushNotificationMessageSerializer;
7375
use App\ModelSerializers\ResourceServer\ApiEndpointAuthzGroupSerializer;
7476
use App\ModelSerializers\ResourceServer\ApiEndpointSerializer;
@@ -668,6 +670,8 @@ private function __construct()
668670
$this->registry['RemoteCloudService'] = RemoteCloudServiceSerializer::class;
669671
$this->registry['CloudServiceOffered'] = CloudServiceOfferedSerializer::class;
670672
$this->registry['TrainingService'] = TrainingServiceSerializer::class;
673+
$this->registry['Driver'] = DriverSerializer::class;
674+
$this->registry['DriverRelease'] = DriverReleaseSerializer::class;
671675
$this->registry['TrainingCourse'] = TrainingCourseSerializer::class;
672676
$this->registry['TrainingCourseType'] = TrainingCourseTypeSerializer::class;
673677
$this->registry['TrainingCourseLevel'] = TrainingCourseLevelSerializer::class;

0 commit comments

Comments
 (0)