-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature/more-accurate-related-services (#271)
* Added depth column to database ERD * Added migration for creating depth column * Taxonomy depths calculated in migration * Added controller logic for taxnomy depth * Added taxonomy depth to existing unit tests * Child taxonomy depths updated when parent is changed * Added test case for sorting related service by lower level taxonomies Test currently failing as logic still needs to be implemented. * Added ordering in RelatedController * Linted code * Fixed parent call
- Loading branch information
1 parent
c01f3c1
commit 57a8320
Showing
11 changed files
with
422 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
73 changes: 73 additions & 0 deletions
73
database/migrations/2020_05_15_134251_add_depth_column_to_taxonomies_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
use App\Models\Taxonomy; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class AddDepthColumnToTaxonomiesTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('taxonomies', function (Blueprint $table) { | ||
$table->unsignedInteger('depth') | ||
->default(0) | ||
->after('order') | ||
->index(); | ||
}); | ||
|
||
// Remove the default value. | ||
Schema::table('taxonomies', function (Blueprint $table) { | ||
$table->unsignedInteger('depth') | ||
->default(null) | ||
->change(); | ||
}); | ||
|
||
// Calculate the depth for all. | ||
DB::table('taxonomies')->orderBy('id')->chunk( | ||
200, | ||
function (Collection $taxonomies) { | ||
$taxonomies->each(function (stdClass $taxonomy) { | ||
DB::table('taxonomies') | ||
->where('id', '=', $taxonomy->id) | ||
->update([ | ||
'depth' => $this->getDepth($taxonomy), | ||
]); | ||
}); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* @param \stdClass $taxonomy | ||
* @return int | ||
*/ | ||
protected function getDepth(stdClass $taxonomy): int | ||
{ | ||
if ($taxonomy->parent_id === null) { | ||
return 0; | ||
} | ||
|
||
$parentTaxonomy = DB::table('taxonomies') | ||
->where('id', '=', $taxonomy->parent_id) | ||
->first(); | ||
|
||
return 1 + $this->getDepth($parentTaxonomy); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('taxonomies', function (Blueprint $table) { | ||
$table->dropIndex(['depth']); | ||
$table->dropColumn('depth'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.