Finish add and remove components via commands

This commit is contained in:
Joseph Cohen
2015-08-24 14:38:10 -05:00
committed by James Brooks
parent 30eec9da24
commit 4a5110ae53
8 changed files with 152 additions and 11 deletions

View File

@@ -0,0 +1,90 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Commands\Component;
class AddComponentCommand
{
/**
* The component name.
*
* @var string
*/
public $name;
/**
* The component description.
*
* @var string
*/
public $description;
/**
* The component status.
*
* @var int
*/
public $status;
/**
* The component link.
*
* @var string
*/
public $link;
/**
* The component order.
*
* @var int
*/
public $order;
/**
* The component group.
*
* @var int
*/
public $group_id;
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'name' => 'required|string',
'status' => 'required|integer',
'link' => 'url',
];
/**
* Create a new add metric command instance.
*
* @param string $name
* @param string $description
* @param int $status
* @param string $link
* @param int $order
* @param int $group_id
*
* @return void
*/
public function __construct($name, $description, $status, $link, $order, $group_id)
{
$this->name = $name;
$this->description = $description;
$this->status = (int) $status;
$this->link = $link;
$this->order = $order;
$this->group_id = $group_id;
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Commands\Component;
use CachetHQ\Cachet\Models\Component;
class RemoveComponentCommand
{
/**
* The component to remove.
*
* @var \CachetHQ\Cachet\Models\Component
*/
public $component;
/**
* Create a new remove component command instance.
*
* @param \CachetHQ\Cachet\Models\Component $component
*
* @return void
*/
public function __construct(Component $component)
{
$this->component = $component;
}
}

View File

@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Events\Metric;
namespace CachetHQ\Cachet\Events\Component;
use CachetHQ\Cachet\Models\Component;

View File

@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Events\Metric;
namespace CachetHQ\Cachet\Events\Component;
use CachetHQ\Cachet\Models\Component;

View File

@@ -15,7 +15,7 @@ use CachetHQ\Cachet\Commands\Component\AddComponentCommand;
use CachetHQ\Cachet\Events\Component\ComponentWasAddedEvent;
use CachetHQ\Cachet\Models\Component;
class AddComponentHandler
class AddComponentCommandHandler
{
/**
* Handle the add component command.

View File

@@ -12,10 +12,10 @@
namespace CachetHQ\Cachet\Handlers\Commands\Component;
use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Events\Component\ComponentWasAddedEvent;
use CachetHQ\Cachet\Events\Component\ComponentWasRemovedEvent;
use CachetHQ\Cachet\Models\Component;
class RemoveComponentHandler
class RemoveComponentCommandHandler
{
/**
* Handle the remove component command.

View File

@@ -11,16 +11,21 @@
namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\Component\AddComponentCommand;
use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Tag;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class ComponentController extends AbstractApiController
{
use DispatchesJobs;
/**
* Get all components.
*
@@ -56,10 +61,15 @@ class ComponentController extends AbstractApiController
*/
public function postComponents(Guard $auth)
{
$componentData = Binput::except('tags');
try {
$component = Component::create($componentData);
$component = $this->dispatch(new AddComponentCommand(
Binput::get('name'),
Binput::get('description'),
Binput::get('status'),
Binput::get('link'),
Binput::get('order'),
Binput::get('group_id')
));
} catch (Exception $e) {
throw new BadRequestHttpException();
}
@@ -119,7 +129,7 @@ class ComponentController extends AbstractApiController
*/
public function deleteComponent(Component $component)
{
$component->delete();
$this->dispatch(new RemoveComponentCommand($component));
return $this->noContent();
}

View File

@@ -11,17 +11,22 @@
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
use CachetHQ\Cachet\Commands\Component\AddComponentCommand;
use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand;
use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Tag;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
class ComponentController extends Controller
{
use DispatchesJobs;
protected $subMenu = [];
public function __construct()
@@ -157,7 +162,7 @@ class ComponentController extends Controller
$tags = array_pull($_component, 'tags');
try {
$component = Component::create($_component);
$component = $this->dispatchFromArray(AddComponentCommand::class, Binput::get('component'));
} catch (ValidationException $e) {
return Redirect::route('dashboard.components.add')
->withInput(Binput::all())
@@ -188,7 +193,7 @@ class ComponentController extends Controller
*/
public function deleteComponentAction(Component $component)
{
$component->delete();
$this->dispatch(new RemoveComponentCommand($component));
return Redirect::route('dashboard.components.index');
}