Skip to content

Commit

Permalink
Merge b05fc4e into 3726051
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhenry committed Dec 6, 2015
2 parents 3726051 + b05fc4e commit 254dde1
Show file tree
Hide file tree
Showing 100 changed files with 1,797 additions and 397 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ before_script:
- sleep 5 # give server some time to boot

script:
- gulp test
- gulp test:app
- php ./api/vendor/bin/phpunit --colors --configuration ./api/phpunit.xml --coverage-clover=./reports/coverage/api/clover.xml
- ./node_modules/.bin/cucumber.js --tags ~@ignore #skip @ignored features

after_script:
Expand Down
20 changes: 20 additions & 0 deletions api/app/Http/Controllers/AbstractRelatedEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ public function __construct(BaseModel $parentModel, TransformerInterface $transf
parent::__construct($transformer);
}

/**
* Function called before child models are synced with parent.
*
* @param $model
* @param $parent
*/
protected function preSync(BaseModel $parent, Collection $children)
{
}

/**
* Function called before response is sent after the model has been updated via sync.
*
* @param $model
* @param $parent
*/
protected function postSync(BaseModel $parent, Collection $children)
{
}

/**
* Override this method to provide custom pivot validation rules.
*
Expand Down
50 changes: 50 additions & 0 deletions api/app/Http/Controllers/AbstractSectionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Spira framework.
*
* @link https://github.com/spira/spira
*
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
*/

namespace App\Http\Controllers;

use App\Models\Section;
use Spira\Model\Model\BaseModel;

class AbstractSectionController extends ChildEntityController
{
protected $relationName = 'sections';

/**
* @param $requestEntity
* @param array $validationRules
* @param BaseModel $existingModel
* @param bool $limitToKeysPresent
* @return bool
*/
public function validateRequest($requestEntity, $validationRules, BaseModel $existingModel = null, $limitToKeysPresent = false)
{
$contentRules = $this->getContentRules($requestEntity);

foreach ($contentRules as $attribute => $rule) {
$validationRules['content.'.$attribute] = $rule;
}

return parent::validateRequest($requestEntity, $validationRules, $existingModel, $limitToKeysPresent);
}

/**
* @param $requestEntity
* @return array
*/
protected function getContentRules($requestEntity)
{
if (! isset(Section::$contentTypeMap[$requestEntity['type']])) {
return [];
}

return with(new Section::$contentTypeMap[$requestEntity['type']])->getValidationRules();
}
}
37 changes: 1 addition & 36 deletions api/app/Http/Controllers/ArticleCommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,10 @@
namespace App\Http\Controllers;

use App\Models\Article;
use Illuminate\Http\Request;
use App\Models\ArticleDiscussion;
use Spira\Responder\Response\ApiResponse;
use App\Http\Transformers\EloquentModelTransformer;

class ArticleCommentController extends ChildEntityController
class ArticleCommentController extends PostCommentController
{
/**
* Name in the parent model of this entity.
*
* @var string
*/
protected $relationName = 'comments';

/**
* Set dependencies.
*
Expand All @@ -35,29 +25,4 @@ public function __construct(Article $parentModel, EloquentModelTransformer $tran
{
parent::__construct($parentModel, $transformer);
}

/**
* Post a new comment.
*
* @param Request $request
* @param string $id
*
* @return ApiResponse
*/
public function postOne(Request $request, $id)
{
$this->validateRequest($request->json()->all(), $this->getValidationRules($id));

$parent = $this->findParentEntity($id);
$childModel = $this->getRelation($parent);
/** @var ArticleDiscussion $childModel */
$childModel = $childModel->save($request->json()->all(), $request->user());

// If we respond with createdItem() it would be an empty response, so
// we respond with item() instead to provide the data from the new
// comment
return $this->getResponse()
->transformer($this->getTransformer())
->item($childModel);
}
}
44 changes: 1 addition & 43 deletions api/app/Http/Controllers/ArticleSectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@
namespace App\Http\Controllers;

use App\Models\Article;
use Spira\Model\Model\BaseModel;
use App\Models\Sections\MediaContent;
use App\Models\Sections\PromoContent;
use App\Models\Sections\RichTextContent;
use App\Models\Sections\BlockquoteContent;
use App\Extensions\Controller\LocalizableTrait;
use App\Http\Transformers\EloquentModelTransformer;

