From 19161fb734e5762106be9344ab23b5103b1b47c9 Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Mon, 5 Mar 2018 15:58:34 -0300 Subject: [PATCH] Added enpoint update summit venue floor PUT /api/v1/summits/{id}/locations/venues/{venue_id}/floors/{floor_id} Payload * name (sometimes|string|max:50) * number (sometimes|integer) * description (sometimes|string) Change-Id: I575680d5f32814393eeac655d55227dd1cae349a --- app/Events/FloorAction.php | 72 +++++++++++ app/Events/FloorDeleted.php | 22 ++++ app/Events/FloorInserted.php | 22 ++++ app/Events/FloorUpdated.php | 22 ++++ app/Events/SummitEventTypeAction.php | 75 ++++++++++++ app/Events/SummitEventTypeDeleted.php | 18 +++ app/Events/SummitEventTypeInserted.php | 22 ++++ app/Events/SummitEventTypeUpdated.php | 22 ++++ .../FloorActionEntityEventFactory.php | 59 +++++++++ ...ummitEventTypeActionEntityEventFactory.php | 58 +++++++++ .../TrackActionEntityEventFactory.php | 2 +- .../OAuth2SummitLocationsApiController.php | 54 +++++++++ app/Http/routes.php | 5 +- app/Providers/EventServiceProvider.php | 35 ++++++ app/Services/Model/LocationService.php | 113 +++++++++++++++++- app/Services/Model/SummitEventTypeService.php | 54 ++++++++- resources/lang/en/not_found_errors.php | 4 + resources/lang/en/validation_errors.php | 4 +- tests/OAuth2SummitLocationsApiTest.php | 51 +++++++- 19 files changed, 702 insertions(+), 12 deletions(-) create mode 100644 app/Events/FloorAction.php create mode 100644 app/Events/FloorDeleted.php create mode 100644 app/Events/FloorInserted.php create mode 100644 app/Events/FloorUpdated.php create mode 100644 app/Events/SummitEventTypeAction.php create mode 100644 app/Events/SummitEventTypeDeleted.php create mode 100644 app/Events/SummitEventTypeInserted.php create mode 100644 app/Events/SummitEventTypeUpdated.php create mode 100644 app/Factories/EntityEvents/FloorActionEntityEventFactory.php create mode 100644 app/Factories/EntityEvents/SummitEventTypeActionEntityEventFactory.php diff --git a/app/Events/FloorAction.php b/app/Events/FloorAction.php new file mode 100644 index 00000000..66938b3a --- /dev/null +++ b/app/Events/FloorAction.php @@ -0,0 +1,72 @@ +summit_id = $summit_id; + $this->venue_id = $venue_id; + $this->floor_id = $floor_id; + } + + /** + * @return int + */ + public function getFloorId() + { + return $this->floor_id; + } + + /** + * @return int + */ + public function getVenueId() + { + return $this->venue_id; + } + + public function getSummitId(){ + return $this->summit_id; + } + +} \ No newline at end of file diff --git a/app/Events/FloorDeleted.php b/app/Events/FloorDeleted.php new file mode 100644 index 00000000..9c7e4a72 --- /dev/null +++ b/app/Events/FloorDeleted.php @@ -0,0 +1,22 @@ +event_type_id = $event_type_id; + $this->class_name = $class_name; + $this->summit_id = $summit_id; + } + + /** + * @return int + */ + public function getEventTypeId() + { + return $this->event_type_id; + } + + /** + * @return string + */ + public function getClassName() + { + return $this->class_name; + } + + /** + * @return int + */ + public function getSummitId() + { + return $this->summit_id; + } + +} \ No newline at end of file diff --git a/app/Events/SummitEventTypeDeleted.php b/app/Events/SummitEventTypeDeleted.php new file mode 100644 index 00000000..29b06ea3 --- /dev/null +++ b/app/Events/SummitEventTypeDeleted.php @@ -0,0 +1,18 @@ +getById($event->getSummitId()); + $owner_id = $resource_server_context->getCurrentUserExternalId(); + + if (is_null($owner_id)) $owner_id = 0; + + $entity_event = new SummitEntityEvent; + $entity_event->setEntityClassName('SummitVenueFloor'); + $entity_event->setEntityId($event->getFloorId()); + $entity_event->setType($type); + + if ($owner_id > 0) { + $member = $member_repository->getById($owner_id); + $entity_event->setOwner($member); + } + + $entity_event->setSummit($summit); + $entity_event->setMetadata(json_encode([ + 'venue_id' => $event->getVenueId() + ])); + + return $entity_event; + } +} \ No newline at end of file diff --git a/app/Factories/EntityEvents/SummitEventTypeActionEntityEventFactory.php b/app/Factories/EntityEvents/SummitEventTypeActionEntityEventFactory.php new file mode 100644 index 00000000..7a45da16 --- /dev/null +++ b/app/Factories/EntityEvents/SummitEventTypeActionEntityEventFactory.php @@ -0,0 +1,58 @@ +getById($event->getSummitId()); + + $owner_id = $resource_server_context->getCurrentUserExternalId(); + if (is_null($owner_id)) $owner_id = 0; + + + $entity_event = new SummitEntityEvent; + $entity_event->setEntityClassName($event->getClassName()); + $entity_event->setEntityId($event->getEventTypeId()); + $entity_event->setType($type); + + if ($owner_id > 0) { + $member = $member_repository->getById($owner_id); + $entity_event->setOwner($member); + } + + $entity_event->setSummit($summit); + $entity_event->setMetadata(''); + + return $entity_event; + } +} \ No newline at end of file diff --git a/app/Factories/EntityEvents/TrackActionEntityEventFactory.php b/app/Factories/EntityEvents/TrackActionEntityEventFactory.php index 5556be2d..b22452f7 100644 --- a/app/Factories/EntityEvents/TrackActionEntityEventFactory.php +++ b/app/Factories/EntityEvents/TrackActionEntityEventFactory.php @@ -42,7 +42,7 @@ final class TrackActionEntityEventFactory $entity_event = new SummitEntityEvent; - $entity_event->setEntityClassName(PresentationCategory::class); + $entity_event->setEntityClassName('PresentationCategory'); $entity_event->setEntityId($event->getTrackId()); $entity_event->setType($type); diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php index 57cf587b..94ec602f 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php @@ -937,6 +937,56 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController } } + /** + * @param $summit_id + * @param $venue_id + * @param $floor_id + * @return mixed + */ + public function updateVenueFloor($summit_id, $venue_id, $floor_id){ + try { + if(!Request::isJson()) return $this->error403(); + $payload = Input::json()->all(); + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + $payload['class_name'] = SummitAirport::ClassName; + $rules = [ + 'name' => 'sometimes|string|max:50', + 'number' => 'sometimes|integer', + 'description' => 'sometimes|string', + ]; + // Creates a Validator instance and validates the data. + $validation = Validator::make($payload, $rules); + + if ($validation->fails()) { + $messages = $validation->messages()->toArray(); + + return $this->error412 + ( + $messages + ); + } + + $floor = $this->location_service->updateVenueFloor($summit, $venue_id, $floor_id, $payload); + + return $this->updated(SerializerRegistry::getInstance()->getSerializer($floor)->serialize()); + } + 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 $summit_id * @param $hotel_id @@ -1108,4 +1158,8 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController return $this->error500($ex); } } + + public function deleteVenueFloor($summit_id, $venue_id, $floor_id){ + + } } \ No newline at end of file diff --git a/app/Http/routes.php b/app/Http/routes.php index ad34868d..aea9531d 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -293,8 +293,11 @@ Route::group([ Route::group(['prefix' => 'floors'], function () { Route::post('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitLocationsApiController@addVenueFloor']); + Route::group(['prefix' => '{floor_id}'], function () { + Route::put('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitLocationsApiController@updateVenueFloor']); + Route::delete('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitLocationsApiController@deleteVenueFloor']); + }); }); - }); }); diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index ef0661e5..f183bd43 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -19,6 +19,7 @@ use App\Factories\AssetsSyncRequest\FileCreatedAssetSyncRequestFactory; use App\Factories\CalendarAdminActionSyncWorkRequest\AdminSummitLocationActionSyncWorkRequestFactory; use App\Factories\CalendarAdminActionSyncWorkRequest\SummitEventDeletedCalendarSyncWorkRequestFactory; use App\Factories\CalendarAdminActionSyncWorkRequest\SummitEventUpdatedCalendarSyncWorkRequestFactory; +use App\Factories\EntityEvents\FloorActionEntityEventFactory; use App\Factories\EntityEvents\LocationActionEntityEventFactory; use App\Factories\EntityEvents\MyFavoritesAddEntityEventFactory; use App\Factories\EntityEvents\MyFavoritesRemoveEntityEventFactory; @@ -32,6 +33,7 @@ use App\Factories\EntityEvents\PresentationSpeakerDeletedEntityEventFactory; use App\Factories\EntityEvents\PresentationSpeakerUpdatedEntityEventFactory; use App\Factories\EntityEvents\SummitEventCreatedEntityEventFactory; use App\Factories\EntityEvents\SummitEventDeletedEntityEventFactory; +use App\Factories\EntityEvents\SummitEventTypeActionEntityEventFactory; use App\Factories\EntityEvents\SummitEventUpdatedEntityEventFactory; use App\Factories\EntityEvents\TrackActionEntityEventFactory; use App\Services\Utils\SCPFileUploader; @@ -138,6 +140,23 @@ final class EventServiceProvider extends ServiceProvider EntityEventPersister::persist_list(PresentationSpeakerDeletedEntityEventFactory::build($event)); }); + // event types + + Event::listen(\App\Events\SummitEventTypeInserted::class, function($event) + { + EntityEventPersister::persist(SummitEventTypeActionEntityEventFactory::build($event, 'INSERT')); + }); + + Event::listen(\App\Events\SummitEventTypeUpdated::class, function($event) + { + EntityEventPersister::persist(SummitEventTypeActionEntityEventFactory::build($event, 'UPDATE')); + }); + + Event::listen(\App\Events\SummitEventTypeDeleted::class, function($event) + { + EntityEventPersister::persist(SummitEventTypeActionEntityEventFactory::build($event, 'DELETE')); + }); + // tracks Event::listen(\App\Events\TrackInserted::class, function($event) @@ -186,5 +205,21 @@ final class EventServiceProvider extends ServiceProvider } }); + Event::listen(\App\Events\FloorInserted::class, function($event) + { + EntityEventPersister::persist(FloorActionEntityEventFactory::build($event, 'INSERT')); + }); + + Event::listen(\App\Events\FloorUpdated::class, function($event) + { + EntityEventPersister::persist(FloorActionEntityEventFactory::build($event, 'UPDATE')); + + }); + + Event::listen(\App\Events\FloorDeleted::class, function($event) + { + EntityEventPersister::persist(FloorActionEntityEventFactory::build($event, 'DELETE')); + }); + } } diff --git a/app/Services/Model/LocationService.php b/app/Services/Model/LocationService.php index 884f6a6f..8734c4a7 100644 --- a/app/Services/Model/LocationService.php +++ b/app/Services/Model/LocationService.php @@ -12,6 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ +use App\Events\FloorInserted; +use App\Events\FloorUpdated; use App\Events\LocationDeleted; use App\Events\LocationInserted; use App\Events\LocationUpdated; @@ -310,16 +312,16 @@ final class LocationService implements ILocationService */ public function addVenueFloor(Summit $summit, $venue_id, array $data) { - return $this->tx_service->transaction(function () use ($summit, $venue_id, $data) { + $floor = $this->tx_service->transaction(function () use ($summit, $venue_id, $data) { $venue = $summit->getLocation($venue_id); if(is_null($venue)){ - throw new ValidationException + throw new EntityNotFoundException ( trans ( - 'validation_errors.LocationService.addVenueFloor.VenueNotFound', + 'not_found_errors.LocationService.addVenueFloor.VenueNotFound', [ 'summit_id' => $summit->getId(), 'venue_id' => $venue_id, @@ -379,6 +381,18 @@ final class LocationService implements ILocationService return $floor; }); + + Event::fire + ( + new FloorInserted + ( + $floor->getVenue()->getSummitId(), + $floor->getVenueId(), + $floor->getId() + ) + ); + + return $floor; } /** @@ -393,7 +407,100 @@ final class LocationService implements ILocationService public function updateVenueFloor(Summit $summit, $venue_id, $floor_id, array $data) { return $this->tx_service->transaction(function () use ($summit, $venue_id, $floor_id, $data) { + $venue = $summit->getLocation($venue_id); + if(is_null($venue)){ + throw new EntityNotFoundException + ( + trans + ( + 'not_found_errors.LocationService.updateVenueFloor.VenueNotFound', + [ + 'summit_id' => $summit->getId(), + 'venue_id' => $venue_id, + ] + ) + ); + } + + if(!$venue instanceof SummitVenue){ + throw new ValidationException + ( + trans + ( + 'validation_errors.LocationService.updateVenueFloor.VenueNotFound', + [ + 'summit_id' => $summit->getId(), + 'venue_id' => $venue_id, + ] + ) + ); + } + + if(isset($data['name'])) { + $former_floor = $venue->getFloorByName(trim($data['name'])); + + if (!is_null($former_floor) && $former_floor->getId() != $floor_id) { + throw new ValidationException( + trans + ( + 'validation_errors.LocationService.addVenueFloor.FloorNameAlreadyExists', + [ + 'venue_id' => $venue_id, + 'floor_name' => trim($data['name']) + ] + ) + ); + } + } + + if(isset($data['number'])) { + $former_floor = $venue->getFloorByNumber(intval($data['number'])); + + if (!is_null($former_floor) && $former_floor->getId() != $floor_id) { + throw new ValidationException + ( + trans + ( + 'validation_errors.LocationService.addVenueFloor.FloorNumberAlreadyExists', + [ + 'venue_id' => $venue_id, + 'floor_number' => intval($data['number']) + ] + ) + ); + } + + } + + $floor = $venue->getFloor($floor_id); + if(is_null($floor)){ + throw new EntityNotFoundException + ( + trans + ( + 'not_found_errors.LocationService.updateVenueFloor.FloorNotFound', + [ + 'floor_id' => $floor_id, + 'venue_id' => $venue_id + ] + ) + ); + } + + $floor = SummitVenueFloorFactory::populate($floor, $data); + + Event::fire + ( + new FloorUpdated + ( + $floor->getVenue()->getSummitId(), + $floor->getVenueId(), + $floor->getId() + ) + ); + + return $floor; }); } diff --git a/app/Services/Model/SummitEventTypeService.php b/app/Services/Model/SummitEventTypeService.php index a1b0051d..42d0a030 100644 --- a/app/Services/Model/SummitEventTypeService.php +++ b/app/Services/Model/SummitEventTypeService.php @@ -11,10 +11,14 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ +use App\Events\SummitEventTypeDeleted; +use App\Events\SummitEventTypeInserted; +use App\Events\SummitEventTypeUpdated; use App\Models\Foundation\Summit\Factories\SummitEventTypeFactory; use App\Models\Foundation\Summit\Repositories\IDefaultSummitEventTypeRepository; use App\Models\Foundation\Summit\Repositories\ISummitEventTypeRepository; use App\Services\Model\ISummitEventTypeService; +use Illuminate\Support\Facades\Event; use libs\utils\ITransactionService; use models\exceptions\EntityNotFoundException; use models\exceptions\ValidationException; @@ -69,7 +73,7 @@ final class SummitEventTypeService implements ISummitEventTypeService */ public function addEventType(Summit $summit, array $data) { - return $this->tx_service->transaction(function() use($summit, $data){ + $event_type = $this->tx_service->transaction(function() use($summit, $data){ $type = trim($data['name']); @@ -85,6 +89,18 @@ final class SummitEventTypeService implements ISummitEventTypeService return $event_type; }); + + Event::fire + ( + new SummitEventTypeInserted + ( + $event_type->getId(), + $event_type->getClassName(), + $event_type->getSummitId() + ) + ); + + return $event_type; } /** @@ -115,6 +131,16 @@ final class SummitEventTypeService implements ISummitEventTypeService $event_type = SummitEventTypeFactory::populate($event_type, $summit, $data); + Event::fire + ( + new SummitEventTypeUpdated + ( + $event_type->getId(), + $event_type->getClassName(), + $event_type->getSummitId() + ) + ); + return $event_type; }); @@ -154,6 +180,16 @@ final class SummitEventTypeService implements ISummitEventTypeService ); } + Event::fire + ( + new SummitEventTypeDeleted + ( + $event_type->getId(), + $event_type->getClassName(), + $event_type->getSummitId() + ) + ); + $summit->removeEventType($event_type); }); @@ -167,7 +203,7 @@ final class SummitEventTypeService implements ISummitEventTypeService */ public function seedDefaultEventTypes(Summit $summit) { - return $this->tx_service->transaction(function() use($summit){ + $added_types = $this->tx_service->transaction(function() use($summit){ $added_types = []; $default_types = $this->default_event_types_repository->getAll(); foreach ($default_types as $default_type){ @@ -180,5 +216,19 @@ final class SummitEventTypeService implements ISummitEventTypeService return $added_types; }); + + foreach ($added_types as $event_type){ + Event::fire + ( + new SummitEventTypeInserted + ( + $event_type->getId(), + $event_type->getClassName(), + $event_type->getSummitId() + ) + ); + } + + return $added_types; } } \ 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 a5de8552..8dbd31f8 100644 --- a/resources/lang/en/not_found_errors.php +++ b/resources/lang/en/not_found_errors.php @@ -16,4 +16,8 @@ return [ 'promo_code_email_code_not_found' => 'promo code id :promo_code_id does not belongs to summit id :summit_id.', 'add_speaker_assistance_speaker_not_found' => 'speaker id :speaker_id not found', 'send_speaker_summit_assistance_announcement_mail_not_found_assistance' => 'summit speaker assistance :assistance_id not found for summit id :summit_id', + // LocationService + 'LocationService.addVenueFloor.VenueNotFound' => 'venue :venue_id not found on summit :summit_id', + 'LocationService.updateVenueFloor.FloorNotFound' => 'floor :floor_id does not belongs to venue :venue_id', + 'LocationService.updateVenueFloor.VenueNotFound' => 'venue :venue_id not found on summit :summit_id', ]; \ No newline at end of file diff --git a/resources/lang/en/validation_errors.php b/resources/lang/en/validation_errors.php index fb922ae0..2020bfed 100644 --- a/resources/lang/en/validation_errors.php +++ b/resources/lang/en/validation_errors.php @@ -26,6 +26,7 @@ return [ 'send_speaker_summit_assistance_announcement_mail_code_already_redeemed' => 'promo code :promo_code already redeemed.', 'send_speaker_summit_assistance_announcement_mail_invalid_mail_type' => 'mail type :mail_type is not valid.', 'send_speaker_summit_assistance_promo_code_not_set' => 'speaker :speaker_id has not set a promo code for summit :summit_id, please set one manually.', + // LocationService 'LocationService.addLocation.LocationNameAlreadyExists' => 'there is already another location with same name for summit :summit_id', 'LocationService.addLocation.InvalidClassName' => 'invalid class name', 'LocationService.addLocation.InvalidAddressOrCoordinates' => 'was passed a non-existent address', @@ -34,7 +35,8 @@ return [ 'LocationService.updateLocation.LocationNameAlreadyExists' => 'there is already another location with same name for summit :summit_id', 'LocationService.updateLocation.LocationNotFoundOnSummit' => 'location :location_id not found on summit :summit_id', 'LocationService.updateLocation.ClassNameMissMatch' => 'location :location_id on summit :summit_id does not belongs to class name :class_name', - 'LocationService.addVenueFloor.VenueNotFound' => 'venue :venue_id not found on summit :summit_id', 'LocationService.addVenueFloor.FloorNameAlreadyExists' => 'floor name :floor_name already belongs to another floor on venue :venue_id', 'LocationService.addVenueFloor.FloorNumberAlreadyExists' => 'floor number :floor_number already belongs to another floor on venue :venue_id', + 'LocationService.updateVenueFloor.FloorNameAlreadyExists' => 'floor name :floor_name already belongs to another floor on venue :venue_id', + 'LocationService.updateVenueFloor.FloorNumberAlreadyExists' => 'floor number :floor_number already belongs to another floor on venue :venue_id', ]; \ No newline at end of file diff --git a/tests/OAuth2SummitLocationsApiTest.php b/tests/OAuth2SummitLocationsApiTest.php index c89b3107..e25b242a 100644 --- a/tests/OAuth2SummitLocationsApiTest.php +++ b/tests/OAuth2SummitLocationsApiTest.php @@ -691,17 +691,23 @@ final class OAuth2SummitLocationsApiTest extends ProtectedApiTest $this->assertResponseStatus(204); } - public function testAddVenueFloor($summit_id = 23, $venue_id = 292){ + /** + * @param int $summit_id + * @param int $venue_id + * @param int $number + * @return mixed + */ + public function testAddVenueFloor($summit_id = 23, $venue_id = 292, $number = 0){ $params = [ 'id' => $summit_id, 'venue_id' => $venue_id ]; - + $name = str_random(16).'_floor'; $data = [ - 'name' => 'test floor', + 'name' => $name, 'description' => 'test floor', - 'number' => -1 + 'number' => $number ]; $headers = [ @@ -726,4 +732,41 @@ final class OAuth2SummitLocationsApiTest extends ProtectedApiTest $this->assertTrue(!is_null($floor)); return $floor; } + + public function testUpdateVenueFloor($summit_id = 23, $venue_id = 292){ + + $floor = $this->testAddVenueFloor($summit_id, $venue_id, 50); + $params = [ + 'id' => $summit_id, + 'venue_id' => $venue_id, + 'floor_id' => $floor->id + ]; + + $data = [ + 'name' => 'test floor update', + 'description' => 'test floor update', + ]; + + $headers = [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action( + "PUT", + "OAuth2SummitLocationsApiController@updateVenueFloor", + $params, + [], + [], + [], + $headers, + json_encode($data) + ); + + $content = $response->getContent(); + $this->assertResponseStatus(201); + $floor = json_decode($content); + $this->assertTrue(!is_null($floor)); + return $floor; + } } \ No newline at end of file