Cachet is now a Laravel 5 app
This commit is contained in:
160
app/Http/Controllers/Api/AbstractApiController.php
Normal file
160
app/Http/Controllers/Api/AbstractApiController.php
Normal file
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Http\Controllers\Api;
|
||||
|
||||
use CachetHQ\Cachet\Http\Controllers\AbstractController as BaseController;
|
||||
use Illuminate\Contracts\Pagination\Paginator;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Response;
|
||||
|
||||
abstract class AbstractApiController extends BaseController
|
||||
{
|
||||
/**
|
||||
* The HTTP response headers.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $headers = [];
|
||||
|
||||
/**
|
||||
* The HTTP response meta data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $meta = [];
|
||||
|
||||
/**
|
||||
* The HTTP response data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $data = [];
|
||||
|
||||
/**
|
||||
* The HTTP response status code.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $statusCode = 200;
|
||||
|
||||
/**
|
||||
* Set the response headers.
|
||||
*
|
||||
* @param array $headers
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setHeaders(array $headers)
|
||||
{
|
||||
$this->headers = $headers;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the response meta data.
|
||||
*
|
||||
* @param array $meta
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setMetaData(array $meta)
|
||||
{
|
||||
$this->meta = $meta;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the response meta data.
|
||||
*
|
||||
* @param array $data
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setData(array $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the response status code.
|
||||
*
|
||||
* @param int $statusCode
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setStatusCode($satusCode)
|
||||
{
|
||||
$this->satusCode = $satusCode;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Respond with a pagination response.
|
||||
*
|
||||
* @param \Illuminate\Pagination\Paginator $paginator
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
protected function paginator(Paginator $paginator, Request $request)
|
||||
{
|
||||
foreach ($request->query as $key => $value) {
|
||||
if ($key != 'page') {
|
||||
$paginator->addQuery($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
$pagination = [
|
||||
'pagination' => [
|
||||
'total' => $paginator->total(),
|
||||
'count' => count($paginator->items()),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'total_pages' => $paginator->lastPage(),
|
||||
'links' => [
|
||||
'next_page' => $paginator->nextPageUrl(),
|
||||
'previous_page' => $paginator->previousPageUrl(),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $this->setMetaData($pagination)->setData($paginator->items())->respond();
|
||||
}
|
||||
|
||||
/**
|
||||
* Respond with a no content response.
|
||||
*
|
||||
* @param string $message
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
protected function noContent()
|
||||
{
|
||||
return $this->setStatusCode(204)->respond();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the response.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
protected function respond()
|
||||
{
|
||||
if (! empty($this->meta)) {
|
||||
$response['meta'] = $this->meta;
|
||||
}
|
||||
|
||||
$response['data'] = $this->data;
|
||||
|
||||
if ($this->data instanceof Arrayable) {
|
||||
$response['data'] = $this->data->toArray();
|
||||
}
|
||||
|
||||
return Response::json($response, $this->statusCode, $this->headers);
|
||||
}
|
||||
}
|
||||
124
app/Http/Controllers/Api/ComponentController.php
Normal file
124
app/Http/Controllers/Api/ComponentController.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Http\Controllers\Api;
|
||||
|
||||
use CachetHQ\Cachet\Models\Tag;
|
||||
use CachetHQ\Cachet\Repositories\Component\ComponentRepository;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ComponentController extends AbstractApiController
|
||||
{
|
||||
/**
|
||||
* The component repository instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Repositories\Component\ComponentRepository
|
||||
*/
|
||||
protected $component;
|
||||
|
||||
/**
|
||||
* Create a new component controller instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Repositories\Component\ComponentRepository $component
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(ComponentRepository $component)
|
||||
{
|
||||
$this->component = $component;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all components.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function getComponents(Request $request)
|
||||
{
|
||||
$components = $this->component->paginate(Binput::get('per_page', 20));
|
||||
|
||||
return $this->paginator($components, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single component.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\Component
|
||||
*/
|
||||
public function getComponent($id)
|
||||
{
|
||||
return $this->component->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new component.
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\Component
|
||||
*/
|
||||
public function postComponents()
|
||||
{
|
||||
$component = $this->component->create(
|
||||
$this->auth->user()->id,
|
||||
Binput::except('tags')
|
||||
);
|
||||
|
||||
if (Binput::has('tags')) {
|
||||
// The component was added successfully, so now let's deal with the tags.
|
||||
$tags = preg_split('/ ?, ?/', Binput::get('tags'));
|
||||
|
||||
// For every tag, do we need to create it?
|
||||
$componentTags = array_map(function ($taggable) use ($component) {
|
||||
return Tag::firstOrCreate([
|
||||
'name' => $taggable,
|
||||
])->id;
|
||||
}, $tags);
|
||||
|
||||
$component->tags()->sync($componentTags);
|
||||
}
|
||||
|
||||
return $component;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing component.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\Component
|
||||
*/
|
||||
public function putComponent($id)
|
||||
{
|
||||
$component = $this->component->update($id, Binput::except('tags'));
|
||||
|
||||
if (Binput::has('tags')) {
|
||||
$tags = preg_split('/ ?, ?/', Binput::get('tags'));
|
||||
|
||||
// For every tag, do we need to create it?
|
||||
$componentTags = array_map(function ($taggable) use ($component) {
|
||||
return Tag::firstOrCreate([
|
||||
'name' => $taggable,
|
||||
])->id;
|
||||
}, $tags);
|
||||
|
||||
$component->tags()->sync($componentTags);
|
||||
}
|
||||
|
||||
return $component;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an existing component.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function deleteComponent($id)
|
||||
{
|
||||
$this->component->destroy($id);
|
||||
|
||||
return $this->noContent();
|
||||
}
|
||||
}
|
||||
89
app/Http/Controllers/Api/IncidentController.php
Normal file
89
app/Http/Controllers/Api/IncidentController.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Http\Controllers\Api;
|
||||
|
||||
use CachetHQ\Cachet\Repositories\Incident\IncidentRepository;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class IncidentController extends AbstractApiController
|
||||
{
|
||||
/**
|
||||
* The incident repository instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Repositories\Incident\IncidentRepository
|
||||
*/
|
||||
protected $incident;
|
||||
|
||||
/**
|
||||
* Create a new incident controller instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Repositories\Incident\IncidentRepository $incident
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(IncidentRepository $incident)
|
||||
{
|
||||
$this->incident = $incident;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all incidents.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function getIncidents(Request $request)
|
||||
{
|
||||
$incidents = $this->incident->paginate(Binput::get('per_page', 20));
|
||||
|
||||
return $this->paginator($incidents, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single incident.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\Incident
|
||||
*/
|
||||
public function getIncident($id)
|
||||
{
|
||||
return $this->incident->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new incident.
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\Incident
|
||||
*/
|
||||
public function postIncidents()
|
||||
{
|
||||
return $this->incident->create($this->auth->user()->id, Binput::all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing incident.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\Incident
|
||||
*/
|
||||
public function putIncident($id)
|
||||
{
|
||||
return $this->incident->update($id, Binput::all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an existing incident.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function deleteIncident($id)
|
||||
{
|
||||
$this->incident->destroy($id);
|
||||
|
||||
return $this->noContent();
|
||||
}
|
||||
}
|
||||
101
app/Http/Controllers/Api/MetricController.php
Normal file
101
app/Http/Controllers/Api/MetricController.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Http\Controllers\Api;
|
||||
|
||||
use CachetHQ\Cachet\Repositories\Metric\MetricRepository;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class MetricController extends AbstractApiController
|
||||
{
|
||||
/**
|
||||
* The metric repository instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Repositories\Metric\MetricRepository
|
||||
*/
|
||||
protected $metric;
|
||||
|
||||
/**
|
||||
* Create a new metric controller instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Repositories\Metric\MetricRepository $metric
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(MetricRepository $metric)
|
||||
{
|
||||
$this->metric = $metric;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all metrics.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function getMetrics(Request $request)
|
||||
{
|
||||
$metrics = $this->metric->paginate(Binput::get('per_page', 20));
|
||||
|
||||
return $this->paginator($metrics, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single metric.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\Metric
|
||||
*/
|
||||
public function getMetric($id)
|
||||
{
|
||||
return $this->metric->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all metric points.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function getMetricPoints($id)
|
||||
{
|
||||
return $this->metric->points($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new metric.
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\Metric
|
||||
*/
|
||||
public function postMetrics()
|
||||
{
|
||||
return $this->metric->create(Binput::all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing metric.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\Metric
|
||||
*/
|
||||
public function putMetric($id)
|
||||
{
|
||||
return $this->metric->update($id, Binput::all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an existing metric.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function deleteMetric($id)
|
||||
{
|
||||
$this->metric->destroy($id);
|
||||
|
||||
return $this->noContent();
|
||||
}
|
||||
}
|
||||
83
app/Http/Controllers/Api/MetricPointController.php
Normal file
83
app/Http/Controllers/Api/MetricPointController.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Http\Controllers\Api;
|
||||
|
||||
use CachetHQ\Cachet\Repositories\MetricPoint\MetricPointRepository;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
|
||||
class MetricPointController extends AbstractApiController
|
||||
{
|
||||
/**
|
||||
* The metric point repository instance.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Repositories\MetricPoint\MetricPointRepository
|
||||
*/
|
||||
protected $metricPoint;
|
||||
|
||||
/**
|
||||
* Create a new metric point controller instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Repositories\MetricPoint\MetricPointRepository $metricPoint
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(MetricPointRepository $metricPoint)
|
||||
{
|
||||
$this->metricPoint = $metricPoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single metric point.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\MetricPoint
|
||||
*/
|
||||
public function getMetricPoints($id)
|
||||
{
|
||||
return $this->metricPoint->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new metric point.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\MetricPoint
|
||||
*/
|
||||
public function postMetricPoints($id)
|
||||
{
|
||||
return $this->metricPoint->create($id, Binput::all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a metric point.
|
||||
*
|
||||
* @param int $metricId
|
||||
* @param int $pointId
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\MetricPoint
|
||||
*/
|
||||
public function putMetricPoint($metricId, $pointId)
|
||||
{
|
||||
$metricPoint = $this->metricPoint->findOrFail($pointId);
|
||||
$metricPoint->update(Binput::all());
|
||||
|
||||
return $metricPoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys a metric point.
|
||||
*
|
||||
* @param int $metricId
|
||||
* @param int $pointId
|
||||
*
|
||||
* @return \Dingo\Api\Http\Response
|
||||
*/
|
||||
public function deleteMetricPoint($metricId, $pointId)
|
||||
{
|
||||
$this->metricPoint->destroy($pointId);
|
||||
|
||||
return $this->noContent();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user