class ArticleSectionController extends ChildEntityController
class ArticleSectionController extends AbstractSectionController
{
use LocalizableTrait;

Expand All @@ -29,41 +24,4 @@ public function __construct(Article $parentModel, EloquentModelTransformer $tran
{
parent::__construct($parentModel, $transformer);
}

/**
* @param $requestEntity
* @param array $validationRules
* @param BaseModel $existingModel
* @param bool $limitToKeysPresent
* @return bool
*/
public function validateRequest($requestEntity, $validationRules, BaseModel $existingModel = null, $limitToKeysPresent = false)
{
$contentRules = [];
switch ($requestEntity['type']) {
case RichTextContent::CONTENT_TYPE:

$contentRules = with(new RichTextContent)->getValidationRules();

break;
case BlockquoteContent::CONTENT_TYPE:

$contentRules = with(new BlockquoteContent)->getValidationRules();
break;
case MediaContent::CONTENT_TYPE:

$contentRules = with(new MediaContent)->getValidationRules();
break;
case PromoContent::CONTENT_TYPE:

$contentRules = with(new PromoContent)->getValidationRules();
break;
}

foreach ($contentRules as $attribute => $rule) {
$validationRules['content.'.$attribute] = $rule;
}

return parent::validateRequest($requestEntity, $validationRules, $existingModel, $limitToKeysPresent);
}
}
77 changes: 74 additions & 3 deletions api/app/Http/Controllers/ChildEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace App\Http\Controllers;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Http\Request;
use Spira\Model\Model\BaseModel;
use Spira\Responder\Response\ApiResponse;
Expand Down Expand Up @@ -163,8 +164,8 @@ public function putOne(Request $request, $id, $childId = false)
* Put many entities.
* Internally make use of Relation::sync().
*
* @param string $id
* @param Request $request
* @param string $id
* @return ApiResponse
*/
public function putMany(Request $request, $id)
Expand All @@ -180,8 +181,16 @@ public function putMany(Request $request, $id)

$this->checkPermission(static::class.'@putMany', ['model' => $parent, 'children' => $childModels]);

$this->saveNewItemsInCollection($childModels);
$this->getRelation($parent)->sync($this->makeSyncList($childModels, $requestCollection));
$relation = $this->getRelation($parent);

if ($relation instanceof BelongsToMany) {
$this->saveNewItemsInCollection($childModels);
$relation->sync($this->makeSyncList($childModels, $requestCollection));
} else {
$relation->saveMany($childModels);
}

$this->postSync($parent, $childModels);

return $this->getResponse()
->transformer($this->getTransformer())
Expand Down Expand Up @@ -295,4 +304,66 @@ public function deleteMany(Request $request, $id)

return $this->getResponse()->noContent();
}

/**
* @param null $entityId
* @return array
*/
protected function getValidationRules($entityId = null)
{
$childRules = $this->getChildModel()->getValidationRules($entityId);
$pivotRules = $this->getPivotValidationRules();

return array_merge($childRules, $pivotRules);
}

/**
* Override this method to provide custom validation rules.
*
* @return array
*/
protected function prepareSyncList(Collection $childModels, array $requestCollection)
{
$childPk = $this->getChildModel()->getPrimaryKey();

$childModels->keyBy($childPk);

$requestCollection = new Collection($requestCollection);
$requestCollection->keyBy($childPk);

$syncList = $childModels->reduce(function ($syncList, $childModel) use ($requestCollection, $childPk) {

$key = $childModel->{$childPk};
$requestItem = $requestCollection[$key];
if (isset($requestItem['_pivot'])) {
$syncList[$key] = $requestItem['_pivot'];

return $syncList;
}

$syncList[] = $key;

return $syncList;
}, []);

return $syncList;
}

/**
* @param $childId
* @param BaseModel $parentModel
* @return bool
*/
protected function childIdCanFallbackToParent($childId, BaseModel $parentModel)
{
$fk = $this->getRelation($parentModel)->getForeignKey();
$parentKey = $parentModel->getKeyName();

return $childId === false && ends_with($fk, $parentKey);
}

protected function getPivotValidationRules()
{
return [];
}
}
Loading

0 comments on commit 254dde1

Please sign in to comment.