mirror of
https://github.com/CachetHQ/Cachet.git
synced 2025-01-17 21:49:01 +01:00
Abstract presenter, timestampstrait, update api responses
This commit is contained in:
parent
10bcbc6169
commit
088b54a5ce
@ -44,9 +44,9 @@ class Repository
|
||||
/**
|
||||
* Returns a setting from the database.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $default
|
||||
* @param bool $checkEnv
|
||||
* @param string $name
|
||||
* @param string|null $default
|
||||
* @param bool $checkEnv
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
|
@ -16,6 +16,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
|
||||
use CachetHQ\Cachet\Models\Tag;
|
||||
use CachetHQ\Cachet\Repositories\Component\ComponentRepository;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use Illuminate\Contracts\Auth\Guard;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ComponentController extends AbstractApiController
|
||||
@ -58,18 +59,20 @@ class ComponentController extends AbstractApiController
|
||||
*/
|
||||
public function getComponent($id)
|
||||
{
|
||||
return $this->component->findOrFail($id);
|
||||
return $this->item($this->component->findOrFail($id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new component.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Auth\Guard $auth
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\Component
|
||||
*/
|
||||
public function postComponents()
|
||||
public function postComponents(Guard $auth)
|
||||
{
|
||||
$component = $this->component->create(
|
||||
$this->auth->user()->id,
|
||||
$auth->user()->id,
|
||||
Binput::except('tags')
|
||||
);
|
||||
|
||||
@ -87,7 +90,7 @@ class ComponentController extends AbstractApiController
|
||||
$component->tags()->sync($componentTags);
|
||||
}
|
||||
|
||||
return $component;
|
||||
return $this->item($component);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -114,7 +117,7 @@ class ComponentController extends AbstractApiController
|
||||
$component->tags()->sync($componentTags);
|
||||
}
|
||||
|
||||
return $component;
|
||||
return $this->item($component);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -15,6 +15,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
|
||||
|
||||
use CachetHQ\Cachet\Repositories\Incident\IncidentRepository;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use Illuminate\Contracts\Auth\Guard;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class IncidentController extends AbstractApiController
|
||||
@ -57,17 +58,19 @@ class IncidentController extends AbstractApiController
|
||||
*/
|
||||
public function getIncident($id)
|
||||
{
|
||||
return $this->incident->findOrFail($id);
|
||||
return $this->item($this->incident->findOrFail($id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new incident.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Auth\Guard $auth
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\Incident
|
||||
*/
|
||||
public function postIncidents()
|
||||
public function postIncidents(Guard $auth)
|
||||
{
|
||||
return $this->incident->create($this->auth->user()->id, Binput::all());
|
||||
return $this->item($this->incident->create($auth->user()->id, Binput::all()));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -79,7 +82,7 @@ class IncidentController extends AbstractApiController
|
||||
*/
|
||||
public function putIncident($id)
|
||||
{
|
||||
return $this->incident->update($id, Binput::all());
|
||||
return $this->item($this->incident->update($id, Binput::all()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -79,7 +79,7 @@ class MetricController extends AbstractApiController
|
||||
*/
|
||||
public function postMetrics()
|
||||
{
|
||||
return $this->metric->create(Binput::all());
|
||||
return $this->item($this->metric->create(Binput::all()));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -91,7 +91,7 @@ class MetricController extends AbstractApiController
|
||||
*/
|
||||
public function putMetric($id)
|
||||
{
|
||||
return $this->metric->update($id, Binput::all());
|
||||
return $this->item($this->metric->update($id, Binput::all()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,7 +44,7 @@ class MetricPointController extends AbstractApiController
|
||||
*/
|
||||
public function getMetricPoints($id)
|
||||
{
|
||||
return $this->metricPoint->findOrFail($id);
|
||||
return $this->item($this->metricPoint->findOrFail($id));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -56,7 +56,7 @@ class MetricPointController extends AbstractApiController
|
||||
*/
|
||||
public function postMetricPoints($id)
|
||||
{
|
||||
return $this->metricPoint->create($id, Binput::all());
|
||||
return $this->item($this->metricPoint->create($id, Binput::all()));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -72,7 +72,7 @@ class MetricPointController extends AbstractApiController
|
||||
$metricPoint = $this->metricPoint->findOrFail($pointId);
|
||||
$metricPoint->update(Binput::all());
|
||||
|
||||
return $metricPoint;
|
||||
return $this->item($metricPoint);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -16,6 +16,7 @@ namespace CachetHQ\Cachet\Models;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use McCool\LaravelAutoPresenter\HasPresenter;
|
||||
use Watson\Validating\ValidatingTrait;
|
||||
|
||||
/**
|
||||
@ -31,7 +32,7 @@ use Watson\Validating\ValidatingTrait;
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
* @property \Carbon\Carbon $deleted_at
|
||||
*/
|
||||
class Component extends Model
|
||||
class Component extends Model implements HasPresenter
|
||||
{
|
||||
use SoftDeletes, ValidatingTrait;
|
||||
|
||||
@ -161,4 +162,14 @@ class Component extends Model
|
||||
|
||||
return implode(', ', $tags->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the presenter class.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPresenterClass()
|
||||
{
|
||||
return 'CachetHQ\Cachet\Presenters\ComponentPresenter';
|
||||
}
|
||||
}
|
||||
|
40
app/Presenters/AbstractPresenter.php
Normal file
40
app/Presenters/AbstractPresenter.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) James Brooks <james@cachethq.io>
|
||||
* (c) Joseph Cohen <joseph.cohen@dinkbit.com>
|
||||
* (c) Graham Campbell <graham@mineuk.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CachetHQ\Cachet\Presenters;
|
||||
|
||||
use CachetHQ\Cachet\Facades\Setting;
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use McCool\LaravelAutoPresenter\BasePresenter as BaseLaravelAutoPresenter;
|
||||
|
||||
abstract class AbstractPresenter extends BaseLaravelAutoPresenter implements Arrayable
|
||||
{
|
||||
/**
|
||||
* The setting repository.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Config\Repository
|
||||
*/
|
||||
protected $setting;
|
||||
|
||||
/**
|
||||
* Create a incident presenter instance.
|
||||
*
|
||||
* @param object $resource
|
||||
*/
|
||||
public function __construct($resource)
|
||||
{
|
||||
parent::__construct($resource);
|
||||
|
||||
$this->setting = app('setting');
|
||||
}
|
||||
}
|
34
app/Presenters/ComponentPresenter.php
Normal file
34
app/Presenters/ComponentPresenter.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) James Brooks <james@cachethq.io>
|
||||
* (c) Joseph Cohen <joseph.cohen@dinkbit.com>
|
||||
* (c) Graham Campbell <graham@mineuk.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CachetHQ\Cachet\Presenters;
|
||||
|
||||
use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait;
|
||||
|
||||
class ComponentPresenter extends AbstractPresenter
|
||||
{
|
||||
use TimestampsTrait;
|
||||
|
||||
/**
|
||||
* Convert the presenter instance to an array.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return array_merge($this->wrappedObject->toArray(), [
|
||||
'created_at' => $this->created_at(),
|
||||
'updated_at' => $this->updated_at(),
|
||||
]);
|
||||
}
|
||||
}
|
@ -14,32 +14,13 @@
|
||||
namespace CachetHQ\Cachet\Presenters;
|
||||
|
||||
use CachetHQ\Cachet\Facades\Setting;
|
||||
use CachetHQ\Cachet\Models\Incident;
|
||||
use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait;
|
||||
use GrahamCampbell\Markdown\Facades\Markdown;
|
||||
use Jenssegers\Date\Date;
|
||||
use McCool\LaravelAutoPresenter\BasePresenter;
|
||||
|
||||
class IncidentPresenter extends BasePresenter
|
||||
class IncidentPresenter extends AbstractPresenter
|
||||
{
|
||||
/**
|
||||
* Time zone setting.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $tz;
|
||||
|
||||
/**
|
||||
* Create a incident presenter instance.
|
||||
*
|
||||
* @param object $resource
|
||||
*/
|
||||
public function __construct($resource)
|
||||
{
|
||||
parent::__construct($resource);
|
||||
|
||||
$this->tz = Setting::get('app_timezone');
|
||||
$this->format = Setting::get('incident_date_format') ?: 'l jS F Y H:i:s';
|
||||
}
|
||||
use TimestampsTrait;
|
||||
|
||||
/**
|
||||
* Renders the message from Markdown into HTML.
|
||||
@ -59,7 +40,7 @@ class IncidentPresenter extends BasePresenter
|
||||
public function created_at_diff()
|
||||
{
|
||||
return (new Date($this->wrappedObject->created_at))
|
||||
->setTimezone($this->tz)
|
||||
->setTimezone($this->setting->get('app_timezone'))
|
||||
->diffForHumans();
|
||||
}
|
||||
|
||||
@ -71,8 +52,8 @@ class IncidentPresenter extends BasePresenter
|
||||
public function created_at_formatted()
|
||||
{
|
||||
return ucfirst((new Date($this->wrappedObject->created_at))
|
||||
->setTimezone($this->tz)
|
||||
->format($this->format));
|
||||
->setTimezone($this->setting->get('app_timezone'))
|
||||
->format($this->setting->get('incident_date_format', 'l jS F Y H:i:s')));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,7 +63,7 @@ class IncidentPresenter extends BasePresenter
|
||||
*/
|
||||
public function created_at_datetimepicker()
|
||||
{
|
||||
return $this->wrappedObject->created_at->setTimezone($this->tz)->format('d/m/Y H:i');
|
||||
return $this->wrappedObject->created_at->setTimezone($this->setting->get('app_timezone'))->format('d/m/Y H:i');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,7 +73,18 @@ class IncidentPresenter extends BasePresenter
|
||||
*/
|
||||
public function created_at_iso()
|
||||
{
|
||||
return $this->wrappedObject->created_at->setTimezone($this->tz)->toISO8601String();
|
||||
return $this->wrappedObject->created_at->setTimezone($this->setting->get('app_timezone'))->toISO8601String();
|
||||
}
|
||||
|
||||
/**
|
||||
* Present formatted date time.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function scheduled_at()
|
||||
{
|
||||
return (new Date($this->wrappedObject->scheduled_at))
|
||||
->setTimezone($this->setting->get('app_timezone'))->toDateTimeString();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -103,7 +95,7 @@ class IncidentPresenter extends BasePresenter
|
||||
public function scheduled_at_diff()
|
||||
{
|
||||
return (new Date($this->wrappedObject->scheduled_at))
|
||||
->setTimezone($this->tz)
|
||||
->setTimezone($this->setting->get('app_timezone'))
|
||||
->diffForHumans();
|
||||
}
|
||||
|
||||
@ -115,8 +107,8 @@ class IncidentPresenter extends BasePresenter
|
||||
public function scheduled_at_formatted()
|
||||
{
|
||||
return ucfirst((new Date($this->wrappedObject->scheduled_at))
|
||||
->setTimezone($this->tz)
|
||||
->format($this->format));
|
||||
->setTimezone($this->setting->get('app_timezone'))
|
||||
->format($this->setting->get('incident_date_format', 'l jS F Y H:i:s')));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -126,7 +118,7 @@ class IncidentPresenter extends BasePresenter
|
||||
*/
|
||||
public function scheduled_at_iso()
|
||||
{
|
||||
return $this->wrappedObject->scheduled_at->setTimezone($this->tz)->toISO8601String();
|
||||
return $this->wrappedObject->scheduled_at->setTimezone($this->setting->get('app_timezone'))->toISO8601String();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -136,7 +128,7 @@ class IncidentPresenter extends BasePresenter
|
||||
*/
|
||||
public function scheduled_at_datetimepicker()
|
||||
{
|
||||
return $this->wrappedObject->scheduled_at->setTimezone($this->tz)->format('d/m/Y H:i');
|
||||
return $this->wrappedObject->scheduled_at->setTimezone($this->setting->get('app_timezone'))->format('d/m/Y H:i');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -161,4 +153,18 @@ class IncidentPresenter extends BasePresenter
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the presenter instance to an array.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return array_merge($this->wrappedObject->toArray(), [
|
||||
'scheduled_at' => $this->created_at(),
|
||||
'created_at' => $this->created_at(),
|
||||
'updated_at' => $this->updated_at(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,8 @@
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) James Brooks <james@cachethq.io>
|
||||
* (c) Joseph Cohen <joseph.cohen@dinkbit.com>
|
||||
* (c) Graham Campbell <graham@mineuk.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
@ -11,58 +13,16 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Presenters;
|
||||
|
||||
use CachetHQ\Cachet\Facades\Setting;
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Jenssegers\Date\Date;
|
||||
use McCool\LaravelAutoPresenter\BasePresenter;
|
||||
use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait;
|
||||
|
||||
class MetricPointPresenter extends BasePresenter implements Arrayable
|
||||
class MetricPointPresenter extends AbstractPresenter
|
||||
{
|
||||
/**
|
||||
* Time zone setting.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $tz;
|
||||
|
||||
/**
|
||||
* Create a incident presenter instance.
|
||||
*
|
||||
* @param object $resource
|
||||
*/
|
||||
public function __construct($resource)
|
||||
{
|
||||
parent::__construct($resource);
|
||||
|
||||
$this->tz = Setting::get('app_timezone');
|
||||
}
|
||||
|
||||
/**
|
||||
* Present formatted date time.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function created_at()
|
||||
{
|
||||
return (new Date($this->wrappedObject->created_at))
|
||||
->setTimezone($this->tz)->toDateTimeString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Present formatted date time.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function updated_at()
|
||||
{
|
||||
return (new Date($this->wrappedObject->updated_at))
|
||||
->setTimezone($this->tz)->toDateTimeString();
|
||||
}
|
||||
use TimestampsTrait;
|
||||
|
||||
/**
|
||||
* Convert the presenter instance to an array.
|
||||
*
|
||||
* @return array
|
||||
* @return string[]
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
|
@ -4,6 +4,8 @@
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) James Brooks <james@cachethq.io>
|
||||
* (c) Joseph Cohen <joseph.cohen@dinkbit.com>
|
||||
* (c) Graham Campbell <graham@mineuk.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
@ -11,58 +13,16 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Presenters;
|
||||
|
||||
use CachetHQ\Cachet\Facades\Setting;
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Jenssegers\Date\Date;
|
||||
use McCool\LaravelAutoPresenter\BasePresenter;
|
||||
use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait;
|
||||
|
||||
class MetricPresenter extends BasePresenter implements Arrayable
|
||||
class MetricPresenter extends AbstractPresenter
|
||||
{
|
||||
/**
|
||||
* Time zone setting.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $tz;
|
||||
|
||||
/**
|
||||
* Create a incident presenter instance.
|
||||
*
|
||||
* @param object $resource
|
||||
*/
|
||||
public function __construct($resource)
|
||||
{
|
||||
parent::__construct($resource);
|
||||
|
||||
$this->tz = Setting::get('app_timezone');
|
||||
}
|
||||
|
||||
/**
|
||||
* Present formatted date time.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function created_at()
|
||||
{
|
||||
return (new Date($this->wrappedObject->created_at))
|
||||
->setTimezone($this->tz)->toDateTimeString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Present formatted date time.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function updated_at()
|
||||
{
|
||||
return (new Date($this->wrappedObject->updated_at))
|
||||
->setTimezone($this->tz)->toDateTimeString();
|
||||
}
|
||||
use TimestampsTrait;
|
||||
|
||||
/**
|
||||
* Convert the presenter instance to an array.
|
||||
*
|
||||
* @return array
|
||||
* @return string[]
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
|
52
app/Presenters/Traits/TimestampsTrait.php
Normal file
52
app/Presenters/Traits/TimestampsTrait.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Cachet.
|
||||
*
|
||||
* (c) James Brooks <james@cachethq.io>
|
||||
* (c) Joseph Cohen <joseph.cohen@dinkbit.com>
|
||||
* (c) Graham Campbell <graham@mineuk.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CachetHQ\Cachet\Presenters\Traits;
|
||||
|
||||
use Jenssegers\Date\Date;
|
||||
|
||||
trait TimestampsTrait
|
||||
{
|
||||
/**
|
||||
* Present formatted date time.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function created_at()
|
||||
{
|
||||
return (new Date($this->wrappedObject->created_at))
|
||||
->setTimezone($this->setting->get('app_timezone'))->toDateTimeString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Present formatted date time.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function updated_at()
|
||||
{
|
||||
return (new Date($this->wrappedObject->updated_at))
|
||||
->setTimezone($this->setting->get('app_timezone'))->toDateTimeString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Present formatted date time.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function deleted_at()
|
||||
{
|
||||
return (new Date($this->wrappedObject->deleted_at))
|
||||
->setTimezone($this->setting->get('app_timezone'))->toDateTimeString();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user