From edd111451ee07b257656ab795cf782f95acef733 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Tue, 3 Nov 2015 21:32:45 +0000 Subject: [PATCH] Started work on enabling/disabling components --- .../Component/AddComponentCommand.php | 12 +++++- .../Component/UpdateComponentCommand.php | 12 +++++- app/Composers/StatusPageComposer.php | 8 ++-- .../Component/AddComponentCommandHandler.php | 8 +++- .../UpdateComponentCommandHandler.php | 8 +++- .../Controllers/Api/ComponentController.php | 6 ++- app/Models/Component.php | 3 ++ ...9_AlterTableComponentsAddEnabledColumn.php | 41 +++++++++++++++++++ resources/views/partials/components.blade.php | 4 +- tests/Api/ComponentTest.php | 34 +++++++++++++++ 10 files changed, 122 insertions(+), 14 deletions(-) create mode 100644 database/migrations/2015_11_03_211049_AlterTableComponentsAddEnabledColumn.php diff --git a/app/Commands/Component/AddComponentCommand.php b/app/Commands/Component/AddComponentCommand.php index 5b26698f..7a5bffd7 100644 --- a/app/Commands/Component/AddComponentCommand.php +++ b/app/Commands/Component/AddComponentCommand.php @@ -55,6 +55,13 @@ final class AddComponentCommand */ public $group_id; + /** + * Is the component enabled? + * + * @var bool + */ + public $enabled; + /** * The validation rules. * @@ -67,6 +74,7 @@ final class AddComponentCommand 'link' => 'url', 'order' => 'int', 'group_id' => 'int', + 'enabled' => 'bool', ]; /** @@ -78,10 +86,11 @@ final class AddComponentCommand * @param string $link * @param int $order * @param int $group_id + * @param bool $enabled * * @return void */ - public function __construct($name, $description, $status, $link, $order, $group_id) + public function __construct($name, $description, $status, $link, $order, $group_id, $enabled) { $this->name = $name; $this->description = $description; @@ -89,5 +98,6 @@ final class AddComponentCommand $this->link = $link; $this->order = $order; $this->group_id = $group_id; + $this->enabled = $enabled; } } diff --git a/app/Commands/Component/UpdateComponentCommand.php b/app/Commands/Component/UpdateComponentCommand.php index 8e50ca0a..6ab4624d 100644 --- a/app/Commands/Component/UpdateComponentCommand.php +++ b/app/Commands/Component/UpdateComponentCommand.php @@ -64,6 +64,13 @@ final class UpdateComponentCommand */ public $group_id; + /** + * Is the component enabled? + * + * @var bool + */ + public $enabled; + /** * The validation rules. * @@ -76,6 +83,7 @@ final class UpdateComponentCommand 'link' => 'url', 'order' => 'int', 'group_id' => 'int', + 'enabled' => 'bool', ]; /** @@ -88,10 +96,11 @@ final class UpdateComponentCommand * @param string $link * @param int $order * @param int $group_id + * @param bool $enabled * * @return void */ - public function __construct(Component $component, $name, $description, $status, $link, $order, $group_id) + public function __construct(Component $component, $name, $description, $status, $link, $order, $group_id, $enabled) { $this->component = $component; $this->name = $name; @@ -100,5 +109,6 @@ final class UpdateComponentCommand $this->link = $link; $this->order = $order; $this->group_id = $group_id; + $this->enabled = $enabled; } } diff --git a/app/Composers/StatusPageComposer.php b/app/Composers/StatusPageComposer.php index f52f3f5d..38d182e6 100644 --- a/app/Composers/StatusPageComposer.php +++ b/app/Composers/StatusPageComposer.php @@ -35,7 +35,7 @@ class StatusPageComposer 'favicon' => 'favicon-high-alert', ]; - if (Component::notStatus(1)->count() === 0) { + if (Component::where('enabled', true)->notStatus(1)->count() === 0) { // If all our components are ok, do we have any non-fixed incidents? $incidents = Incident::notScheduled()->orderBy('created_at', 'desc')->get(); $incidentCount = $incidents->count(); @@ -48,7 +48,7 @@ class StatusPageComposer ]; } } else { - if (Component::whereIn('status', [2, 3])->count() > 0) { + if (Component::where('enabled', true)->whereIn('status', [2, 3])->count() > 0) { $withData['favicon'] = 'favicon-medium-alert'; } } @@ -57,9 +57,9 @@ class StatusPageComposer $scheduledMaintenance = Incident::scheduled()->orderBy('scheduled_at')->get(); // Component & Component Group lists. - $usedComponentGroups = Component::where('group_id', '>', 0)->groupBy('group_id')->lists('group_id'); + $usedComponentGroups = Component::where('enabled', true)->where('group_id', '>', 0)->groupBy('group_id')->lists('group_id'); $componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get(); - $ungroupedComponents = Component::where('group_id', 0)->orderBy('order')->orderBy('created_at')->get(); + $ungroupedComponents = Component::where('enabled', true)->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get(); $view->with($withData) ->withComponentGroups($componentGroups) diff --git a/app/Handlers/Commands/Component/AddComponentCommandHandler.php b/app/Handlers/Commands/Component/AddComponentCommandHandler.php index 9b4d2332..b3bd9791 100644 --- a/app/Handlers/Commands/Component/AddComponentCommandHandler.php +++ b/app/Handlers/Commands/Component/AddComponentCommandHandler.php @@ -26,14 +26,18 @@ class AddComponentCommandHandler */ public function handle(AddComponentCommand $command) { - $component = Component::create(array_filter([ + $componentData = array_filter([ 'name' => $command->name, 'description' => $command->description, 'link' => $command->link, 'status' => $command->status, 'order' => $command->order, 'group_id' => $command->group_id, - ])); + ]); + + $componentData['enabled'] = $command->enabled; + + $component = Component::create($componentData); event(new ComponentWasAddedEvent($component)); diff --git a/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php b/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php index 06a6dc8d..d6cbb414 100644 --- a/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php +++ b/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php @@ -27,14 +27,18 @@ class UpdateComponentCommandHandler public function handle(UpdateComponentCommand $command) { $component = $command->component; - $component->update(array_filter([ + $componentData = array_filter([ 'name' => $command->name, 'description' => $command->description, 'link' => $command->link, 'status' => $command->status, 'order' => $command->order, 'group_id' => $command->group_id, - ])); + ]); + + $componentData['enabled'] = $command->enabled; + + $component->update($componentData); event(new ComponentWasUpdatedEvent($component)); diff --git a/app/Http/Controllers/Api/ComponentController.php b/app/Http/Controllers/Api/ComponentController.php index c15ae168..16f44930 100644 --- a/app/Http/Controllers/Api/ComponentController.php +++ b/app/Http/Controllers/Api/ComponentController.php @@ -69,7 +69,8 @@ class ComponentController extends AbstractApiController Binput::get('status'), Binput::get('link'), Binput::get('order'), - Binput::get('group_id') + Binput::get('group_id'), + (bool) Binput::get('enabled') )); } catch (Exception $e) { throw new BadRequestHttpException(); @@ -109,7 +110,8 @@ class ComponentController extends AbstractApiController Binput::get('status'), Binput::get('link'), Binput::get('order'), - Binput::get('group_id') + Binput::get('group_id'), + (bool) Binput::get('enabled') )); } catch (Exception $e) { throw new BadRequestHttpException(); diff --git a/app/Models/Component.php b/app/Models/Component.php index 5eba3fc2..a12642eb 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -32,6 +32,7 @@ class Component extends Model implements HasPresenter 'group_id' => 0, 'description' => '', 'link' => '', + 'enabled' => true, ]; /** @@ -46,6 +47,7 @@ class Component extends Model implements HasPresenter 'description' => 'string', 'link' => 'string', 'deleted_at' => 'date', + 'enabled' => 'bool', ]; /** @@ -61,6 +63,7 @@ class Component extends Model implements HasPresenter 'link', 'order', 'group_id', + 'enabled', ]; /** diff --git a/database/migrations/2015_11_03_211049_AlterTableComponentsAddEnabledColumn.php b/database/migrations/2015_11_03_211049_AlterTableComponentsAddEnabledColumn.php new file mode 100644 index 00000000..f50dadef --- /dev/null +++ b/database/migrations/2015_11_03_211049_AlterTableComponentsAddEnabledColumn.php @@ -0,0 +1,41 @@ +boolean('enabled')->after('group_id')->default(true); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('components', function (Blueprint $table) { + $table->dropColumn('enabled'); + }); + } +} diff --git a/resources/views/partials/components.blade.php b/resources/views/partials/components.blade.php index ed14c0a5..d976130c 100644 --- a/resources/views/partials/components.blade.php +++ b/resources/views/partials/components.blade.php @@ -1,14 +1,14 @@