openstackid-resources/app/Http/Controllers/Apis/Protected/Main/OAuth2SponsoredProjectApiController.php
smarcet 35fc57473c Sponsored Projects - Endpoints
POST /api/v1/sponsored-projects

payload

name
description
is_active

scope

REALM_URL/sponsored-projects/write

GET /api/v1/sponsored-projects

scope

REALM_URL/sponsored-projects/read

PUT /api/v1/sponsored-projects/{id}

payload

name
description
is_active

scope

REALM_URL/sponsored-projects/write

GET /api/v1/sponsored-projects/{id}
scope

REALM_URL/sponsored-projects/read

PUBLIC

GET /api/public/v1/sponsored-projects/{slug}

DELETE /api/v1/sponsored-projects/{id}

scope

REALM_URL/sponsored-projects/write

POST /api/v1/sponsored-projects/{id}/sponsorship-types

payload
name
description
is_active
order

scope

REALM_URL/sponsored-projects/write

GET /api/v1/sponsored-projects/{id}/sponsorship-types

scope

REALM_URL/sponsored-projects/read

GET /api/v1/sponsored-projects/{id}/sponsorship-types/{id}

scope

REALM_URL/sponsored-projects/read

DELETE /api/v1/sponsored-projects/{id}/sponsorship-types/{id}

scope

REALM_URL/sponsored-projects/write

PUT /api/v1/sponsored-projects/{id}/sponsorship-types/{id}

payload
name
description
is_active
order

scope

REALM_URL/sponsored-projects/write

PUT /api/v1/sponsored-projects/{id}/sponsorship-types/{id}/supporting-companies/{id}

payload
order (optional)

scope

REALM_URL/sponsored-projects/write

DELETE /api/v1/sponsored-projects/{id}/sponsorship-types/{id}/supporting-companies/{id}

scope

REALM_URL/sponsored-projects/write

GET /api/v1/sponsored-projects/{id}/sponsorship-types/{id}/supporting-companies

scope

REALM_URL/sponsored-projects/read

Change-Id: I9c0b1bb457a1c583afd284f56f2aced5deceaa02
Signed-off-by: smarcet <smarcet@gmail.com>
2020-11-30 15:09:40 -03:00

378 lines
11 KiB
PHP

