From 12fc9cf4900e840eaa0ae95ff1742ac2a12157af Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Mon, 12 Mar 2018 18:57:37 -0300 Subject: [PATCH] added endpoint to delete location map DELETE /api/v1/summits/{id}/locations/{location_id}/maps/{map_id} Change-Id: I5b16e4f89888437df723597da5d423c5549cdab8 --- .../LocationImageActionEntityEventFactory.php | 1 + .../OAuth2SummitLocationsApiController.php | 4 +- .../Locations/SummitGeoLocatedLocation.php | 10 +++ .../Summit/Locations/SummitLocationImage.php | 13 ++- app/Services/Model/ILocationService.php | 10 +++ app/Services/Model/LocationService.php | 80 ++++++++++++++++++- resources/lang/en/not_found_errors.php | 2 + tests/OAuth2SummitLocationsApiTest.php | 27 +++++++ 8 files changed, 141 insertions(+), 6 deletions(-) diff --git a/app/Factories/EntityEvents/LocationImageActionEntityEventFactory.php b/app/Factories/EntityEvents/LocationImageActionEntityEventFactory.php index c3001c7c..687df930 100644 --- a/app/Factories/EntityEvents/LocationImageActionEntityEventFactory.php +++ b/app/Factories/EntityEvents/LocationImageActionEntityEventFactory.php @@ -1,5 +1,6 @@ repository, $this->resource_server_context)->find($summit_id); if (is_null($summit)) return $this->error404(); + $this->location_service->deleteLocationMap($summit, $location_id, $map_id); return $this->deleted(); } catch (EntityNotFoundException $ex1) { diff --git a/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php b/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php index f468cfe0..dfed7b2a 100644 --- a/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php +++ b/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php @@ -419,6 +419,16 @@ class SummitGeoLocatedLocation extends SummitAbstractLocation $map->setLocation($this); } + /** + * @param SummitLocationImage $map + * @return $this + */ + public function removeMap(SummitLocationImage $map){ + $this->images->removeElement($map); + $map->ClearLocation(); + return $this; + } + /** * @param SummitLocationImage $image */ diff --git a/app/Models/Foundation/Summit/Locations/SummitLocationImage.php b/app/Models/Foundation/Summit/Locations/SummitLocationImage.php index 98ba3567..4beab4e8 100644 --- a/app/Models/Foundation/Summit/Locations/SummitLocationImage.php +++ b/app/Models/Foundation/Summit/Locations/SummitLocationImage.php @@ -45,14 +45,14 @@ class SummitLocationImage extends SilverstripeBaseModel /** * @ORM\ManyToOne(targetEntity="models\main\File", fetch="EAGER") - * @ORM\JoinColumn(name="PictureID", referencedColumnName="ID") + * @ORM\JoinColumn(name="PictureID", referencedColumnName="ID", onDelete="CASCADE") * @var File */ protected $picture; /** * @ORM\ManyToOne(targetEntity="models\summit\SummitGeoLocatedLocation", inversedBy="images") - * @ORM\JoinColumn(name="LocationID", referencedColumnName="ID") + * @ORM\JoinColumn(name="LocationID", referencedColumnName="ID", onDelete="CASCADE") * @var SummitGeoLocatedLocation */ protected $location; @@ -149,7 +149,6 @@ class SummitLocationImage extends SilverstripeBaseModel $this->location = $location; } - /** * @return string */ @@ -184,4 +183,12 @@ class SummitLocationImage extends SilverstripeBaseModel return 0; } } + + public function clearLocation(){ + $this->location = null; + } + + public function clearPicture(){ + $this->picture = null; + } } \ No newline at end of file diff --git a/app/Services/Model/ILocationService.php b/app/Services/Model/ILocationService.php index 0022d6d0..c9e184de 100644 --- a/app/Services/Model/ILocationService.php +++ b/app/Services/Model/ILocationService.php @@ -170,4 +170,14 @@ interface ILocationService */ public function updateLocationMap(Summit $summit, $location_id, $map_id, array $metadata, UploadedFile $file); + /** + * @param Summit $summit + * @param int $location_id + * @param int $map_id + * @return SummitAbstractLocation + * @throws EntityNotFoundException + * @throws ValidationException + */ + public function deleteLocationMap(Summit $summit, $location_id, $map_id); + } \ No newline at end of file diff --git a/app/Services/Model/LocationService.php b/app/Services/Model/LocationService.php index 9c8e6c01..52349db2 100644 --- a/app/Services/Model/LocationService.php +++ b/app/Services/Model/LocationService.php @@ -16,6 +16,7 @@ use App\Events\FloorDeleted; use App\Events\FloorInserted; use App\Events\FloorUpdated; use App\Events\LocationDeleted; +use App\Events\LocationImageDeleted; use App\Events\LocationImageInserted; use App\Events\LocationImageUpdated; use App\Events\LocationInserted; @@ -1189,7 +1190,7 @@ final class LocationService implements ILocationService ( $map->getId(), $map->getLocationId(), - $map->getLocation()->getSumitId(), + $map->getLocation()->getSummitId(), $map->getClassName() ) ); @@ -1297,11 +1298,86 @@ final class LocationService implements ILocationService ( $map->getId(), $map->getLocationId(), - $map->getLocation()->getSumitId(), + $map->getLocation()->getSummitId(), $map->getClassName() ) ); + return $map; }); } + + /** + * @param Summit $summit + * @param int $location_id + * @param int $map_id + * @return SummitAbstractLocation + * @throws EntityNotFoundException + * @throws ValidationException + */ + public function deleteLocationMap(Summit $summit, $location_id, $map_id) + { + return $this->tx_service->transaction(function () use ($summit, $location_id, $map_id) { + + $location = $summit->getLocation($location_id); + + if(is_null($location)){ + throw new EntityNotFoundException + ( + trans + ( + 'not_found_errors.LocationService.deleteLocationMap.LocationNotFound', + [ + 'summit_id' => $summit->getId(), + 'location_id' => $location_id, + ] + ) + ); + } + + if(!$location instanceof SummitGeoLocatedLocation){ + throw new EntityNotFoundException + ( + trans + ( + 'not_found_errors.LocationService.deleteLocationMap.LocationNotFound', + [ + 'summit_id' => $summit->getId(), + 'location_id' => $location_id, + ] + ) + ); + } + + $map = $location->getMap($map_id); + + if(is_null($map)){ + throw new EntityNotFoundException + ( + trans + ( + 'not_found_errors.LocationService.deleteLocationMap.MapNotFound', + [ + 'location_id' => $location_id, + 'banner_id' => $map_id, + ] + ) + ); + } + + Event::fire + ( + new LocationImageDeleted + ( + $map->getId(), + $map->getLocationId(), + $map->getLocation()->getSummitId(), + $map->getClassName() + ) + ); + + $location->removeMap($map); + + }); + } } \ 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 05254df7..4def83c3 100644 --- a/resources/lang/en/not_found_errors.php +++ b/resources/lang/en/not_found_errors.php @@ -33,4 +33,6 @@ return [ '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', + 'LocationService.deleteLocationMap.LocationNotFound' => 'location :location_id not found on summit :summit_id', + 'LocationService.deleteLocationMap.MapNotFound' => 'map :map_id not found on location :location_id', ]; \ No newline at end of file diff --git a/tests/OAuth2SummitLocationsApiTest.php b/tests/OAuth2SummitLocationsApiTest.php index c2e700a0..16b443e8 100644 --- a/tests/OAuth2SummitLocationsApiTest.php +++ b/tests/OAuth2SummitLocationsApiTest.php @@ -1282,4 +1282,31 @@ final class OAuth2SummitLocationsApiTest extends ProtectedApiTest $content = $response->getContent(); $this->assertResponseStatus(204); } + + public function testDeleteLocationMap($summit_id = 22, $location_id = 214, $map_id=30){ + + $params = [ + 'id' => $summit_id, + 'location_id' => $location_id, + 'map_id' => $map_id + ]; + + $headers = [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action( + "DELETE", + "OAuth2SummitLocationsApiController@deleteLocationMap", + $params, + [], + [], + [], + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(204); + } } \ No newline at end of file