From 92f87924df86d942e4bafecde68b68165a09ddff Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Mon, 12 Mar 2018 15:13:29 -0300 Subject: [PATCH] added endpoint to update location maps and get by id PUT /api/v1/summits/{id}/locations/{location_id}/maps/{map_id} Content Type multipart/form-data' * file (sometimes) * name (sometimes|string|max:255) * description (sometimes|string) GET /api/v1/summits/{id}/locations/{location_id}/maps/{map_id} Change-Id: I00d28aae722ba7416317c561b2e42140908d73b5 --- .../OAuth2SummitLocationsApiController.php | 58 +++++++++- ...Auth2BearerAccessTokenRequestValidator.php | 5 +- app/Http/routes.php | 7 +- .../Locations/SummitGeoLocatedLocation.php | 17 ++- app/Services/Model/ILocationService.php | 12 ++ app/Services/Model/LocationService.php | 109 ++++++++++++++++++ database/seeds/ApiEndpointsSeeder.php | 9 ++ resources/lang/en/not_found_errors.php | 1 + 8 files changed, 209 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php index 1eafaf5a..474f022c 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php @@ -768,6 +768,42 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController } } + /** + * @param $summit_id + * @param $location_id + * @param $map_id + * @return mixed + */ + public function getLocationMap($summit_id, $location_id, $map_id){ + try { + + $expand = Request::input('expand', ''); + $relations = Request::input('relations', ''); + $relations = !empty($relations) ? explode(',', $relations) : []; + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + $location = $summit->getLocation($location_id); + if (is_null($location)) { + return $this->error404(); + } + + if (!Summit::isPrimaryLocation($location)) { + return $this->error404(); + } + + $map = $location->getMap($map_id); + if (is_null($map)) { + return $this->error404(); + } + + return $this->ok(SerializerRegistry::getInstance()->getSerializer($map)->serialize($expand,[], $relations)); + + } catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } /** * @param LaravelRequest $request * @param $summit_id @@ -799,7 +835,7 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController ); } - $this->location_service->addLocationMap + $map = $this->location_service->addLocationMap ( $summit, $location_id, @@ -809,6 +845,8 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController ), $file ); + + return $this->created(SerializerRegistry::getInstance()->getSerializer($map)->serialize()); } catch (EntityNotFoundException $ex1) { Log::warning($ex1); @@ -856,6 +894,10 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController $input = $multiPartRequestParser->getInput(); $metadata = $input['parameters']; $files = $input['files']; + $file = null; + + if(count($files) > 0) + $file = $files[0]; $rules = SummitLocationImageValidationRulesFactory::build(true); // Creates a Validator instance and validates the data. @@ -869,6 +911,20 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController $messages ); } + + $map = $this->location_service->updateLocationMap + ( + $summit, + $location_id, + $map_id, + HTMLCleaner::cleanData + ( + $metadata, ['description'] + ), + $file + ); + + return $this->updated(SerializerRegistry::getInstance()->getSerializer($map)->serialize()); } catch (EntityNotFoundException $ex1) { Log::warning($ex1); diff --git a/app/Http/Middleware/OAuth2BearerAccessTokenRequestValidator.php b/app/Http/Middleware/OAuth2BearerAccessTokenRequestValidator.php index 73f0936a..97a52840 100644 --- a/app/Http/Middleware/OAuth2BearerAccessTokenRequestValidator.php +++ b/app/Http/Middleware/OAuth2BearerAccessTokenRequestValidator.php @@ -84,11 +84,8 @@ class OAuth2BearerAccessTokenRequestValidator $method = $request->getMethod(); $realm = $request->getHost(); - $response = $next($request); - - return $response; - try { + $route = RequestUtils::getCurrentRoutePath($request); if (!$route) { throw new OAuth2ResourceServerException( diff --git a/app/Http/routes.php b/app/Http/routes.php index cab3303a..9f79d148 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -347,10 +347,11 @@ Route::group([ // locations maps Route::group(['prefix' => 'maps'], function () { - Route::post('', 'OAuth2SummitLocationsApiController@addLocationMap'); + Route::post('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitLocationsApiController@addLocationMap']); Route::group(['prefix' => '{map_id}'], function () { - Route::put('', 'OAuth2SummitLocationsApiController@updateLocationMap'); - Route::delete('', 'OAuth2SummitLocationsApiController@deleteLocationMap'); + Route::get('', 'OAuth2SummitLocationsApiController@getLocationMap'); + Route::put('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitLocationsApiController@updateLocationMap']); + Route::delete('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitLocationsApiController@deleteLocationMap']); }); }); diff --git a/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php b/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php index 07a47290..f468cfe0 100644 --- a/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php +++ b/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php @@ -359,7 +359,7 @@ class SummitGeoLocatedLocation extends SummitAbstractLocation ( Criteria::create() ->where(Criteria::expr()->eq("class_name", SummitLocationImage::TypeMap)) - ->orderBy(array("order" => Criteria::ASC)) + ->orderBy(["order" => Criteria::ASC]) ); } @@ -392,6 +392,21 @@ class SummitGeoLocatedLocation extends SummitAbstractLocation return $res === false ? null : $res; } + /** + * @param int $map_id + * @return SummitLocationImage + */ + public function getMap($map_id){ + $res = $this->images + ->matching + ( + Criteria::create()->where(Criteria::expr()->eq("id", $map_id))->andWhere(Criteria::expr()->eq("class_name", SummitLocationImage::TypeMap)) + + )->first(); + return $res === false ? null : $res; + } + + /** * @param SummitLocationImage $map */ diff --git a/app/Services/Model/ILocationService.php b/app/Services/Model/ILocationService.php index 2866f96b..0022d6d0 100644 --- a/app/Services/Model/ILocationService.php +++ b/app/Services/Model/ILocationService.php @@ -158,4 +158,16 @@ interface ILocationService */ public function addLocationMap(Summit $summit, $location_id, array $metadata, UploadedFile $file); + /** + * @param Summit $summit + * @param int $location_id + * @param int $map_id + * @param array $metadata + * @param $file + * @return SummitLocationImage + * @throws EntityNotFoundException + * @throws ValidationException + */ + public function updateLocationMap(Summit $summit, $location_id, $map_id, array $metadata, UploadedFile $file); + } \ No newline at end of file diff --git a/app/Services/Model/LocationService.php b/app/Services/Model/LocationService.php index 42bd5ef4..9c8e6c01 100644 --- a/app/Services/Model/LocationService.php +++ b/app/Services/Model/LocationService.php @@ -17,6 +17,7 @@ use App\Events\FloorInserted; use App\Events\FloorUpdated; use App\Events\LocationDeleted; use App\Events\LocationImageInserted; +use App\Events\LocationImageUpdated; use App\Events\LocationInserted; use App\Events\LocationUpdated; use App\Events\SummitVenueRoomDeleted; @@ -1195,4 +1196,112 @@ final class LocationService implements ILocationService return $map; } + + /** + * @param Summit $summit + * @param int $location_id + * @param int $map_id + * @param array $metadata + * @param $file + * @return SummitLocationImage + * @throws EntityNotFoundException + * @throws ValidationException + */ + public function updateLocationMap(Summit $summit, $location_id, $map_id, array $metadata, UploadedFile $file) + { + return $this->tx_service->transaction(function () use ($summit, $location_id, $map_id, $metadata, $file) { + $max_file_size = config('file_upload.max_file_upload_size') ; + $allowed_extensions = ['png','jpg','jpeg','gif','pdf']; + $location = $summit->getLocation($location_id); + + if (is_null($location)) { + throw new EntityNotFoundException + ( + trans( + 'not_found_errors.LocationService.addLocationMap.LocationNotFound', + [ + 'location_id' => $location_id, + ] + ) + ); + } + + if(!$location instanceof SummitGeoLocatedLocation){ + throw new EntityNotFoundException + ( + trans( + 'not_found_errors.LocationService.addLocationMap.LocationNotFound', + [ + 'location_id' => $location_id, + ] + ) + ); + } + + $map = $location->getMap($map_id); + + if (is_null($map)) { + throw new EntityNotFoundException + ( + trans( + 'not_found_errors.LocationService.addLocationMap.MapNotFound', + [ + 'map_id' => $map_id, + 'location_id' => $location_id, + ] + ) + ); + } + + if(!is_null($file)) { + if (!in_array($file->extension(), $allowed_extensions)) { + throw new ValidationException + ( + trans( + 'validation_errors.LocationService.addLocationMap.FileNotAllowedExtension', + [ + 'allowed_extensions' => implode(", ", $allowed_extensions), + ] + ) + ); + } + + if ($file->getSize() > $max_file_size) { + throw new ValidationException + ( + trans + ( + 'validation_errors.LocationService.addLocationMap.FileMaxSize', + [ + 'max_file_size' => (($max_file_size / 1024) / 1024) + ] + ) + ); + } + + $uploader = new FileUploader($this->folder_repository); + $pic = $uploader->build($file, sprintf('summits/%s/locations/%s/maps/', $location->getSummitId(), $location->getId()), true); + $map->setPicture($pic); + } + + $map = SummitLocationImageFactory::populate($map, $metadata); + + if (isset($metadata['order']) && intval($metadata['order']) != $map->getOrder()) { + // request to update order + $location->recalculateMapOrder($map, intval($metadata['order'])); + } + + Event::fire + ( + new LocationImageUpdated + ( + $map->getId(), + $map->getLocationId(), + $map->getLocation()->getSumitId(), + $map->getClassName() + ) + ); + return $map; + }); + } } \ No newline at end of file diff --git a/database/seeds/ApiEndpointsSeeder.php b/database/seeds/ApiEndpointsSeeder.php index ac733a5c..33827eff 100644 --- a/database/seeds/ApiEndpointsSeeder.php +++ b/database/seeds/ApiEndpointsSeeder.php @@ -528,6 +528,15 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::WriteLocationsData, $current_realm) ], ], + [ + 'name' => 'get-location-map', + 'route' => '/api/v1/summits/{id}/locations/{location_id}/maps/{map_id}', + 'http_method' => 'GET', + 'scopes' => [ + sprintf(SummitScopes::ReadSummitData, $current_realm), + sprintf(SummitScopes::ReadAllSummitData, $current_realm) + ], + ], [ 'name' => 'delete-location-map', 'route' => '/api/v1/summits/{id}/locations/{location_id}/maps/{map_id}', diff --git a/resources/lang/en/not_found_errors.php b/resources/lang/en/not_found_errors.php index ef99bef9..05254df7 100644 --- a/resources/lang/en/not_found_errors.php +++ b/resources/lang/en/not_found_errors.php @@ -32,4 +32,5 @@ return [ 'LocationService.updateLocationBanner.LocationNotFound' => 'location :location_id not found on summit :summit_id', 'LocationService.updateLocationBanner.BannerNotFound'=> 'banner :banner_id not found on location :location_id', 'LocationService.addLocationMap.LocationNotFound' => 'location :location_id not found on summit :summit_id', + 'LocationService.addLocationMap.MapNotFound' => 'map :map_id does not belongs to location :location_id', ]; \ No newline at end of file