PUT /api/v1/summits/{id}/locations/{location_id}/banners/{banner_id}
Payload * title (sometimes|string) * content (sometimes|string) * type (sometimes|in:Primary,Secondary) * enabled (sometimes|boolean) * class_name (sometimes|in:SummitLocationBanner,ScheduledSummitLocationBanner) Payload part for ScheduledSummitLocationBanner * start_date (sometimes|date_format:U) * end_date (required_with:start_date|date_format:U|after:start_date) Required Scopes * '%s/summits/write' * '%s/locations/write' * '%s/locations/banners/write' Change-Id: I1b60fae02b3d74532ee0f77c98ac783f9a0e23cb
This commit is contained in:
parent
dc8a44a14d
commit
d1d8fb73f4
@ -1748,7 +1748,66 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController
|
||||
}
|
||||
}
|
||||
|
||||
public function updateLocationBanner($summit, $location_id, $banner_id){
|
||||
/**
|
||||
* @param $summit_id
|
||||
* @param $location_id
|
||||
* @param $banner_id
|
||||
* @return mixed
|
||||
*/
|
||||
public function updateLocationBanner($summit_id, $location_id, $banner_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();
|
||||
|
||||
$rules = SummitLocationBannerValidationRulesFactory::build($payload, true);
|
||||
// Creates a Validator instance and validates the data.
|
||||
$messages = [
|
||||
'class_name.in' => sprintf
|
||||
(
|
||||
":attribute has an invalid value ( valid values are %s )",
|
||||
implode(", ", SummitLocationBannerConstants::$valid_class_names)
|
||||
)
|
||||
];
|
||||
$validation = Validator::make($payload, $rules, $messages);
|
||||
|
||||
if ($validation->fails()) {
|
||||
$messages = $validation->messages()->toArray();
|
||||
|
||||
return $this->error412
|
||||
(
|
||||
$messages
|
||||
);
|
||||
}
|
||||
|
||||
$banner = $this->location_service->updateLocationBanner
|
||||
(
|
||||
$summit,
|
||||
$location_id,
|
||||
$banner_id,
|
||||
HTMLCleaner::cleanData
|
||||
(
|
||||
$payload, ['title', 'content']
|
||||
)
|
||||
);
|
||||
|
||||
return $this->updated(SerializerRegistry::getInstance()->getSerializer($banner)->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);
|
||||
}
|
||||
}
|
||||
}
|
@ -33,7 +33,7 @@ final class SummitLocationBannerValidationRulesFactory
|
||||
throw new ValidationException('class_name is not set');
|
||||
|
||||
$base_rules = [
|
||||
'class_name' => sprintf('required|in%s', implode(", ", SummitLocationBannerConstants::$valid_class_names)),
|
||||
'class_name' => sprintf('required|in:%s', implode(",", SummitLocationBannerConstants::$valid_class_names)),
|
||||
'title' => 'required|string',
|
||||
'content' => 'required|string',
|
||||
'type' => sprintf('required|in:%s', implode(",", SummitLocationBannerConstants::$valid_types)),
|
||||
@ -42,7 +42,7 @@ final class SummitLocationBannerValidationRulesFactory
|
||||
|
||||
if($update){
|
||||
$base_rules = [
|
||||
'class_name' => sprintf('required|in%s', implode(", ", SummitLocationBannerConstants::$valid_class_names)),
|
||||
'class_name' => sprintf('required|in:%s', implode(",", SummitLocationBannerConstants::$valid_class_names)),
|
||||
'title' => 'sometimes|string',
|
||||
'content' => 'sometimes|string',
|
||||
'type' => sprintf('sometimes|in:%s', implode(",", SummitLocationBannerConstants::$valid_types)),
|
||||
|
@ -36,7 +36,7 @@ final class SummitLocationValidationRulesFactory
|
||||
throw new ValidationException('class_name is required');
|
||||
|
||||
$base_rules = [
|
||||
'class_name' => sprintf('required|in:%s', implode(", ", SummitLocationConstants::$valid_class_names))
|
||||
'class_name' => sprintf('required|in:%s', implode(",", SummitLocationConstants::$valid_class_names))
|
||||
];
|
||||
|
||||
switch($data['class_name']){
|
||||
@ -64,6 +64,7 @@ final class SummitLocationValidationRulesFactory
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
@ -178,15 +178,14 @@ class SummitAbstractLocation extends SilverstripeBaseModel
|
||||
|
||||
/**
|
||||
* @param SummitLocationBanner $banner
|
||||
* @return $this
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function addBanner(SummitLocationBanner $banner){
|
||||
public function validateBanner(SummitLocationBanner $banner){
|
||||
|
||||
if($banner->getClassName() == SummitLocationBanner::ClassName){
|
||||
// only one static banner could exist at the same time
|
||||
foreach ($this->banners as $old_banner){
|
||||
if($old_banner->getClassName() == SummitLocationBanner::ClassName && $old_banner->isEnabled()){
|
||||
if($old_banner->getClassName() == SummitLocationBanner::ClassName && $old_banner->isEnabled() && $old_banner->getId() != $banner->getId()){
|
||||
throw new ValidationException
|
||||
(
|
||||
sprintf
|
||||
@ -197,12 +196,12 @@ class SummitAbstractLocation extends SilverstripeBaseModel
|
||||
}
|
||||
|
||||
if($banner instanceof ScheduledSummitLocationBanner){
|
||||
// dont overlap enabled ones
|
||||
// do not overlap enabled ones
|
||||
$new_start = $banner->getLocalStartDate();
|
||||
$new_end = $banner->getLocalEndDate();
|
||||
|
||||
foreach ($this->banners as $old_banner){
|
||||
if($old_banner instanceof ScheduledSummitLocationBanner && $old_banner->isEnabled()){
|
||||
if($old_banner instanceof ScheduledSummitLocationBanner && $old_banner->isEnabled() && $old_banner->getId() != $banner->getId()){
|
||||
$old_start = $old_banner->getLocalStartDate();
|
||||
$old_end = $old_banner->getLocalEndDate();
|
||||
// (StartA <= EndB) and (EndA >= StartB)
|
||||
@ -225,6 +224,15 @@ class SummitAbstractLocation extends SilverstripeBaseModel
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* @param SummitLocationBanner $banner
|
||||
* @return $this
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function addBanner(SummitLocationBanner $banner){
|
||||
|
||||
$this->validateBanner($banner);
|
||||
$this->banners->add($banner);
|
||||
$banner->setLocation($this);
|
||||
return $this;
|
||||
|
@ -1008,6 +1008,41 @@ final class LocationService implements ILocationService
|
||||
{
|
||||
return $this->tx_service->transaction(function () use ($summit, $location_id, $banner_id, $data) {
|
||||
|
||||
$location = $summit->getLocation($location_id);
|
||||
|
||||
if(is_null($location)){
|
||||
throw new EntityNotFoundException
|
||||
(
|
||||
trans
|
||||
(
|
||||
'not_found_errors.LocationService.updateLocationBanner.LocationNotFound',
|
||||
[
|
||||
'summit_id' => $summit->getId(),
|
||||
'location_id' => $location_id,
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$banner = $location->getBannerById($banner_id);
|
||||
|
||||
if(is_null($banner)){
|
||||
throw new EntityNotFoundException
|
||||
(
|
||||
trans
|
||||
(
|
||||
'not_found_errors.LocationService.updateLocationBanner.BannerNotFound',
|
||||
[
|
||||
'location_id' => $location_id,
|
||||
'banner_id' => $banner_id,
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$banner = SummitLocationBannerFactory::populate($summit, $location, $data, $banner);
|
||||
$location->validateBanner($banner);
|
||||
return $banner;
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -29,4 +29,6 @@ return [
|
||||
'LocationService.addLocationBanner.LocationNotFound' => 'location :location_id not found on summit :summit_id',
|
||||
'LocationService.deleteLocationBanner.LocationNotFound' => 'location :location_id not found on summit :summit_id',
|
||||
'LocationService.deleteLocationBanner.BannerNotFound'=> 'banner :banner_id not found on location :location_id',
|
||||
'LocationService.updateLocationBanner.LocationNotFound' => 'location :location_id not found on summit :summit_id',
|
||||
'LocationService.updateLocationBanner.BannerNotFound'=> 'banner :banner_id not found on location :location_id',
|
||||
];
|
Loading…
x
Reference in New Issue
Block a user