Added missing endpoints to sponsored projects

Change-Id: I273977fee7f97569be8a33284d30a0d0f65fe746
Signed-off-by: smarcet <smarcet@gmail.com>
This commit is contained in:
smarcet 2021-04-09 18:08:45 -03:00
parent fbc98ca24a
commit 25f429082d
7 changed files with 137 additions and 10 deletions

View File

@ -18,6 +18,7 @@ use App\Models\Foundation\Main\Repositories\ISupportingCompanyRepository;
use App\Services\Model\ISponsoredProjectService;
use Illuminate\Support\Facades\Log;
use libs\utils\HTMLCleaner;
use models\exceptions\EntityNotFoundException;
use models\oauth2\IResourceServerContext;
use models\utils\IEntity;
use ModelSerializers\SerializerRegistry;
@ -236,6 +237,7 @@ final class OAuth2SponsoredProjectApiController extends OAuth2ProtectedControlle
return $this->project_sponsorship_type_repository->getById(intval($id));
});
}
/**
* @param $id
* @return mixed
@ -340,16 +342,44 @@ final class OAuth2SponsoredProjectApiController extends OAuth2ProtectedControlle
);
}
/**
* @param $id
* @param $sponsorship_type_id
* @return mixed
*/
public function addSupportingCompanies($id, $sponsorship_type_id){
return $this->_add(
function($payload){
return [
'company_id' => 'required|integer',
'order' => 'sometimes|integer|min:1',
];
},
function($payload, $project_id, $sponsorship_type_id){
return $this->service->addCompanyToProjectSponsorshipType
(
$project_id,
$sponsorship_type_id,
$payload
);
},
$id,
$sponsorship_type_id
);
}
/**
* @param $id
* @param $sponsorship_type_id
* @param $company_id
* @return mixed
*/
public function addSupportingCompanies($id, $sponsorship_type_id, $company_id){
public function updateSupportingCompanies($id, $sponsorship_type_id, $company_id){
return $this->_update($company_id,
function($payload){
return [
'company_id' => 'sometimes|integer',
'order' => 'sometimes|integer|min:1',
];
},
@ -378,4 +408,19 @@ final class OAuth2SponsoredProjectApiController extends OAuth2ProtectedControlle
$this->service->removeCompanyToProjectSponsorshipType($id, $sponsorship_type_id, $id);
}, $id, $sponsorship_type_id);
}
/**
* @param $id
* @param $sponsorship_type_id
* @param $company_id
* @return mixed
*/
public function getSupportingCompany($id, $sponsorship_type_id, $company_id){
return $this->_get($sponsorship_type_id, function($id, $company_id){
$sponsorship_type = $this->project_sponsorship_type_repository->getById(intval($id));
if(is_null($sponsorship_type))
throw new EntityNotFoundException();
return $sponsorship_type->getSupportingCompanyById(intval($company_id));
}, $company_id);
}
}

View File

