Skip to content

Commit

Permalink
feat: add new generic get services endpoint
Browse files Browse the repository at this point in the history
GET api/public/v1/marketplace/services

Change-Id: I152f2dfc72b5c3672d4be4a70c83563a11204c11
  • Loading branch information
smarcet committed Apr 12, 2024
1 parent 109040f commit 0854cbb
Show file tree
Hide file tree
Showing 18 changed files with 234 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@
* limitations under the License.
**/

use libs\utils\PaginationValidationRules;
use models\oauth2\IResourceServerContext;
use models\utils\IBaseRepository;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use Illuminate\Support\Facades\Validator;
use utils\Filter;
use utils\FilterParser;
use utils\OrderParser;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Log;
use utils\PagingInfo;
use ModelSerializers\SerializerRegistry;

/**
* Class AbstractCompanyServiceApiController
* @package App\Http\Controllers
Expand All @@ -35,90 +28,63 @@ abstract class AbstractCompanyServiceApiController extends JsonController
protected $repository;

/**
* AppliancesApiController constructor.
* @var IResourceServerContext
*/
protected $resource_server_context;

/**
* @param IBaseRepository $repository
* @param IResourceServerContext $resource_server_context
*/
public function __construct(IBaseRepository $repository)
public function __construct(IBaseRepository $repository, IResourceServerContext $resource_server_context)
{
parent::__construct();
$this->repository = $repository;
$this->resource_server_context = $resource_server_context;
}


protected function getResourceServerContext(): IResourceServerContext
{
return $this->resource_server_context;
}

protected function getRepository(): IBaseRepository
{
return $this->repository;
}

