From fa96c4c1a0df444563f358ea657b92c5675e6eb0 Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Tue, 18 Sep 2018 13:04:24 -0300 Subject: [PATCH] added endpoints for my presentations moderators PUT /api/v1/speakers/me/presentations/{presentation_id}/moderators/{speaker_id} DELETE /api/v1/speakers/me/presentations/{presentation_id}/moderators/{speaker_id} Change-Id: I2b46246bdf2fbfd62d89ab0fd1449ad63c6bc419 --- .../OAuth2SummitSpeakersApiController.php | 60 +++++++++++++- app/Http/routes.php | 5 +- .../Summit/DefaultTrackTagGroupAllowedTag.php | 25 +----- app/Services/Model/ISummitService.php | 22 ++++- app/Services/Model/SummitService.php | 83 ++++++++++++++++++- database/seeds/ApiEndpointsSeeder.php | 22 ++++- tests/OAuth2TrackTagGroupsApiTest.php | 4 +- 7 files changed, 188 insertions(+), 33 deletions(-) diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitSpeakersApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitSpeakersApiController.php index b22e0ea9..82d1d5ed 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitSpeakersApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitSpeakersApiController.php @@ -872,13 +872,40 @@ final class OAuth2SummitSpeakersApiController extends OAuth2ProtectedController * @param $speaker_id * @return mixed */ - public function removeSpeakerToMyPresentation($presentation_id, $speaker_id){ + public function addModeratorToMyPresentation($presentation_id, $speaker_id){ try { $current_member_id = $this->resource_server_context->getCurrentUserExternalId(); if (is_null($current_member_id)) return $this->error403(); - $this->summit_service->removeSpeaker2Presentation($current_member_id, $speaker_id, $presentation_id); + $this->summit_service->addModerator2Presentation($current_member_id, $speaker_id, $presentation_id); + + return $this->updated(); + + } catch (ValidationException $ex1) { + Log::warning($ex1); + return $this->error412(array($ex1->getMessage())); + } catch (EntityNotFoundException $ex2) { + Log::warning($ex2); + return $this->error404(array('message' => $ex2->getMessage())); + } catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + + /** + * @param $presentation_id + * @param $speaker_id + * @return mixed + */ + public function removeSpeakerFromMyPresentation($presentation_id, $speaker_id){ + try { + $current_member_id = $this->resource_server_context->getCurrentUserExternalId(); + if (is_null($current_member_id)) + return $this->error403(); + + $this->summit_service->removeSpeakerFromPresentation($current_member_id, $speaker_id, $presentation_id); return $this->deleted(); @@ -893,4 +920,33 @@ final class OAuth2SummitSpeakersApiController extends OAuth2ProtectedController return $this->error500($ex); } } + + /** + * @param $presentation_id + * @param $speaker_id + * @return mixed + */ + public function removeModeratorFromMyPresentation($presentation_id, $speaker_id){ + try { + $current_member_id = $this->resource_server_context->getCurrentUserExternalId(); + if (is_null($current_member_id)) + return $this->error403(); + + $this->summit_service->removeModeratorFromPresentation($current_member_id, $speaker_id, $presentation_id); + + return $this->deleted(); + + } catch (ValidationException $ex1) { + Log::warning($ex1); + return $this->error412(array($ex1->getMessage())); + } catch (EntityNotFoundException $ex2) { + Log::warning($ex2); + return $this->error404(array('message' => $ex2->getMessage())); + } catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + + } \ No newline at end of file diff --git a/app/Http/routes.php b/app/Http/routes.php index 002523ee..663488f0 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -607,10 +607,11 @@ Route::group([ Route::group(['prefix' => '{presentation_id}'], function(){ Route::group(['prefix' => 'speakers'], function(){ Route::put('{speaker_id}', 'OAuth2SummitSpeakersApiController@addSpeakerToMyPresentation'); - Route::delete('{speaker_id}', 'OAuth2SummitSpeakersApiController@removeSpeakerToMyPresentation'); + Route::delete('{speaker_id}', 'OAuth2SummitSpeakersApiController@removeSpeakerFromMyPresentation'); }); Route::group(['prefix' => 'moderators'], function(){ - + Route::put('{speaker_id}', 'OAuth2SummitSpeakersApiController@addModeratorToMyPresentation'); + Route::delete('{speaker_id}', 'OAuth2SummitSpeakersApiController@removeModeratorFromMyPresentation'); }); }); Route::group(['prefix' => '{role}'], function(){ diff --git a/app/Models/Foundation/Summit/DefaultTrackTagGroupAllowedTag.php b/app/Models/Foundation/Summit/DefaultTrackTagGroupAllowedTag.php index b57b133e..1dda458b 100644 --- a/app/Models/Foundation/Summit/DefaultTrackTagGroupAllowedTag.php +++ b/app/Models/Foundation/Summit/DefaultTrackTagGroupAllowedTag.php @@ -23,12 +23,6 @@ use Doctrine\ORM\Mapping AS ORM; class DefaultTrackTagGroupAllowedTag extends BaseEntity { - /** - * @ORM\Column(name="IsDefault", type="boolean") - * @var boolean - */ - private $is_default; - /** * @ORM\ManyToOne(targetEntity="models\main\Tag") * @ORM\JoinColumn(name="TagID", referencedColumnName="ID") @@ -38,7 +32,7 @@ class DefaultTrackTagGroupAllowedTag extends BaseEntity /** * @ORM\ManyToOne(targetEntity="DefaultTrackTagGroup", inversedBy="allowed_tags") - * @ORM\JoinColumn(name="TrackTagGroupID", referencedColumnName="ID") + * @ORM\JoinColumn(name="DefaultTrackTagGroupID", referencedColumnName="ID") * @var DefaultTrackTagGroup */ private $track_tag_group; @@ -67,23 +61,6 @@ class DefaultTrackTagGroupAllowedTag extends BaseEntity } } - - /** - * @return bool - */ - public function isDefault() - { - return $this->is_default; - } - - /** - * @param bool $is_default - */ - public function setIsDefault($is_default) - { - $this->is_default = $is_default; - } - /** * @return Tag */ diff --git a/app/Services/Model/ISummitService.php b/app/Services/Model/ISummitService.php index fab17033..48012234 100644 --- a/app/Services/Model/ISummitService.php +++ b/app/Services/Model/ISummitService.php @@ -241,5 +241,25 @@ interface ISummitService * @throws EntityNotFoundException * @return void */ - public function removeSpeaker2Presentation($current_member_id, $speaker_id, $presentation_id); + public function addModerator2Presentation($current_member_id, $speaker_id, $presentation_id); + + /** + * @param int $current_member_id + * @param int $speaker_id + * @param int $presentation_id + * @throws ValidationException + * @throws EntityNotFoundException + * @return void + */ + public function removeSpeakerFromPresentation($current_member_id, $speaker_id, $presentation_id); + + /** + * @param int $current_member_id + * @param int $speaker_id + * @param int $presentation_id + * @throws ValidationException + * @throws EntityNotFoundException + * @return void + */ + public function removeSpeakerFromModerator($current_member_id, $speaker_id, $presentation_id); } \ No newline at end of file diff --git a/app/Services/Model/SummitService.php b/app/Services/Model/SummitService.php index 97e895ed..cb9bf3f1 100644 --- a/app/Services/Model/SummitService.php +++ b/app/Services/Model/SummitService.php @@ -1703,7 +1703,7 @@ final class SummitService extends AbstractService implements ISummitService * @throws EntityNotFoundException * @return void */ - public function removeSpeaker2Presentation($current_member_id, $speaker_id, $presentation_id) + public function removeSpeakerFromPresentation($current_member_id, $speaker_id, $presentation_id) { return $this->tx_service->transaction(function () use ($current_member_id, $speaker_id, $presentation_id) { @@ -1735,4 +1735,85 @@ final class SummitService extends AbstractService implements ISummitService $presentation->removeSpeaker($speaker); }); } + + /** + * @param int $current_member_id + * @param int $speaker_id + * @param int $presentation_id + * @throws ValidationException + * @throws EntityNotFoundException + * @return void + */ + public function addModerator2Presentation($current_member_id, $speaker_id, $presentation_id) + { + return $this->tx_service->transaction(function () use ($current_member_id, $speaker_id, $presentation_id) { + $current_member = $this->member_repository->getById($current_member_id); + if(is_null($current_member)) + throw new EntityNotFoundException(sprintf("member %s not found", $current_member_id)); + + $current_speaker = $this->speaker_repository->getByMember($current_member); + if(is_null($current_speaker)) + throw new EntityNotFoundException(sprintf("member %s does not has a speaker profile", $current_member_id)); + + $presentation = $this->event_repository->getById($presentation_id); + if(is_null($presentation)) + throw new EntityNotFoundException(sprintf("presentation %s not found", $presentation_id)); + + if(!$presentation instanceof Presentation) + throw new EntityNotFoundException(sprintf("presentation %s not found", $presentation_id)); + + if(!$presentation->canEdit($current_speaker)) + throw new ValidationException(sprintf("member %s can not edit presentation %s", + $current_member_id, + $presentation_id + )); + + $speaker = $this->speaker_repository->getById(intval($speaker_id)); + if (is_null($speaker)) + throw new EntityNotFoundException(sprintf('speaker %s not found', $speaker_id)); + + $presentation->setModerator($speaker); + }); + } + + /** + * @param int $current_member_id + * @param int $speaker_id + * @param int $presentation_id + * @throws ValidationException + * @throws EntityNotFoundException + * @return void + */ + public function removeSpeakerFromModerator($current_member_id, $speaker_id, $presentation_id) + { + return $this->tx_service->transaction(function () use ($current_member_id, $speaker_id, $presentation_id) { + + $current_member = $this->member_repository->getById($current_member_id); + if(is_null($current_member)) + throw new EntityNotFoundException(sprintf("member %s not found", $current_member_id)); + + $current_speaker = $this->speaker_repository->getByMember($current_member); + if(is_null($current_speaker)) + throw new EntityNotFoundException(sprintf("member %s does not has a speaker profile", $current_member_id)); + + $presentation = $this->event_repository->getById($presentation_id); + if(is_null($presentation)) + throw new EntityNotFoundException(sprintf("presentation %s not found", $presentation_id)); + + if(!$presentation instanceof Presentation) + throw new EntityNotFoundException(sprintf("presentation %s not found", $presentation_id)); + + if(!$presentation->canEdit($current_speaker)) + throw new ValidationException(sprintf("member %s can not edit presentation %s", + $current_member_id, + $presentation_id + )); + + $speaker = $this->speaker_repository->getById(intval($speaker_id)); + if (is_null($speaker)) + throw new EntityNotFoundException(sprintf('speaker %s not found', $speaker_id)); + + $presentation->unsetModerator(); + }); + } } \ No newline at end of file diff --git a/database/seeds/ApiEndpointsSeeder.php b/database/seeds/ApiEndpointsSeeder.php index ac8f4227..4770a889 100644 --- a/database/seeds/ApiEndpointsSeeder.php +++ b/database/seeds/ApiEndpointsSeeder.php @@ -357,7 +357,7 @@ class ApiEndpointsSeeder extends Seeder ], ], [ - 'name' => 'remove-speaker-2-my-presentation', + 'name' => 'remove-speaker-from-my-presentation', 'route' => '/api/v1/speakers/me/presentations/{presentation_id}/speakers/{speaker_id}', 'http_method' => 'DELETE', 'scopes' => [ @@ -366,6 +366,26 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::WriteMySpeakersData, $current_realm) ], ], + [ + 'name' => 'add-moderator-2-my-presentation', + 'route' => '/api/v1/speakers/me/presentations/{presentation_id}/moderators/{speaker_id}', + 'http_method' => 'PUT', + 'scopes' => [ + sprintf(SummitScopes::WriteSummitData, $current_realm), + sprintf(SummitScopes::WriteSpeakersData, $current_realm), + sprintf(SummitScopes::WriteMySpeakersData, $current_realm) + ], + ], + [ + 'name' => 'remove-moderators-from-my-presentation', + 'route' => '/api/v1/speakers/me/presentations/{presentation_id}/moderators/{speaker_id}', + 'http_method' => 'DELETE', + 'scopes' => [ + sprintf(SummitScopes::WriteSummitData, $current_realm), + sprintf(SummitScopes::WriteSpeakersData, $current_realm), + sprintf(SummitScopes::WriteMySpeakersData, $current_realm) + ], + ], array( 'name' => 'create-my-speaker', 'route' => '/api/v1/speakers/me', diff --git a/tests/OAuth2TrackTagGroupsApiTest.php b/tests/OAuth2TrackTagGroupsApiTest.php index 52254654..b47c9f8e 100644 --- a/tests/OAuth2TrackTagGroupsApiTest.php +++ b/tests/OAuth2TrackTagGroupsApiTest.php @@ -207,7 +207,7 @@ final class OAuth2TrackTagGroupsApiTest extends ProtectedApiTest $this->assertResponseStatus(200); } - public function testSeedDefaultTrackTagGroups($summit_id = 25) + public function testSeedDefaultTrackTagGroups($summit_id = 26) { $params = [ @@ -228,7 +228,7 @@ final class OAuth2TrackTagGroupsApiTest extends ProtectedApiTest $content = $response->getContent(); $tags = json_decode($content); $this->assertTrue(!is_null($tags)); - $this->assertResponseStatus(200); + $this->assertResponseStatus(201); } /**