@ -116,8 +116,10 @@ Route::group([
Route::group(['prefix'=>'supporting-companies'], function(){
Route::get('', [ 'uses' => 'OAuth2SponsoredProjectApiController@getSupportingCompanies']);
Route::post('', [ 'uses' => 'OAuth2SponsoredProjectApiController@addSupportingCompanies']);
Route::group(['prefix'=>'{company_id}'], function(){
Route::put('', [ 'middleware' => 'auth.user', 'uses' => 'OAuth2SponsoredProjectApiController@addSupportingCompanies']);
Route::get('', [ 'middleware' => 'auth.user', 'uses' => 'OAuth2SponsoredProjectApiController@getSupportingCompany']);
Route::put('', [ 'middleware' => 'auth.user', 'uses' => 'OAuth2SponsoredProjectApiController@updateSupportingCompanies']);
Route::delete('', [ 'middleware' => 'auth.user', 'uses' => 'OAuth2SponsoredProjectApiController@deleteSupportingCompanies']);
});
});

View File

@ -11,14 +11,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Libs\ModelSerializers\AbstractSerializer;
use Libs\ModelSerializers\One2ManyExpandSerializer;
use models\oauth2\IResourceServerContext;
/**
* Class SupportingCompanySerializer
* @package ModelSerializers
*/
final class SupportingCompanySerializer extends AbstractSerializer
final class SupportingCompanySerializer extends SilverStripeSerializer
{
protected static $array_mappings = [
'CompanyId' => 'company_id:json_int',

View File

@ -238,6 +238,15 @@ class ProjectSponsorshipType extends SilverstripeBaseModel implements IOrderable
return $this->supporting_companies;
}
/**
* @param int $id
* @return SupportingCompany|null
*/
public function getSupportingCompanyById(int $id):?SupportingCompany{
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('id', $id));
$res = $this->supporting_companies->matching($criteria)->first();
return !$res ? null : $res;
}
}

View File

@ -71,6 +71,16 @@ interface ISponsoredProjectService
*/
public function deleteProjectSponsorshipType(int $project_id, int $sponsorship_id):void;
/**
* @param int $project_id
* @param int $sponsorship_id
* @param array $payload
* @return SupportingCompany
* @throws ValidationException
* @throws EntityNotFoundException
*/
public function addCompanyToProjectSponsorshipType(int $project_id, int $sponsorship_id, array $payload):SupportingCompany;
/**
* @param int $project_id
* @param int $sponsorship_id
@ -80,7 +90,7 @@ interface ISponsoredProjectService
* @throws ValidationException
* @throws EntityNotFoundException
*/
public function addCompanyToProjectSponsorshipType(int $project_id, int $sponsorship_id, int $company_id, array $payload):SupportingCompany;
public function updateCompanyToProjectSponsorshipType(int $project_id, int $sponsorship_id, int $company_id, array $payload):SupportingCompany;
/**
* @param int $project_id

View File

@ -208,9 +208,10 @@ final class SponsoredProjectService
* @return SupportingCompany
* @throws \Exception
*/
public function addCompanyToProjectSponsorshipType(int $project_id, int $sponsorship_id, int $company_id, array $payload): SupportingCompany
public function addCompanyToProjectSponsorshipType(int $project_id, int $sponsorship_id, array $payload): SupportingCompany
{
return $this->tx_service->transaction(function() use ($project_id, $sponsorship_id, $company_id, $payload){
return $this->tx_service->transaction(function() use ($project_id, $sponsorship_id, $payload){
$sponsoredProject = $this->repository->getById($project_id);
if(is_null($sponsoredProject) || !$sponsoredProject instanceof SponsoredProject)
@ -220,10 +221,11 @@ final class SponsoredProjectService
if(is_null($projectSponsorshipType) || !$projectSponsorshipType instanceof ProjectSponsorshipType)
throw new EntityNotFoundException(sprintf("sponsorship type %s not found.", $project_id));
$company = $this->company_repository->getById($company_id);
$company = $this->company_repository->getById(intval($payload['company_id']));
if(is_null($company) || !$company instanceof Company)
throw new EntityNotFoundException(sprintf("company %s not found.", $company_id));
throw new EntityNotFoundException(sprintf("company %s not found.", $payload['company_id']));
$supportingCompany = $projectSponsorshipType->addSupportingCompany($company);
@ -236,6 +238,41 @@ final class SponsoredProjectService
});
}
/**
* @inheritDoc
*/
public function updateCompanyToProjectSponsorshipType(int $project_id, int $sponsorship_id, int $company_id, array $payload): SupportingCompany
{
return $this->tx_service->transaction(function() use ($project_id, $sponsorship_id,$company_id, $payload){
$sponsoredProject = $this->repository->getById($project_id);
if(is_null($sponsoredProject) || !$sponsoredProject instanceof SponsoredProject)
throw new EntityNotFoundException(sprintf("sponsored project %s not found.", $project_id));
$projectSponsorshipType = $sponsoredProject->getSponsorshipTypeById($sponsorship_id);
if(is_null($projectSponsorshipType) || !$projectSponsorshipType instanceof ProjectSponsorshipType)
throw new EntityNotFoundException(sprintf("sponsorship type %s not found.", $project_id));
$company = $this->company_repository->getById(intval($payload['company_id']));
if(is_null($company) || !$company instanceof Company)
throw new EntityNotFoundException(sprintf("company %s not found.", $payload['company_id']));
$supportingCompany = $projectSponsorshipType->getSupportingCompanyById($company_id);
if(is_null($supportingCompany))
throw new ValidationException(sprintf("Supporting company %s not found.", $company_id));
if (isset($payload['order']) && intval($payload['order']) != $supportingCompany->getOrder()) {
// request to update order
$projectSponsorshipType->recalculateSupportingCompanyOrder($supportingCompany, intval($payload['order']));
}
return $supportingCompany;
});
}
/**
* @inheritDoc
*/
@ -259,4 +296,5 @@ final class SponsoredProjectService
$projectSponsorshipType->removeSupportingCompany($company);
});
}
}

View File

@ -6181,6 +6181,18 @@ class ApiEndpointsSeeder extends Seeder
[
'name' => 'add-sponsored-project-supporting-companies',
'route' => '/api/v1/sponsored-projects/{id}/sponsorship-types/{sponsorship_type_id}/supporting-companies/{company_id}',
'http_method' => 'POST',
'scopes' => [
sprintf(SponsoredProjectScope::Write, $current_realm)
],
'authz_groups' => [
IGroup::SuperAdmins,
IGroup::Administrators,
]
],
[
'name' => 'update-sponsored-project-supporting-companies',
'route' => '/api/v1/sponsored-projects/{id}/sponsorship-types/{sponsorship_type_id}/supporting-companies/{company_id}',
'http_method' => 'PUT',
'scopes' => [
sprintf(SponsoredProjectScope::Write, $current_realm)
@ -6202,6 +6214,18 @@ class ApiEndpointsSeeder extends Seeder
IGroup::Administrators,
]
],
[
'name' => 'get-sponsored-project-supporting-company',
'route' => '/api/v1/sponsored-projects/{id}/sponsorship-types/{sponsorship_type_id}/supporting-companies/{company_id}',
'http_method' => 'GET',
'scopes' => [
sprintf(SponsoredProjectScope::Read, $current_realm)
],
'authz_groups' => [
IGroup::SuperAdmins,
IGroup::Administrators,
]
],
]
);
}