added new endpoint add location banner

POST /api/v1/summits/{id}/locations/{location_id}/banners
Payload

* title (required|string)
* content (required|string)
* type (required|in:Primary,Secondary)
* enabled (required|boolean)
* class_name (required|in:SummitLocationBanner, ScheduledSummitLocationBanner)

Payload part for ScheduledSummitLocationBanner

* start_date (required|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: Ic8de4a81c4d57253bbe25559a5ba7f50d1c7730d
This commit is contained in:
Sebastian Marcet
2018-03-07 19:01:04 -03:00
parent ec4a3474ad
commit dda0d68378
25 changed files with 1359 additions and 25 deletions

View File

@@ -1,5 +1,4 @@
<?php namespace models\summit;
/**
* Copyright 2015 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,10 +11,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\Models\Foundation\Summit\Locations\Banners\ScheduledSummitLocationBanner;
use App\Models\Foundation\Summit\Locations\Banners\SummitLocationBanner;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use models\exceptions\ValidationException;
use models\utils\SilverstripeBaseModel;
use Doctrine\ORM\Mapping AS ORM;
/**
* @see https://github.com/doctrine/doctrine2/commit/ff28507b88ffd98830c44762
* //@ORM\AssociationOverrides({
@@ -43,28 +45,39 @@ class SummitAbstractLocation extends SilverstripeBaseModel
/**
* @ORM\Column(name="Name", type="string")
* @var string
*/
protected $name;
/**
* @ORM\Column(name="Description", type="string")
* @var string
*/
protected $description;
/**
* @ORM\Column(name="LocationType", type="string")
* @var string
*/
protected $type;
/**
* @ORM\Column(name="`Order`", type="integer")
* @var int
*/
protected $order;
/**
* @ORM\OneToMany(targetEntity="App\Models\Foundation\Summit\Locations\Banners\SummitLocationBanner", mappedBy="location", cascade={"persist"}, orphanRemoval=true)
* @var SummitLocationBanner[]
*/
protected $banners;
public static $metadata = [
'name' => 'string',
'description' => 'string',
'type' => [ self::TypeExternal, self::TypeInternal],
'banners' => 'array',
'order' => 'integer',
];
@@ -78,7 +91,8 @@ class SummitAbstractLocation extends SilverstripeBaseModel
public function __construct()
{
parent::__construct();
$this->type = self::TypeNone;
$this->type = self::TypeNone;
$this->banners = new ArrayCollection;
}
/**
@@ -161,4 +175,87 @@ class SummitAbstractLocation extends SilverstripeBaseModel
{
return false;
}
/**
* @param SummitLocationBanner $banner
* @return $this
* @throws ValidationException
*/
public function addBanner(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()){
throw new ValidationException
(
sprintf
('already exist an enabled static banner for location %s', $this->id)
);
}
}
}
if($banner instanceof ScheduledSummitLocationBanner){
// dont 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()){
$old_start = $old_banner->getLocalStartDate();
$old_end = $old_banner->getLocalEndDate();
// (StartA <= EndB) and (EndA >= StartB)
if($new_start <= $old_end && $new_end >= $old_start){
// overlap!!!
throw new ValidationException
(
sprintf
(
'schedule time range (%s to %s) overlaps with an existent scheduled time range (%s to %s) - banner id %s',
$new_start->format('Y-m-d H:i:s'),
$new_end->format('Y-m-d H:i:s'),
$old_start->format('Y-m-d H:i:s'),
$old_end->format('Y-m-d H:i:s'),
$old_banner->id
)
);
}
}
}
}
$this->banners->add($banner);
$banner->setLocation($this);
return $this;
}
/**
* @param SummitLocationBanner $banner
* @return $this
*/
public function removeBanner(SummitLocationBanner $banner){
$this->banners->removeElement($banner);
$banner->clearLocation();
return $this;
}
/**
* @param int $banner_id
* @return SummitLocationBanner|null
*/
public function getBannerById($banner_id){
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('id', intval($banner_id)));
$banner = $this->banners->matching($criteria)->first();
return $banner === false ? null : $banner;
}
/**
* @param string $class_name
* @return SummitLocationBanner|null
*/
public function getBannerByClass($class_name){
}
}