Skip to content

Commit

Permalink
Merge pull request #62 from spira/hotfix/remove-is-indexed
Browse files Browse the repository at this point in the history
Reverting PR#55 (#55)
  • Loading branch information
Jeremy Sik committed Jun 6, 2016
2 parents b69609e + 05cf60f commit fcfb994
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 462 deletions.
5 changes: 0 additions & 5 deletions Controllers/AbstractRelatedEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ abstract class AbstractRelatedEntityController extends ApiController
*/
protected $parentModel;

/**
* @var bool
*/
protected $isIndexed = true;

public function __construct(BaseModel $parentModel, TransformerInterface $transformer)
{
$this->parentModel = $parentModel;
Expand Down
78 changes: 28 additions & 50 deletions Controllers/ChildEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,8 @@ public function postOne(Request $request, ElasticSearchIndexer $searchIndexer, $

$this->getRelation($parent)->save($childModel);

if ($this->isIndexed) {
// Children is auto updated, so need only to update parent
$searchIndexer->reindexOne($parent, []);
}
// Children is auto updated, so need only to update parent
$searchIndexer->reindexOne($parent, []);

return $this->getResponse()
->transformer($this->getTransformer())
Expand Down Expand Up @@ -121,10 +119,8 @@ public function postMany(Request $request, ElasticSearchIndexer $searchIndexer,

$this->getRelation($parent)->saveMany($childModels);

if ($this->isIndexed) {
// Children is auto updated, so need only to update parent
$searchIndexer->reindexOne($parent, []);
}
// Children is auto updated, so need only to update parent
$searchIndexer->reindexOne($parent, []);

return $this->getResponse()
->transformer($this->getTransformer())
Expand Down Expand Up @@ -163,13 +159,11 @@ public function putOne(Request $request, ElasticSearchIndexer $searchIndexer, $i

$this->getRelation($parent)->save($childModel);

if ($this->isIndexed) {
// Parent should be reindexed itself
$searchIndexer->reindexOne($parent, []);
// Parent should be reindexed itself
$searchIndexer->reindexOne($parent, []);

// Need to reindex childModel and all relations
$searchIndexer->reindexOne($childModel);
}
// Need to reindex childModel and all relations
$searchIndexer->reindexOne($childModel);

return $this->getResponse()
->transformer($this->getTransformer())
Expand Down Expand Up @@ -198,6 +192,12 @@ public function putMany(Request $request, ElasticSearchIndexer $searchIndexer, $

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

// Collect all affected items
$reindexItems = $searchIndexer->mergeUniqueCollection(
$searchIndexer->getAllItemsFromRelations($parent, [$this->relationName]),
$childModels
);

$relation = $this->getRelation($parent);

if ($relation instanceof BelongsToMany) {
Expand All @@ -207,19 +207,11 @@ public function putMany(Request $request, ElasticSearchIndexer $searchIndexer, $
$relation->saveMany($childModels);
}

if ($this->isIndexed) {
// Collect all affected items
$reindexItems = $searchIndexer->mergeUniqueCollection(
$searchIndexer->getAllItemsFromRelations($parent, [$this->relationName]),
$childModels
);

// We need to reindex all affected items with relations
$searchIndexer->reindexMany($reindexItems);
// We need to reindex all affected items with relations
$searchIndexer->reindexMany($reindexItems);

// Reindex parent without relations
$searchIndexer->reindexOne($parent, []);
}
// Reindex parent without relations
$searchIndexer->reindexOne($parent, []);

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

Expand Down Expand Up @@ -259,13 +251,11 @@ public function patchOne(Request $request, ElasticSearchIndexer $searchIndexer,

$this->getRelation($parent)->save($childModel);

if ($this->isIndexed) {
// Parent should be reindexed itself
$searchIndexer->reindexOne($parent, []);
// Parent should be reindexed itself
$searchIndexer->reindexOne($parent, []);

// Need to reindex childModel and all relations
$searchIndexer->reindexOne($childModel);
}
// Need to reindex childModel and all relations
$searchIndexer->reindexOne($childModel);

return $this->getResponse()->noContent();
}
Expand All @@ -292,13 +282,11 @@ public function patchMany(Request $request, ElasticSearchIndexer $searchIndexer,

$this->getRelation($parent)->saveMany($childModels);

if ($this->isIndexed) {
// Parent should be reindexed itself
$searchIndexer->reindexOne($parent, []);
// Parent should be reindexed itself
$searchIndexer->reindexOne($parent, []);

// We need to reindex all items with relations
$searchIndexer->reindexMany($childModels);
}
// We need to reindex all items with relations
$searchIndexer->reindexMany($childModels);

return $this->getResponse()->noContent();
}
Expand All @@ -324,11 +312,7 @@ public function deleteOne($id, ElasticSearchIndexer $searchIndexer, $childId = f

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

if ($this->isIndexed) {
$searchIndexer->deleteOneAndReindexRelated($childModel);
} else {
$childModel->delete();
}
$searchIndexer->deleteOneAndReindexRelated($childModel);

$parent->fireRevisionableEvent('deleteChild', [$childModel, $this->relationName]);

Expand All @@ -351,13 +335,7 @@ public function deleteMany(Request $request, ElasticSearchIndexer $searchIndexer

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

if ($this->isIndexed) {
$searchIndexer->deleteManyAndReindexRelated($childModels);
} else {
$childModels->each(function ($childModel) {
$childModel->delete();
});
}
$searchIndexer->deleteManyAndReindexRelated($childModels);

return $this->getResponse()->noContent();
}
Expand Down
50 changes: 22 additions & 28 deletions Controllers/LinkedEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ public function attachOne(Request $request, ElasticSearchIndexer $searchIndexer,

$this->getRelation($parent)->attach($childModel, $this->getPivotValues($childModel));

if ($this->isIndexed) {
$searchIndexer->reindexOne($parent, []);
$searchIndexer->reindexOne($childModel, []);
}
$searchIndexer->reindexOne($parent, []);
$searchIndexer->reindexOne($childModel, []);

return $this->getResponse()->created();
}
Expand All @@ -74,10 +72,8 @@ public function detachOne(ElasticSearchIndexer $searchIndexer, $id, $childId)
$this->checkPermission(static::class.'@detachOne', ['model' => $parent, 'children' => $childModel]);
$this->getRelation($parent)->detach($childModel);

if ($this->isIndexed) {
$searchIndexer->reindexOne($parent, []);
$searchIndexer->reindexOne($childModel, []);
}
$searchIndexer->reindexOne($parent, []);
$searchIndexer->reindexOne($childModel, []);

return $this->getResponse()->noContent();
}
Expand All @@ -87,16 +83,16 @@ public function detachAll(ElasticSearchIndexer $searchIndexer, $id)
$parent = $this->findParentEntity($id);

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

$reindexItems = $searchIndexer->getAllItemsFromRelations($parent, [$this->relationName]);

$this->getRelation($parent)->detach();

if ($this->isIndexed) {
$reindexItems = $searchIndexer->getAllItemsFromRelations($parent, [$this->relationName]);
// Reindex parent entity
$searchIndexer->reindexOne($parent, []);
// Reindex parent entity
$searchIndexer->reindexOne($parent, []);

// Reindex all detached entities
$searchIndexer->reindexMany($reindexItems, []);
}
// Reindex all detached entities
$searchIndexer->reindexMany($reindexItems, []);

return $this->getResponse()->noContent();
}
Expand All @@ -116,23 +112,21 @@ protected function processMany(Request $request, $id, $method)

$this->saveNewItemsInCollection($childModels);

/** @var ElasticSearchIndexer $searchIndexer */
$searchIndexer = app(ElasticSearchIndexer::class);
$reindexItems = $searchIndexer->mergeUniqueCollection(
$searchIndexer->getAllItemsFromRelations($parent, [$this->relationName]),
$childModels
);

$this->getRelation($parent)->{$method}($this->makeSyncList($childModels, $requestCollection));
$this->postSync($parent, $childModels);

if ($this->isIndexed) {
/** @var ElasticSearchIndexer $searchIndexer */
$searchIndexer = app(ElasticSearchIndexer::class);
$reindexItems = $searchIndexer->mergeUniqueCollection(
$searchIndexer->getAllItemsFromRelations($parent, [$this->relationName]),
$childModels
);
// Reindex parent entity without relations
$searchIndexer->reindexOne($parent, []);

// Reindex parent entity without relations
$searchIndexer->reindexOne($parent, []);

// Reindex all affected items without relations
$searchIndexer->reindexMany($reindexItems, []);
}
// Reindex all affected items without relations
$searchIndexer->reindexMany($reindexItems, []);

$transformed = $this->getTransformer()->transformCollection($this->findAllChildren($parent), ['_self']);

Expand Down
1 change: 0 additions & 1 deletion Responder/TransformerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public function item($data, $transformer = null, $resourceKey = null)
protected function getTransformer($transformer = null)
{
return $transformer ?: function ($data) {

if ($data instanceof Arrayable) {
return $data->toArray();
}
Expand Down
1 change: 0 additions & 1 deletion database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Spira\Core\Model\Test\SecondTestEntity;

$factory->define(TestEntity::class, function (\Faker\Generator $faker) {

return [
'entity_id' => $faker->uuid,
'varchar' => $faker->word,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ public function up()
return true;
}
Schema::create(TestEntity::getTableName(), function (Blueprint $table) {
$table->uuid('entity_id');
$table->string('varchar', 255);
$table->char('hash', 60);
$table->integer('integer');
$table->decimal('decimal', 11, 2);
$table->boolean('boolean');
$table->boolean('nullable')->nullable();
$table->text('text');
$table->date('date');
$table->dateTime('time');
$table->boolean('multi_word_column_title');
$table->boolean('hidden');
$table->json('json');
$table->uuid('entity_id');
$table->string('varchar', 255);
$table->char('hash', 60);
$table->integer('integer');
$table->decimal('decimal', 11, 2);
$table->boolean('boolean');
$table->boolean('nullable')->nullable();
$table->text('text');
$table->date('date');
$table->dateTime('time');
$table->boolean('multi_word_column_title');
$table->boolean('hidden');
$table->json('json');

$table->dateTime('created_at');
$table->dateTime('updated_at')->nullable();
$table->dateTime('created_at');
$table->dateTime('updated_at')->nullable();

$table->primary('entity_id');
}
$table->primary('entity_id');
}
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class CreateLocalizationsTable extends Migration
public function up()
{
Schema::create(static::TABLE_NAME, function (Blueprint $table) {

$table->uuid('localizable_id');
$table->string('localizable_type');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function up()
return true;
}
Schema::create(OrderedTestEntity::getTableName(), function (Blueprint $table) {
$table->uuid('entity_id');
$table->string('varchar', 255);
$table->integer('integer');
$table->uuid('entity_id');
$table->string('varchar', 255);
$table->integer('integer');

$table->primary('entity_id');
}
$table->primary('entity_id');
}
);
}

Expand Down
Loading

0 comments on commit fcfb994

Please sign in to comment.