<?php namespace App\Http\Controllers;
/**
* Copyright 2020 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\Main\Repositories\IProjectSponsorshipTypeRepository;
use App\Models\Foundation\Main\Repositories\ISponsoredProjectRepository;
use App\Models\Foundation\Main\Repositories\ISupportingCompanyRepository;
use App\Services\Model\ISponsoredProjectService;
use libs\utils\HTMLCleaner;
use models\oauth2\IResourceServerContext;
use models\utils\IEntity;
use ModelSerializers\SerializerRegistry;
use utils\Filter;
use utils\FilterElement;
use utils\PagingInfo;
/**
* Class OAuth2SponsoredProjectApiController
* @package App\Http\Controllers
*/
final class OAuth2SponsoredProjectApiController extends OAuth2ProtectedController
{
use ParametrizedGetAll;
use AddEntity;
use UpdateEntity;
use DeleteEntity;
use GetEntity;
use ParametrizedAddEntity;
use ParametrizedUpdateEntity;
use ParametrizedDeleteEntity;
use ParametrizedGetEntity;
/**
* @var ISponsoredProjectService
*/
private $service;
/**
* @var IProjectSponsorshipTypeRepository
*/
private $project_sponsorship_type_repository;
/**
* @var ISupportingCompanyRepository
*/
private $supporting_company_repository;
/**
* OAuth2SponsoredProjectApiController constructor.
* @param ISponsoredProjectRepository $company_repository
* @param IProjectSponsorshipTypeRepository $project_sponsorship_type_repository
* @param ISupportingCompanyRepository $supporting_company_repository
* @param IResourceServerContext $resource_server_context
* @param ISponsoredProjectService $service
*/
public function __construct
(
ISponsoredProjectRepository $company_repository,
IProjectSponsorshipTypeRepository $project_sponsorship_type_repository,
ISupportingCompanyRepository $supporting_company_repository,
IResourceServerContext $resource_server_context,
ISponsoredProjectService $service
)
{
parent::__construct($resource_server_context);
$this->repository = $company_repository;
$this->project_sponsorship_type_repository = $project_sponsorship_type_repository;
$this->supporting_company_repository = $supporting_company_repository;
$this->service = $service;
}
/**
* @inheritDoc
*/
function getAddValidationRules(array $payload): array
{
return SponsoredProjectValidationRulesFactory::build($payload);
}
/**
* @inheritDoc
*/
protected function addEntity(array $payload): IEntity
{
return $this->service->add(HTMLCleaner::cleanData($payload, ['description']));
}
/**
* @inheritDoc
*/
protected function deleteEntity(int $id): void
{
$this->service->delete($id);
}
/**
* @inheritDoc
*/
protected function getEntity(int $id): IEntity
{
return $this->repository->getById($id);
}
/**
* @inheritDoc
*/
function getUpdateValidationRules(array $payload): array
{
return SponsoredProjectValidationRulesFactory::build($payload, true);
}
/**
* @inheritDoc
*/
protected function updateEntity($id, array $payload): IEntity
{
return $this->service->update($id, HTMLCleaner::cleanData($payload, ['description']));
}
/**
* @return mixed
*/
public function getAll()
{
return $this->_getAll(
function () {
return [
'name' => ['=@', '=='],
'slug' => ['=@', '=='],
'is_active' => ['==']
];
},
function () {
return [
'is_active' => 'sometimes|boolean',
'name' => 'sometimes|string',
'slug' => 'sometimes|string',
];
},
function () {
return [
'name',
'id',
];
},
function ($filter) {
return $filter;
},
function () {
return SerializerRegistry::SerializerType_Public;
}
);
}
// sponsorship types
/**
* @param $id string|int
*/
public function getAllSponsorshipTypes($id)
{
return $this->_getAll(
function () {
return [
'name' => ['=@', '=='],
'slug' => ['=@', '=='],
'is_active' => ['==']
];
},
function () {
return [
'is_active' => 'sometimes|boolean',
'name' => 'sometimes|string',
'slug' => 'sometimes|string',
];
},
function () {
return [
'name',
'id',
];
},
function ($filter) use($id) {
if($filter instanceof Filter){
if(is_integer($id))
$filter->addFilterCondition(FilterElement::makeEqual('sponsored_project_id', intval($id)));
else
$filter->addFilterCondition(FilterElement::makeEqual('sponsored_project_slug', $id));
}
return $filter;
},
function () {
return SerializerRegistry::SerializerType_Public;
},
null,
null,
function ($page, $per_page, $filter, $order, $applyExtraFilters) {
return $this->project_sponsorship_type_repository->getAllByPage
(
new PagingInfo($page, $per_page),
call_user_func($applyExtraFilters, $filter),
$order
);
}
);
}
/**
* @param $id
* @param $sponsorship_type_id
*/
public function getSponsorshipType($id, $sponsorship_type_id){
$this->_get($sponsorship_type_id, function($id){
return $this->project_sponsorship_type_repository->getById(intval($id));
});
}
/**
* @param $id
* @return mixed
*/
public function addSponsorshipType($id)
{
$args = [intval($id)];
return $this->_add(
function ($payload) {
return ProjectSponsorshipTypeValidationRulesFactory::build($payload);
},
function ($payload, $id){
return $this->service->addProjectSponsorshipType($id, HTMLCleaner::cleanData($payload, ['description']));
},
...$args
);
}
/**
* @param $id
* @param $sponsorship_type_id
* @return mixed
*/
public function updateSponsorshipType($id, $sponsorship_type_id){
$args = [ intval($id) ];
return $this->_update(
$sponsorship_type_id,
function($payload){
return ProjectSponsorshipTypeValidationRulesFactory::build($payload, true);
},
function($sponsorship_type_id, $payload, $project_id){
return $this->service->updateProjectSponsorshipType($project_id, $sponsorship_type_id, HTMLCleaner::cleanData($payload, ['description']));
},
...$args
);
}
/**
* @param $id
* @param $sponsorship_type_id
* @return mixed
*/
public function deleteSponsorshipType($id, $sponsorship_type_id){
$args = [ intval($id) ];
return $this->_delete(
$sponsorship_type_id,
function ($sponsorship_type_id, $project_id){
$this->service->deleteProjectSponsorshipType($project_id, $sponsorship_type_id);
},
...$args
);
}
// supporting companies
public function getSupportingCompanies($id, $sponsorship_type_id){
return $this->_getAll(
function () {
return [
'name' => ['=@', '=='],
];
},
function () {
return [
'name' => 'sometimes|string',
];
},
function () {
return [
'name',
'order',
];
},
function ($filter) use($id, $sponsorship_type_id) {
if($filter instanceof Filter){
if(is_integer($id))
$filter->addFilterCondition(FilterElement::makeEqual('sponsored_project_id', intval($id)));
else
$filter->addFilterCondition(FilterElement::makeEqual('sponsored_project_slug', $id));
if(is_integer($sponsorship_type_id))
$filter->addFilterCondition(FilterElement::makeEqual('sponsorship_type_id', intval($sponsorship_type_id)));
else
$filter->addFilterCondition(FilterElement::makeEqual('sponsorship_type_slug', $sponsorship_type_id));
}
return $filter;
},
function () {
return SerializerRegistry::SerializerType_Public;
},
null,
null,
function ($page, $per_page, $filter, $order, $applyExtraFilters) {
return $this->supporting_company_repository->getAllByPage
(
new PagingInfo($page, $per_page),
call_user_func($applyExtraFilters, $filter),
$order
);
}
);
}
/**
* @param $id
* @param $sponsorship_type_id
* @param $company_id
* @return mixed
*/
public function addSupportingCompanies($id, $sponsorship_type_id, $company_id){
return $this->_update($company_id,
function($payload){
return [
'order' => 'sometimes|integer|min:1',
];
},
function($id, $payload, $project_id, $sponsorship_type_id){
return $this->service->addCompanyToProjectSponsorshipType
(
$project_id,
$sponsorship_type_id,
$id,
$payload
);
},
$id,
$sponsorship_type_id
);
}
/**
* @param $id
* @param $sponsorship_type_id
* @param $company_id
* @return mixed
*/
public function deleteSupportingCompanies($id, $sponsorship_type_id, $company_id){
return $this->_delete($company_id, function($id, $project_id, $sponsorship_type_id){
$this->service->removeCompanyToProjectSponsorshipType($id, $sponsorship_type_id, $id);
}, $id, $sponsorship_type_id);
}
}