use ParametrizedGetAll;
/**
* @return mixed
*/
public function getAll(){
$values = Request::all();

$rules = PaginationValidationRules::get();

try {

$validation = Validator::make($values, $rules);

if ($validation->fails()) {
$ex = new ValidationException();
throw $ex->setMessages($validation->messages()->toArray());
}

// default values
$page = 1;
$per_page = PaginationValidationRules::PerPageMin;

if (Request::has('page')) {
$page = intval(Request::input('page'));
$per_page = intval(Request::input('per_page'));
}

$filter = null;

if (Request::has('filter')) {
$filter = FilterParser::parse(Request::input('filter'), array
(
return $this->_getAll(
function () {
return [
'name' => ['=@', '=='],
'company' => ['=@', '=='],
));
}

$order = null;

if (Request::has('order'))
{
$order = OrderParser::parse(Request::input('order'), array
(
];
},
function () {
return [
'name' => 'sometimes|string',
'company' => 'sometimes|string',
];
},
function () {
return [
'name',
'company',
'id',
));
];
},
function ($filter) {
return $filter;
},
function () {
return SerializerRegistry::SerializerType_Public;
}

if(is_null($filter)) $filter = new Filter();

$data = $this->repository->getAllByPage(new PagingInfo($page, $per_page), $filter, $order);
$fields = Request::input('fields', '');
$fields = !empty($fields) ? explode(',', $fields) : [];
$relations = Request::input('relations', '');
$relations = !empty($relations) ? explode(',', $relations) : [];
return $this->ok
(
$data->toArray
(
Request::input('expand', ''),
$fields,
$relations
)
);
}
catch (EntityNotFoundException $ex1) {
Log::warning($ex1);
return $this->error404();
}
catch (ValidationException $ex2) {
Log::warning($ex2);
return $this->error412($ex2->getMessages());
}
catch (\Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* limitations under the License.
**/
use App\Models\Foundation\Marketplace\IApplianceRepository;
use models\oauth2\IResourceServerContext;

/**
* Class AppliancesApiController
* @package App\Http\Controllers
Expand All @@ -23,9 +25,9 @@ final class AppliancesApiController extends AbstractCompanyServiceApiController
* AppliancesApiController constructor.
* @param IApplianceRepository $repository
*/
public function __construct(IApplianceRepository $repository)
public function __construct(IApplianceRepository $repository, IResourceServerContext $resource_server_context)
{
parent::__construct($repository);
parent::__construct($repository, $resource_server_context);
}

public function getAll()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php namespace App\Http\Controllers;
/*
* Copyright 2024 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use App\Models\Foundation\Marketplace\ICompanyServiceRepository;
use models\oauth2\IResourceServerContext;

/**
* Class CompanyServiceApiController
* @package App\Http\Controllers
*/
final class CompanyServiceApiController extends AbstractCompanyServiceApiController
{

/**
* @param ICompanyServiceRepository $repository
* @param IResourceServerContext $resource_server_context
*/
public function __construct(ICompanyServiceRepository $repository, IResourceServerContext$resource_server_context)
{
parent::__construct($repository, $resource_server_context);
}

public function getAll()
{
return parent::getAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* limitations under the License.
**/
use App\Models\Foundation\Marketplace\IConsultantRepository;
use models\oauth2\IResourceServerContext;

/**
* Class ConsultantsApiController
Expand All @@ -24,9 +25,9 @@ final class ConsultantsApiController extends AbstractCompanyServiceApiController
* ConsultansApiController constructor.
* @param IConsultantRepository $repository
*/
public function __construct(IConsultantRepository $repository)
public function __construct(IConsultantRepository $repository, IResourceServerContext $resource_server_context)
{
parent::__construct($repository);
parent::__construct($repository, $resource_server_context);
}

public function getAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* limitations under the License.
**/
use App\Models\Foundation\Marketplace\IDistributionRepository;
use models\oauth2\IResourceServerContext;

/**
* Class DistributionsApiController
* @package App\Http\Controllers\Apis\Marketplace
Expand All @@ -23,9 +25,9 @@ final class DistributionsApiController extends AbstractCompanyServiceApiControll
* DistributionsApiController constructor.
* @param IDistributionRepository $repository
*/
public function __construct(IDistributionRepository $repository)
public function __construct(IDistributionRepository $repository, IResourceServerContext $resource_server_context)
{
parent::__construct($repository);
parent::__construct($repository, $resource_server_context);
}

public function getAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* limitations under the License.
**/
use App\Models\Foundation\Marketplace\IPrivateCloudServiceRepository;
use models\oauth2\IResourceServerContext;

/**
* Class PrivateCloudsApiController
* @package App\Http\Controllers
Expand All @@ -22,9 +24,9 @@ final class PrivateCloudsApiController extends AbstractCompanyServiceApiControll
* PrivateCloudsApiController constructor.
* @param IPrivateCloudServiceRepository $repository
*/
public function __construct(IPrivateCloudServiceRepository $repository)
public function __construct(IPrivateCloudServiceRepository $repository, IResourceServerContext $resource_server_context)
{
parent::__construct($repository);
parent::__construct($repository, $resource_server_context);
}

public function getAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* limitations under the License.
**/
use App\Models\Foundation\Marketplace\IPublicCloudServiceRepository;
use models\oauth2\IResourceServerContext;

/**
* Class PublicCloudsApiController
* @package App\Http\Controllers
Expand All @@ -22,9 +24,9 @@ final class PublicCloudsApiController extends AbstractCompanyServiceApiControlle
* PrivateCloudsApiController constructor.
* @param IPublicCloudServiceRepository $repository
*/
public function __construct(IPublicCloudServiceRepository $repository)
public function __construct(IPublicCloudServiceRepository $repository, IResourceServerContext $resource_server_context)
{
parent::__construct($repository);
parent::__construct($repository, $resource_server_context);
}

public function getAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* limitations under the License.
**/
use App\Models\Foundation\Marketplace\IRemoteCloudServiceRepository;
use models\oauth2\IResourceServerContext;

/**
* Class RemoteCloudsApiController
* @package App\Http\Controllers
Expand All @@ -22,9 +24,9 @@ final class RemoteCloudsApiController extends AbstractCompanyServiceApiControlle
* PrivateCloudsApiController constructor.
* @param IRemoteCloudServiceRepository $repository
*/
public function __construct(IRemoteCloudServiceRepository $repository)
public function __construct(IRemoteCloudServiceRepository $repository, IResourceServerContext $resource_server_context)
{
parent::__construct($repository);
parent::__construct($repository, $resource_server_context);
}

public function getAll()
Expand Down
23 changes: 23 additions & 0 deletions app/ModelSerializers/Marketplace/TrainingServiceSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php namespace App\ModelSerializers\Marketplace;
/*
* Copyright 2024 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/


/**
* Class TrainingServiceSerializer
* @package App\ModelSerializers\Marketplace
*/
final class TrainingServiceSerializer extends CompanyServiceSerializer
{

}
2 changes: 2 additions & 0 deletions app/ModelSerializers/SerializerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use App\ModelSerializers\Marketplace\ServiceOfferedTypeSerializer;
use App\ModelSerializers\Marketplace\SpokenLanguageSerializer;
use App\ModelSerializers\Marketplace\SupportChannelTypeSerializer;
use App\ModelSerializers\Marketplace\TrainingServiceSerializer;
use App\ModelSerializers\PushNotificationMessageSerializer;
use App\ModelSerializers\ResourceServer\ApiEndpointAuthzGroupSerializer;
use App\ModelSerializers\ResourceServer\ApiEndpointSerializer;
Expand Down Expand Up @@ -627,6 +628,7 @@ private function __construct()
$this->registry['PublicCloudService'] = PublicCloudServiceSerializer::class;
$this->registry['RemoteCloudService'] = RemoteCloudServiceSerializer::class;
$this->registry['CloudServiceOffered'] = CloudServiceOfferedSerializer::class;
$this->registry['TrainingService'] = TrainingServiceSerializer::class;
// software

$this->registry['OpenStackComponent'] = OpenStackComponentSerializer::class;
Expand Down
5 changes: 3 additions & 2 deletions app/Models/Foundation/Marketplace/CompanyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use models\utils\SilverstripeBaseModel;
use models\main\Company;
/**
* @ORM\Entity
* @ORM\Entity(repositoryClass="App\Repositories\Marketplace\DoctrineCompanyServiceRepository")
* @ORM\Table(name="CompanyService")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="ClassName", type="string")
Expand All @@ -32,7 +32,8 @@
* "CloudService", "CloudService",
* "PrivateCloudService" = "PrivateCloudService",
* "PublicCloudService" = "PublicCloudService",
* "RemoteCloudService" = "RemoteCloudService"
* "RemoteCloudService" = "RemoteCloudService",
* "TrainingService" ="TrainingService",
* } )
* Class CompanyService
* @package App\Models\Foundation\Marketplace
Expand Down
Loading

0 comments on commit 0854cbb

Please sign in to comment.