From fcec607f95442b19f4ca535571d9741ab42e0a33 Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Thu, 22 Feb 2018 07:03:45 -0300 Subject: [PATCH] Added new endpoint to seed default event types per summit POST /api/v1/summits/{id}/event-types/seed-defaults Change-Id: I8466fbbdec8410eb160545442b351187b8c2293d --- .../OAuth2SummitsEventTypesApiController.php | 46 +++ app/Http/routes.php | 1 + .../Defaults/DefaultPresentationType.php | 269 ++++++++++++++++++ .../Defaults/DefaultSummitEventType.php | 205 +++++++++++++ .../Summit/Events/SummitEventType.php | 43 ++- .../Summit/Factories/SummitEventFactory.php | 11 +- .../IDefaultSummitEventTypeRepository.php | 23 ++ app/Models/Foundation/Summit/Summit.php | 2 +- app/Models/Utils/SilverstripeBaseModel.php | 7 + app/Repositories/RepositoriesProvider.php | 9 + ...ctrineDefaultSummitEventTypeRepository.php | 33 +++ .../Model/ISummitEventTypeService.php | 8 + app/Services/Model/SummitEventTypeService.php | 36 ++- app/Services/Model/SummitService.php | 7 +- database/seeds/ApiEndpointsSeeder.php | 9 + tests/OAuth2EventTypesApiTest.php | 28 ++ 16 files changed, 725 insertions(+), 12 deletions(-) create mode 100644 app/Models/Foundation/Summit/Defaults/DefaultPresentationType.php create mode 100644 app/Models/Foundation/Summit/Defaults/DefaultSummitEventType.php create mode 100644 app/Models/Foundation/Summit/Repositories/IDefaultSummitEventTypeRepository.php create mode 100644 app/Repositories/Summit/DoctrineDefaultSummitEventTypeRepository.php diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitsEventTypesApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitsEventTypesApiController.php index d8899532..220af018 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitsEventTypesApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitsEventTypesApiController.php @@ -31,6 +31,7 @@ use utils\FilterParser; use utils\OrderParser; use utils\PagingInfo; use Exception; +use utils\PagingResponse; /** * Class OAuth2SummitsEventTypesApiController * @package App\Http\Controllers @@ -47,6 +48,13 @@ final class OAuth2SummitsEventTypesApiController extends OAuth2ProtectedControll */ private $event_type_service; + /** + * OAuth2SummitsEventTypesApiController constructor. + * @param ISummitEventTypeRepository $repository + * @param ISummitRepository $summit_repository + * @param ISummitEventTypeService $event_type_service + * @param IResourceServerContext $resource_server_context + */ public function __construct ( ISummitEventTypeRepository $repository, @@ -442,4 +450,42 @@ final class OAuth2SummitsEventTypesApiController extends OAuth2ProtectedControll return $this->error500($ex); } } + + /** + * @param $summit_id + * @return mixed + */ + public function seedDefaultEventTypesBySummit($summit_id){ + try { + + $summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + $event_types = $this->event_type_service->seedDefaultEventTypes($summit); + + $response = new PagingResponse + ( + count($event_types), + count($event_types), + 1, + 1, + $event_types + ); + + return $this->created($response->toArray()); + } + 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 b10710a7..43b8d921 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -296,6 +296,7 @@ Route::group([ Route::group(['prefix' => 'event-types'], function () { Route::get('', 'OAuth2SummitsEventTypesApiController@getAllBySummit'); Route::get('csv', 'OAuth2SummitsEventTypesApiController@getAllBySummitCSV'); + Route::post('seed-defaults', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitsEventTypesApiController@seedDefaultEventTypesBySummit']); Route::post('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitsEventTypesApiController@addEventTypeBySummit']); Route::group(['prefix' => '{event_type_id}'], function () { Route::get('', 'OAuth2SummitsEventTypesApiController@getEventTypeBySummit'); diff --git a/app/Models/Foundation/Summit/Defaults/DefaultPresentationType.php b/app/Models/Foundation/Summit/Defaults/DefaultPresentationType.php new file mode 100644 index 00000000..4fb471fb --- /dev/null +++ b/app/Models/Foundation/Summit/Defaults/DefaultPresentationType.php @@ -0,0 +1,269 @@ +max_speakers; + } + + /** + * @param int $max_speakers + */ + public function setMaxSpeakers($max_speakers) + { + $this->max_speakers = $max_speakers; + } + + /** + * @return int + */ + public function getMinSpeakers() + { + return $this->min_speakers; + } + + /** + * @param int $min_speakers + */ + public function setMinSpeakers($min_speakers) + { + $this->min_speakers = $min_speakers; + } + + /** + * @return int + */ + public function getMaxModerators() + { + return $this->max_moderators; + } + + /** + * @param int $max_moderators + */ + public function setMaxModerators($max_moderators) + { + $this->max_moderators = $max_moderators; + } + + /** + * @return int + */ + public function getMinModerators() + { + return $this->min_moderators; + } + + /** + * @param int $min_moderators + */ + public function setMinModerators($min_moderators) + { + $this->min_moderators = $min_moderators; + } + + /** + * @return bool + */ + public function isUseSpeakers() + { + return $this->use_speakers; + } + + /** + * @param bool $use_speakers + */ + public function setUseSpeakers($use_speakers) + { + $this->use_speakers = $use_speakers; + } + + /** + * @return bool + */ + public function isAreSpeakersMandatory() + { + return $this->are_speakers_mandatory; + } + + /** + * @param bool $are_speakers_mandatory + */ + public function setAreSpeakersMandatory($are_speakers_mandatory) + { + $this->are_speakers_mandatory = $are_speakers_mandatory; + } + + /** + * @return bool + */ + public function isUseModerator() + { + return $this->use_moderator; + } + + /** + * @param bool $use_moderator + */ + public function setUseModerator($use_moderator) + { + $this->use_moderator = $use_moderator; + } + + /** + * @return bool + */ + public function isModeratorMandatory() + { + return $this->is_moderator_mandatory; + } + + /** + * @param bool $is_moderator_mandatory + */ + public function setIsModeratorMandatory($is_moderator_mandatory) + { + $this->is_moderator_mandatory = $is_moderator_mandatory; + } + + /** + * @return bool + */ + public function isShouldBeAvailableOnCfp() + { + return $this->should_be_available_on_cfp; + } + + /** + * @param bool $should_be_available_on_cfp + */ + public function setShouldBeAvailableOnCfp($should_be_available_on_cfp) + { + $this->should_be_available_on_cfp = $should_be_available_on_cfp; + } + + /** + * @return string + */ + public function getModeratorLabel() + { + return $this->moderator_label; + } + + /** + * @param string $moderator_label + */ + public function setModeratorLabel($moderator_label) + { + $this->moderator_label = $moderator_label; + } + + protected function newType(){ + return new PresentationType(); + } + + /** + * @param Summit $summit + * @return SummitEventType + */ + public function buildType(Summit $summit){ + $new_type = parent::buildType($summit); + $new_type->setMaxSpeakers($this->max_speakers); + $new_type->setMinSpeakers($this->min_speakers); + $new_type->setMaxModerators($this->max_moderators); + $new_type->setMinModerators($this->min_moderators); + $new_type->setUseSpeakers($this->use_speakers); + $new_type->setAreSpeakersMandatory($this->are_speakers_mandatory); + $new_type->setUseModerator($this->use_moderator); + $new_type->setIsModeratorMandatory($this->is_moderator_mandatory); + $new_type->setShouldBeAvailableOnCfp($this->should_be_available_on_cfp); + $new_type->setModeratorLabel($this->moderator_label); + return $new_type; + } +} \ No newline at end of file diff --git a/app/Models/Foundation/Summit/Defaults/DefaultSummitEventType.php b/app/Models/Foundation/Summit/Defaults/DefaultSummitEventType.php new file mode 100644 index 00000000..7f1ffe1f --- /dev/null +++ b/app/Models/Foundation/Summit/Defaults/DefaultSummitEventType.php @@ -0,0 +1,205 @@ +type; + } + + /** + * @param string $type + */ + public function setType($type) + { + $this->type = $type; + } + + /** + * @return string + */ + public function getColor() + { + return $this->color; + } + + /** + * @param string $color + */ + public function setColor($color) + { + $this->color = $color; + } + + /** + * @return bool + */ + public function isBlackoutTimes() + { + return $this->blackout_times; + } + + /** + * @param bool $blackout_times + */ + public function setBlackoutTimes($blackout_times) + { + $this->blackout_times = $blackout_times; + } + + /** + * @return bool + */ + public function isUseSponsors() + { + return $this->use_sponsors; + } + + /** + * @param bool $use_sponsors + */ + public function setUseSponsors($use_sponsors) + { + $this->use_sponsors = $use_sponsors; + } + + /** + * @return bool + */ + public function isAreSponsorsMandatory() + { + return $this->are_sponsors_mandatory; + } + + /** + * @param bool $are_sponsors_mandatory + */ + public function setAreSponsorsMandatory($are_sponsors_mandatory) + { + $this->are_sponsors_mandatory = $are_sponsors_mandatory; + } + + /** + * @return bool + */ + public function isAllowsAttachment() + { + return $this->allows_attachment; + } + + /** + * @param bool $allows_attachment + */ + public function setAllowsAttachment($allows_attachment) + { + $this->allows_attachment = $allows_attachment; + } + + /** + * @return bool + */ + public function isPrivate() + { + return $this->is_private; + } + + /** + * @param bool $is_private + */ + public function setIsPrivate($is_private) + { + $this->is_private = $is_private; + } + + protected function newType(){ + return new SummitEventType(); + } + + /** + * @param Summit $summit + * @return SummitEventType + */ + public function buildType(Summit $summit){ + $new_type = $this->newType(); + $new_type->setSummit($summit); + $new_type->setType($this->type); + $new_type->setColor($this->color); + $new_type->setBlackoutTimes($this->blackout_times); + $new_type->setUseSponsors($this->use_sponsors); + $new_type->setAreSponsorsMandatory($this->are_sponsors_mandatory); + $new_type->setAllowsAttachment($this->allows_attachment); + $new_type->setIsPrivate($this->is_private); + $new_type->setAsDefault(); + return $new_type; + } + +} \ No newline at end of file diff --git a/app/Models/Foundation/Summit/Events/SummitEventType.php b/app/Models/Foundation/Summit/Events/SummitEventType.php index c929fb1c..0ba323f8 100644 --- a/app/Models/Foundation/Summit/Events/SummitEventType.php +++ b/app/Models/Foundation/Summit/Events/SummitEventType.php @@ -123,11 +123,29 @@ class SummitEventType extends SilverstripeBaseModel /** * @param string $type + * @param int $summit_id * @return bool */ - static public function isPrivate($type){ + static public function isPrivateType($type, $summit_id){ $private_types = [ISummitEventType::GroupsEvents]; return in_array($type, $private_types); + + try{ + $sql = <<execute(['summit_id' => $summit->getId(), 'type' => $type]); + $res = $stmt->fetchAll(\PDO::FETCH_COLUMN); + return count($res) > 0 ; + } + catch (\Exception $ex){ + + } + return false; } /** @@ -169,6 +187,12 @@ class SummitEventType extends SilverstripeBaseModel const ClassName = 'EVENT_TYPE'; + /** + * @ORM\Column(name="IsPrivate", type="boolean") + * @var bool + */ + protected $is_private; + /** * @return boolean */ @@ -219,6 +243,23 @@ class SummitEventType extends SilverstripeBaseModel $this->blackout_times = false; $this->are_sponsors_mandatory = false; $this->allows_attachment = false; + $this->is_private = false; + } + + /** + * @return bool + */ + public function isPrivate() + { + return $this->is_private; + } + + /** + * @param bool $is_private + */ + public function setIsPrivate($is_private) + { + $this->is_private = $is_private; } } diff --git a/app/Models/Foundation/Summit/Factories/SummitEventFactory.php b/app/Models/Foundation/Summit/Factories/SummitEventFactory.php index 56e5ce22..d7a735e8 100644 --- a/app/Models/Foundation/Summit/Factories/SummitEventFactory.php +++ b/app/Models/Foundation/Summit/Factories/SummitEventFactory.php @@ -1,4 +1,4 @@ -getType())) + if(SummitEventType::isPrivateType($type->getType(), $summit->getId())) $event = new SummitGroupEvent(); if($type->isAllowsAttachment()) $event = new SummitEventWithFile(); + $event->setSummit($summit); + return $event; } } \ No newline at end of file diff --git a/app/Models/Foundation/Summit/Repositories/IDefaultSummitEventTypeRepository.php b/app/Models/Foundation/Summit/Repositories/IDefaultSummitEventTypeRepository.php new file mode 100644 index 00000000..6a0e8a34 --- /dev/null +++ b/app/Models/Foundation/Summit/Repositories/IDefaultSummitEventTypeRepository.php @@ -0,0 +1,23 @@ +getType()->getType())) { + if (SummitEventType::isPrivateType($summit_event->getType()->getType(), $summit_event->getSummitId())) { if (is_null($member)) return false; diff --git a/app/Models/Utils/SilverstripeBaseModel.php b/app/Models/Utils/SilverstripeBaseModel.php index e3a526a5..a98de814 100644 --- a/app/Models/Utils/SilverstripeBaseModel.php +++ b/app/Models/Utils/SilverstripeBaseModel.php @@ -140,5 +140,12 @@ class SilverstripeBaseModel extends BaseEntity return Registry::getManager(self::EntityManager); } + /** + * @return EntityManager + */ + protected static function getEMStatic(){ + return Registry::getManager(self::EntityManager); + } + const EntityManager = 'ss'; } \ No newline at end of file diff --git a/app/Repositories/RepositoriesProvider.php b/app/Repositories/RepositoriesProvider.php index a3b1940c..36102f6a 100644 --- a/app/Repositories/RepositoriesProvider.php +++ b/app/Repositories/RepositoriesProvider.php @@ -11,6 +11,8 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ +use App\Models\Foundation\Summit\Defaults\DefaultSummitEventType; +use App\Models\Foundation\Summit\Repositories\IDefaultSummitEventTypeRepository; use App\Models\Foundation\Summit\Repositories\IPresentationSpeakerSummitAssistanceConfirmationRequestRepository; use App\Models\Foundation\Summit\Repositories\ISummitEventTypeRepository; use Illuminate\Support\Facades\App; @@ -272,5 +274,12 @@ final class RepositoriesProvider extends ServiceProvider return EntityManager::getRepository(SummitEventType::class); } ); + + App::singleton( + IDefaultSummitEventTypeRepository::class, + function(){ + return EntityManager::getRepository(DefaultSummitEventType::class); + } + ); } } \ No newline at end of file diff --git a/app/Repositories/Summit/DoctrineDefaultSummitEventTypeRepository.php b/app/Repositories/Summit/DoctrineDefaultSummitEventTypeRepository.php new file mode 100644 index 00000000..6ff20c00 --- /dev/null +++ b/app/Repositories/Summit/DoctrineDefaultSummitEventTypeRepository.php @@ -0,0 +1,33 @@ +tx_service = $tx_service; - $this->repository = $repository; + $this->tx_service = $tx_service; + $this->repository = $repository; + $this->default_event_types_repository = $default_event_types_repository; } /** @@ -140,4 +149,27 @@ final class SummitEventTypeService implements ISummitEventTypeService }); } + + /** + * @param Summit $summit + * @return SummitEventType[] + * @throws EntityNotFoundException + * @throws ValidationException + */ + public function seedDefaultEventTypes(Summit $summit) + { + return $this->tx_service->transaction(function() use($summit){ + $added_types = []; + $default_types = $this->default_event_types_repository->getAll(); + foreach ($default_types as $default_type){ + $former_type = $summit->getEventTypeByType($default_type->getType()); + if(!is_null($former_type)) continue; + $new_type = $default_type->buildType($summit); + $summit->addEventType($new_type); + $added_types[] = $new_type; + } + + return $added_types; + }); + } } \ No newline at end of file diff --git a/app/Services/Model/SummitService.php b/app/Services/Model/SummitService.php index 7c1a4361..19eeff92 100644 --- a/app/Services/Model/SummitService.php +++ b/app/Services/Model/SummitService.php @@ -581,8 +581,9 @@ final class SummitService implements ISummitService return false; } - $old_is_private = SummitEventType::isPrivate($old_event_type->getType()); - $new_is_private = SummitEventType::isPrivate($event_type->getType()); + $old_is_private = SummitEventType::isPrivateType($old_event_type->getType(), $old_event_type->getSummitId()); + $new_is_private = SummitEventType::isPrivateType($event_type->getType(), $event_type->getSummitId()); + if((!$old_is_private && $new_is_private) || ($old_is_private && !$new_is_private)) return false; @@ -660,7 +661,7 @@ final class SummitService implements ISummitService // new event if (is_null($event)) - $event = SummitEventFactory::build($event_type); + $event = SummitEventFactory::build($event_type, $summit); // main data diff --git a/database/seeds/ApiEndpointsSeeder.php b/database/seeds/ApiEndpointsSeeder.php index 85c930c3..8f24ffd5 100644 --- a/database/seeds/ApiEndpointsSeeder.php +++ b/database/seeds/ApiEndpointsSeeder.php @@ -573,6 +573,15 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::WriteSummitData, $current_realm) ], ], + [ + 'name' => 'seed-default-event-types', + 'route' => '/api/v1/summits/{id}/event-types/seed-defaults', + 'http_method' => 'POST', + 'scopes' => [ + sprintf(SummitScopes::WriteEventTypeData, $current_realm), + sprintf(SummitScopes::WriteSummitData, $current_realm) + ], + ], [ 'name' => 'update-event-type', 'route' => '/api/v1/summits/{id}/event-types/{event_type_id}', diff --git a/tests/OAuth2EventTypesApiTest.php b/tests/OAuth2EventTypesApiTest.php index 180f30f8..1ae43a4a 100644 --- a/tests/OAuth2EventTypesApiTest.php +++ b/tests/OAuth2EventTypesApiTest.php @@ -304,4 +304,32 @@ final class OAuth2EventTypesApiTest extends ProtectedApiTest $this->assertResponseStatus(204); } + public function testSeedDefaultEventTYpes($summit_id = 23){ + $params = [ + 'id' => $summit_id, + ]; + + $headers = [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action( + "POST", + "OAuth2SummitsEventTypesApiController@seedDefaultEventTypesBySummit", + $params, + [], + [], + [], + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(201); + $event_types = json_decode($content); + $this->assertTrue(!is_null($event_types)); + return $event_types; + } + + } \ No newline at end of file