added endpoint to delete location map

DELETE /api/v1/summits/{id}/locations/{location_id}/maps/{map_id}

Change-Id: I5b16e4f89888437df723597da5d423c5549cdab8
This commit is contained in:
Sebastian Marcet 2018-03-12 18:57:37 -03:00
parent 92f87924df
commit 12fc9cf490
8 changed files with 141 additions and 6 deletions

View File

@ -1,5 +1,6 @@
<?php namespace App\Factories\EntityEvents;
use App\Events\LocationImageAction;
use Illuminate\Support\Facades\App;
use models\main\IMemberRepository;
use models\oauth2\IResourceServerContext;
use models\summit\ISummitRepository;

View File

@ -949,12 +949,14 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController
/**
* @param $summit_id
* @param $location_id
* @param $map_id
* @return mixed
*/
public function deleteLocationMap($summit_id, $location_id){
public function deleteLocationMap($summit_id, $location_id, $map_id){
try {
$summit = SummitFinderStrategyFactory::build($this->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) {

View File

@ -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
*/

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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);
});
}
}

View File

@ -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',
];

View File

@ -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);
}
}