diff --git a/app/Http/Controllers/Apis/Protected/Main/OAuth2SponsoredProjectApiController.php b/app/Http/Controllers/Apis/Protected/Main/OAuth2SponsoredProjectApiController.php index 741e85d1..cbc7f957 100644 --- a/app/Http/Controllers/Apis/Protected/Main/OAuth2SponsoredProjectApiController.php +++ b/app/Http/Controllers/Apis/Protected/Main/OAuth2SponsoredProjectApiController.php @@ -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); + } } \ No newline at end of file diff --git a/app/Http/routes.php b/app/Http/routes.php index 02356b77..0cfeceb7 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -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']); }); }); diff --git a/app/ModelSerializers/Companies/SupportingCompanySerializer.php b/app/ModelSerializers/Companies/SupportingCompanySerializer.php index db64d3b2..0ee9ba9d 100644 --- a/app/ModelSerializers/Companies/SupportingCompanySerializer.php +++ b/app/ModelSerializers/Companies/SupportingCompanySerializer.php @@ -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', diff --git a/app/Models/Foundation/Main/Companies/ProjectSponsorshipType.php b/app/Models/Foundation/Main/Companies/ProjectSponsorshipType.php index 098572fe..acceed21 100644 --- a/app/Models/Foundation/Main/Companies/ProjectSponsorshipType.php +++ b/app/Models/Foundation/Main/Companies/ProjectSponsorshipType.php @@ -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; + } } \ No newline at end of file diff --git a/app/Services/Model/ISponsoredProjectService.php b/app/Services/Model/ISponsoredProjectService.php index 5c396536..d451c894 100644 --- a/app/Services/Model/ISponsoredProjectService.php +++ b/app/Services/Model/ISponsoredProjectService.php @@ -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 diff --git a/app/Services/Model/Imp/SponsoredProjectService.php b/app/Services/Model/Imp/SponsoredProjectService.php index 1593716c..8d75799d 100644 --- a/app/Services/Model/Imp/SponsoredProjectService.php +++ b/app/Services/Model/Imp/SponsoredProjectService.php @@ -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); }); } + } \ No newline at end of file diff --git a/database/seeds/ApiEndpointsSeeder.php b/database/seeds/ApiEndpointsSeeder.php index bec55f91..f2603286 100644 --- a/database/seeds/ApiEndpointsSeeder.php +++ b/database/seeds/ApiEndpointsSeeder.php @@ -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, + ] + ], ] ); }