From 18bf5bfa04a177a555c933e73651a24086f31041 Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Wed, 4 Apr 2018 18:08:33 -0300 Subject: [PATCH] added endpoint update summit PUT /api/v1/summits/{id} Payload * name (sometimes|string|max:50) * start_date (sometimes|date_format:U) * end_date (required_with:start_date|date_format:U|after:start_date) * submission_begin_date (sometimes|date_format:U) * submission_end_date (required_with:submission_begin_date|date_format:U|after:submission_begin_date) * voting_begin_date (sometimes|date_format:U) * voting_end_date (required_with:voting_begin_date|date_format:U|after:voting_begin_date) * selection_begin_date (sometimes|date_format:U) * selection_end_date (required_with:selection_begin_date|date_format:U|after:selection_begin_date) * registration_begin_date (sometimes|date_format:U) * registration_end_date (required_with:registration_begin_date|date_format:U|after:registration_begin_date) * start_showing_venues_date (sometimes|date_format:U|before:start_date) * schedule_start_date (sometimes|date_format:U) * active (sometimes|boolean) * dates_label (sometimes|string) * time_zone_id (sometimes|timezone) check http://php.net/manual/en/timezones.php * external_summit_id (sometimes|string) * available_on_api (sometimes|boolean) * calendar_sync_name (sometimes|string|max:255) * calendar_sync_desc (sometimes|string) * link (sometimes|url) * registration_link (sometimes|url) * max_submission_allowed_per_user (sometimes|integer|min:1) Required scopes '%s/summits/write' Change-Id: Ib50c64994f9de5e8cfba0aaf2a990708a3c6afb9 --- app/Events/SummitAction.php | 45 +++++++++++++ app/Events/SummitDeleted.php | 22 ++++++ app/Events/SummitUpdated.php | 22 ++++++ .../SummitActionEntityEventFactory.php | 58 ++++++++++++++++ .../Summit/Repositories/ISummitRepository.php | 5 ++ app/Providers/EventServiceProvider.php | 13 ++++ .../Summit/DoctrineSummitRepository.php | 16 +++++ app/Services/Model/SummitService.php | 67 +++++++++++++++++++ resources/lang/en/not_found_errors.php | 2 + resources/lang/en/validation_errors.php | 3 +- tests/OAuth2SummitApiTest.php | 62 +++++++++++++++++ 11 files changed, 314 insertions(+), 1 deletion(-) create mode 100644 app/Events/SummitAction.php create mode 100644 app/Events/SummitDeleted.php create mode 100644 app/Events/SummitUpdated.php create mode 100644 app/Factories/EntityEvents/SummitActionEntityEventFactory.php diff --git a/app/Events/SummitAction.php b/app/Events/SummitAction.php new file mode 100644 index 00000000..87c41606 --- /dev/null +++ b/app/Events/SummitAction.php @@ -0,0 +1,45 @@ +summit_id = $summit_id; + } + + /** + * @return int + */ + public function getSummitId() + { + return $this->summit_id; + } +} \ No newline at end of file diff --git a/app/Events/SummitDeleted.php b/app/Events/SummitDeleted.php new file mode 100644 index 00000000..0485e56a --- /dev/null +++ b/app/Events/SummitDeleted.php @@ -0,0 +1,22 @@ +getById($event->getSummitId()); + + $owner_id = $resource_server_context->getCurrentUserExternalId(); + if (is_null($owner_id)) $owner_id = 0; + + + $entity_event = new SummitEntityEvent; + $entity_event->setEntityClassName('Summit'); + $entity_event->setEntityId($event->getSummitId()); + $entity_event->setType($type); + + if ($owner_id > 0) { + $member = $member_repository->getById($owner_id); + $entity_event->setOwner($member); + } + if(!is_null($summit)) + $entity_event->setSummit($summit); + + $entity_event->setMetadata(''); + + return $entity_event; + } +} \ No newline at end of file diff --git a/app/Models/Foundation/Summit/Repositories/ISummitRepository.php b/app/Models/Foundation/Summit/Repositories/ISummitRepository.php index da206efe..fd9a185d 100644 --- a/app/Models/Foundation/Summit/Repositories/ISummitRepository.php +++ b/app/Models/Foundation/Summit/Repositories/ISummitRepository.php @@ -23,6 +23,11 @@ interface ISummitRepository extends IBaseRepository */ public function getCurrent(); + /** + * @return Summit + */ + public function getActive(); + /** * @return Summit[] */ diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index d84625b4..551fc49f 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -32,6 +32,7 @@ use App\Factories\EntityEvents\PresentationMaterialUpdatedEntityEventFactory; use App\Factories\EntityEvents\PresentationSpeakerCreatedEntityEventFactory; use App\Factories\EntityEvents\PresentationSpeakerDeletedEntityEventFactory; use App\Factories\EntityEvents\PresentationSpeakerUpdatedEntityEventFactory; +use App\Factories\EntityEvents\SummitActionEntityEventFactory; use App\Factories\EntityEvents\SummitEventCreatedEntityEventFactory; use App\Factories\EntityEvents\SummitEventDeletedEntityEventFactory; use App\Factories\EntityEvents\SummitEventTypeActionEntityEventFactory; @@ -304,5 +305,17 @@ final class EventServiceProvider extends ServiceProvider { EntityEventPersister::persist(TrackGroupActionActionEntityEventFactory::build($event, 'DELETE')); }); + + // summits + + Event::listen(\App\Events\SummitUpdated::class, function($event) + { + EntityEventPersister::persist(SummitActionEntityEventFactory::build($event, 'UPDATE')); + }); + + Event::listen(\App\Events\SummitDeleted::class, function($event) + { + EntityEventPersister::persist(SummitActionEntityEventFactory::build($event, 'DELETE')); + }); } } diff --git a/app/Repositories/Summit/DoctrineSummitRepository.php b/app/Repositories/Summit/DoctrineSummitRepository.php index 9d24a396..33128899 100644 --- a/app/Repositories/Summit/DoctrineSummitRepository.php +++ b/app/Repositories/Summit/DoctrineSummitRepository.php @@ -89,4 +89,20 @@ final class DoctrineSummitRepository ->getQuery() ->getOneOrNullResult(); } + + /** + * @return Summit + */ + public function getActive() + { + $res = $this->getEntityManager()->createQueryBuilder() + ->select("s") + ->from(\models\summit\Summit::class, "s") + ->where('s.active = 1') + ->orderBy('s.begin_date', 'DESC') + ->getQuery() + ->getResult(); + if (count($res) == 0) return null; + return $res[0]; + } } \ No newline at end of file diff --git a/app/Services/Model/SummitService.php b/app/Services/Model/SummitService.php index 8501575c..77c01121 100644 --- a/app/Services/Model/SummitService.php +++ b/app/Services/Model/SummitService.php @@ -15,6 +15,8 @@ use App\Events\MyFavoritesAdd; use App\Events\MyFavoritesRemove; use App\Events\MyScheduleAdd; use App\Events\MyScheduleRemove; +use App\Events\SummitDeleted; +use App\Events\SummitUpdated; use App\Http\Utils\FileUploader; use App\Models\Foundation\Summit\Factories\SummitFactory; use App\Models\Utils\IntervalParser; @@ -1525,6 +1527,54 @@ final class SummitService extends AbstractService implements ISummitService { return $this->tx_service->transaction(function () use ($summit_id, $data) { + if(isset($data['name'])) { + + $former_summit = $this->summit_repository->getByName(trim($data['name'])); + if (!is_null($former_summit) && $former_summit->getId() != $summit_id) { + throw new ValidationException + ( + trans + ( + 'validation_errors.SummitService.updateSummit.NameAlreadyExists', + ['name' => $data['name']] + ) + ); + } + } + + if(isset($data['active'])) { + $active = boolval($data['active']); + $active_summit = $this->summit_repository->getActive(); + if ($active && !is_null($active_summit) && $active_summit->getId() != $summit_id) { + throw new ValidationException + ( + trans + ( + 'validation_errors.SummitService.updateSummit.SummitAlreadyActive', + ['active_summit_id' => $active_summit->getId()] + ) + ); + } + } + + $summit = $this->summit_repository->getById($summit_id); + + if(is_null($summit)){ + throw new EntityNotFoundException + ( + trans + ( + 'not_found_errors.SummitService.updateSummit.SummitNotFound', + ['summit_id' => $summit_id] + ) + ); + } + + $summit = SummitFactory::populate($summit, $data); + + Event::fire(new SummitUpdated($summit_id)); + + return $summit; }); } @@ -1538,6 +1588,23 @@ final class SummitService extends AbstractService implements ISummitService { return $this->tx_service->transaction(function () use ($summit_id) { + $summit = $this->summit_repository->getById($summit_id); + + if(is_null($summit)){ + throw new EntityNotFoundException + ( + trans + ( + 'not_found_errors.SummitService.deleteSummit.SummitNotFound', + ['summit_id' => $summit_id] + ) + ); + } + + $this->summit_repository->delete($summit); + + Event::fire(new SummitDeleted($summit_id)); + }); } } \ No newline at end of file diff --git a/resources/lang/en/not_found_errors.php b/resources/lang/en/not_found_errors.php index f9033dc8..53b12f47 100644 --- a/resources/lang/en/not_found_errors.php +++ b/resources/lang/en/not_found_errors.php @@ -68,4 +68,6 @@ return [ 'PresentationCategoryGroupService.associateAllowedGroup2TrackGroup.GroupNotFound' => 'group :group_id does not exists.', 'PresentationCategoryGroupService.disassociateAllowedGroup2TrackGroup.TrackGroupNotFound' => 'track group :track_group_id does not exists on summit :summit_id', 'PresentationCategoryGroupService.disassociateAllowedGroup2TrackGroup.GroupNotFound' => 'group :group_id does not exists.', + 'SummitService.updateSummit.SummitNotFound' => 'summit :summit_id not found', + 'SummitService.deleteSummit.SummitNotFound' => 'summit :summit_id not found', ]; \ No newline at end of file diff --git a/resources/lang/en/validation_errors.php b/resources/lang/en/validation_errors.php index 048fa5b1..534a6c84 100644 --- a/resources/lang/en/validation_errors.php +++ b/resources/lang/en/validation_errors.php @@ -67,5 +67,6 @@ return [ 'PresentationCategoryGroupService.addTrackGroup.NameAlreadyExists' => 'name :name already exists for summit :summit_id', // SummitService 'SummitService.AddSummit.NameAlreadyExists' => 'name :name its already being assigned to another summit', - + 'SummitService.updateSummit.NameAlreadyExists'=> 'name :name its already being assigned to another summit', + 'SummitService.updateSummit.SummitAlreadyActive' => 'summit :active_summit_id is already activated please deactivate it to set current summit as active' ]; \ No newline at end of file diff --git a/tests/OAuth2SummitApiTest.php b/tests/OAuth2SummitApiTest.php index fed325a2..b9345add 100644 --- a/tests/OAuth2SummitApiTest.php +++ b/tests/OAuth2SummitApiTest.php @@ -184,6 +184,68 @@ final class OAuth2SummitApiTest extends ProtectedApiTest return $summit; } + public function testUpdateSummitAlreadyActiveError(){ + $summit = $this->testAddSummit(); + $params = [ + 'id' => $summit->id + ]; + $data = [ + 'active' => 1 + ]; + + $headers = [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action( + "PUT", + "OAuth2SummitApiController@updateSummit", + $params, + [], + [], + [], + $headers, + json_encode($data) + ); + + $content = $response->getContent(); + $this->assertResponseStatus(412); + } + + public function testUpdateSummitTitle(){ + $summit = $this->testAddSummit(); + $params = [ + 'id' => $summit->id + ]; + $data = [ + 'name' => $summit->name.' update!' + ]; + + $headers = [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action( + "PUT", + "OAuth2SummitApiController@updateSummit", + $params, + [], + [], + [], + $headers, + json_encode($data) + ); + + $content = $response->getContent(); + $this->assertResponseStatus(201); + $summit = json_decode($content); + $this->assertTrue(!is_null($summit)); + + return $summit; + } + public function testGetSummitMin($summit_id = 23) {